CapersProject/Assets/02.Scripts/LocalizationManager.cs

101 lines
2.8 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;
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,
Spanish = 3
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]
private Material _defaultFontMaterial;
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
if (localeType == LocaleType.ChineseSimplified)
{
SetMaterialOutline(0.1f);
}
else
{
SetMaterialOutline(0.2f);
}
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 => "中文(简体)",
LocaleType.Spanish => "Español",
_ => "Unknown"
};
}
2024-11-28 23:20:42 +00:00
public void SetMaterialOutline(float value)
{
_defaultFontMaterial.SetFloat(OutlineWidthHash, value);
}
2024-11-11 02:02:24 +00:00
}
}