using System.Collections; using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif public enum LocalizationLanguage { English, Japanese, Spanish }; public class Localization : ScriptableObject { const string path = "Assets/Resources/Localization.asset"; public static LocalizationLanguage SelectedLanguage; public static readonly int LanguageCount = System.Enum.GetNames(typeof(LocalizationLanguage)).Length; public static readonly System.Array Langauges = typeof(LocalizationLanguage).GetEnumValues(); private static Localization _Instance; public static Localization Instance{ get { if (_Instance == null) { #if UNITY_EDITOR _Instance = AssetDatabase.LoadAssetAtPath(path); if (_Instance == null) { _Instance = ScriptableObject.CreateInstance(); AssetDatabase.CreateAsset(_Instance, path); } #else _Instance = Resources.Load("Localization"); #endif } return _Instance; } } public static void RebuildKeyDictionary(){ if (keyDictionary == null) keyDictionary = new Dictionary(); else keyDictionary.Clear(); var locals = Instance.GetLocals(); foreach (var l in locals){ keyDictionary.Add(l.key, l.text); } } private static Dictionary keyDictionary; public static string GetString(string key){ if (keyDictionary == null){ RebuildKeyDictionary(); } if (keyDictionary.ContainsKey(key)) return keyDictionary[key]; else return "E-R-R-O-R"; } [System.Serializable] public struct Local{ public string key; public string text; } public Local[] englishLocals; public Local[] japaneseLocals; public Local[] spanishLocals; public Local[] GetLocals(LocalizationLanguage language){ switch (language) { case LocalizationLanguage.English: return englishLocals; case LocalizationLanguage.Japanese: return japaneseLocals; case LocalizationLanguage.Spanish: return spanishLocals; } return englishLocals; } public Local[] GetLocals() => GetLocals(SelectedLanguage); }