SafetyAreaLanguageManager.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using SC.XR.Unity;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using System;
  6. using System.Linq;
  7. public class SafetyAreaLanguageManager : Singleton<SafetyAreaLanguageManager>
  8. {
  9. public Action onLanguageChange;
  10. private const string SAFETY_AREA_LANGUAGE_SAVE_KEY = "SAFETY_AREA_LANGUAGE_SAVE_KEY";
  11. private const string LANGUAGE_FILE_PATH = "Localization/Language";
  12. private SafetyAreaLanguageList safetyAreaLanguageList;
  13. private SafetyAreaLanguageEnum currentLanguageEnum = SafetyAreaLanguageEnum.Chinese;
  14. public void Init()
  15. {
  16. TextAsset languageText = Resources.Load<TextAsset>(LANGUAGE_FILE_PATH);
  17. safetyAreaLanguageList = JsonUtility.FromJson<SafetyAreaLanguageList>(languageText.text);
  18. LoadLanguage();
  19. }
  20. public void ChangeLanguage(SafetyAreaLanguageEnum safetyAreaLanguageEnum)
  21. {
  22. currentLanguageEnum = safetyAreaLanguageEnum;
  23. PlayerPrefs.SetInt(SAFETY_AREA_LANGUAGE_SAVE_KEY, (int)currentLanguageEnum);
  24. PlayerPrefs.Save();
  25. onLanguageChange?.Invoke();
  26. }
  27. private void LoadLanguage()
  28. {
  29. currentLanguageEnum = (SafetyAreaLanguageEnum)PlayerPrefs.GetInt(SAFETY_AREA_LANGUAGE_SAVE_KEY, 0);
  30. }
  31. public string GetWord(int id)
  32. {
  33. List<SafetyAreaLanguageItem> languageItemList = null;
  34. switch (currentLanguageEnum)
  35. {
  36. case SafetyAreaLanguageEnum.Chinese:
  37. languageItemList = safetyAreaLanguageList.chinese;
  38. break;
  39. case SafetyAreaLanguageEnum.English:
  40. languageItemList = safetyAreaLanguageList.english;
  41. break;
  42. }
  43. if (languageItemList == null)
  44. {
  45. Debug.LogError("GetWord languageItemList == null");
  46. return string.Empty;
  47. }
  48. SafetyAreaLanguageItem safetyAreaLanguageItem = languageItemList.Where((item) => item.id == id).FirstOrDefault();
  49. if (safetyAreaLanguageItem == null)
  50. {
  51. Debug.LogError("GetWord safetyAreaLanguageItem == null id:" + id + " not exist");
  52. return string.Empty;
  53. }
  54. return safetyAreaLanguageItem.value;
  55. }
  56. }