CapersProject/Assets/02.Scripts/LocalizationManager.cs

119 lines
3.5 KiB
C#
Raw Normal View History

2024-11-11 12:23:27 +00:00
using System;
2024-11-11 02:02:24 +00:00
using System.Collections;
2024-12-03 12:30:09 +00:00
using System.Collections.Generic;
2024-11-11 02:02:24 +00:00
using Sirenix.OdinInspector;
2024-11-28 23:20:42 +00:00
using UnityEngine;
2024-11-11 02:02:24 +00:00
using UnityEngine.Localization.Settings;
namespace BlueWater
{
public enum LocaleType
{
2024-11-28 23:07:50 +00:00
Korean = 0,
English = 1,
ChineseSimplified = 2,
2024-12-10 06:49:15 +00:00
ChineseTradition = 3,
Japanese = 4,
Spanish = 5,
Russian = 6,
French = 7
2024-11-11 02:02:24 +00:00
}
public class LocalizationManager : Singleton<LocalizationManager>
{
2024-11-11 12:23:27 +00:00
public bool IsInitialized;
2024-11-28 23:20:42 +00:00
[SerializeField]
2024-12-03 12:30:09 +00:00
private List<Material> _usedFontMaterials = new();
[SerializeField]
private float _defaultOutlineSize = 0.2f;
[SerializeField]
private float _chineseOutlineSize = 0.05f;
2024-11-11 12:23:27 +00:00
2024-11-11 02:02:24 +00:00
private bool _isChanging;
2024-11-28 23:20:42 +00:00
private static readonly int OutlineWidthHash = Shader.PropertyToID("_OutlineWidth");
2024-11-11 02:02:24 +00:00
private void Start()
{
2024-11-19 13:32:43 +00:00
var index = ES3.Load(SaveData.Locale, GetCurrentLocaleIndex());
2024-11-11 12:23:27 +00:00
ChangeLocale((LocaleType)index, () => IsInitialized = true);
2024-11-11 02:02:24 +00:00
}
/// <summary>
/// <para>0 - 한국어</para>
/// 1 - 영어
/// </summary>
/// <param name="index"></param>
[Button("언어 변경")]
2024-11-11 12:23:27 +00:00
public void ChangeLocale(LocaleType localeType, Action completeEvent = null)
2024-11-11 02:02:24 +00:00
{
if (_isChanging) return;
2024-11-11 12:23:27 +00:00
StartCoroutine(ChangeLocaleCoroutine(localeType, completeEvent));
2024-11-11 02:02:24 +00:00
}
2024-11-11 12:23:27 +00:00
private IEnumerator ChangeLocaleCoroutine(LocaleType localeType, Action completeEvent)
2024-11-11 02:02:24 +00:00
{
_isChanging = true;
yield return LocalizationSettings.InitializationOperation;
LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[(int)localeType];
2024-11-28 23:20:42 +00:00
2024-12-10 06:49:15 +00:00
if (localeType == LocaleType.ChineseSimplified || localeType == LocaleType.ChineseTradition)
2024-11-28 23:20:42 +00:00
{
2024-12-03 12:30:09 +00:00
SetMaterialOutline(_chineseOutlineSize);
2024-11-28 23:20:42 +00:00
}
else
{
2024-12-03 12:30:09 +00:00
SetMaterialOutline(_defaultOutlineSize);
2024-11-28 23:20:42 +00:00
}
2024-11-19 13:32:43 +00:00
ES3.Save(SaveData.Locale, (int)localeType);
2024-11-11 12:23:27 +00:00
2024-11-11 02:02:24 +00:00
_isChanging = false;
2024-11-11 12:23:27 +00:00
completeEvent?.Invoke();
2024-11-11 02:02:24 +00:00
}
2024-11-11 12:23:27 +00:00
public int GetCurrentLocaleIndex()
2024-11-11 02:02:24 +00:00
{
var selectedLocale = LocalizationSettings.SelectedLocale;
var locales = LocalizationSettings.AvailableLocales.Locales;
for (var i = 0; i < locales.Count; i++)
{
if (locales[i] == selectedLocale)
{
return i;
}
}
return -1;
}
2024-11-28 23:07:50 +00:00
public string GetLocaleDisplayName(LocaleType localeType)
{
return localeType switch
{
LocaleType.Korean => "한국어",
LocaleType.English => "English",
LocaleType.ChineseSimplified => "中文(简体)",
2024-12-10 06:49:15 +00:00
LocaleType.ChineseTradition => "中文(繁體)",
LocaleType.Japanese => "日本語",
2024-11-28 23:07:50 +00:00
LocaleType.Spanish => "Español",
2024-12-10 06:49:15 +00:00
LocaleType.Russian => "Русский",
LocaleType.French => "Français",
2024-11-28 23:07:50 +00:00
_ => "Unknown"
};
}
2024-11-28 23:20:42 +00:00
public void SetMaterialOutline(float value)
{
2024-12-03 12:30:09 +00:00
foreach (var element in _usedFontMaterials)
{
element.SetFloat(OutlineWidthHash, value);
}
2024-11-28 23:20:42 +00:00
}
2024-11-11 02:02:24 +00:00
}
}