EnviromentController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Newtonsoft.Json;
  5. using System.IO;
  6. using Unity.VisualScripting.FullSerializer;
  7. /* EnviromentController类主要功能是操作EnviromentSetting类,包含初始化数据,设置语言等
  8. * 必须挂载在第一个场景的Camera上
  9. */
  10. public class EnviromentController : MonoBehaviour
  11. {
  12. // Start is called before the first frame update
  13. //private string logFilePath;
  14. void Awake()
  15. {
  16. InitialGameEnviroment();
  17. // 启动log功能
  18. //logFilePath = Path.Combine(Application.persistentDataPath, "game_log.txt");
  19. //Application.logMessageReceived += LogMessage;
  20. //Debug.Log("日志系统已启动,日志将保存到: " + logFilePath);
  21. }
  22. void Start()
  23. {
  24. // todo 开发代码,加载狗的信息,以后要移到ProgressBar页面
  25. //DogProperty puppy_1 = new();
  26. //UserProperty.dogs.Add(puppy_1);
  27. }
  28. public static void InitialGameEnviroment()
  29. {
  30. // 读取language.json
  31. string filePath = EnviromentSetting.langFilePath;
  32. //string jsonText = File.ReadAllText(filePath);
  33. // 正确的方式(跨平台工作)
  34. string jsonText = string.Empty;
  35. TextAsset textAsset = Resources.Load<TextAsset>(filePath);
  36. if (textAsset != null)
  37. {
  38. jsonText = textAsset.text;
  39. // 处理JSON数据
  40. }
  41. else
  42. {
  43. Debug.LogError("无法加载语言文件!");
  44. }
  45. EnviromentSetting.languageData = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonText);
  46. // 读取系统语言
  47. string systemLanguage = Application.systemLanguage.ToString();
  48. if (systemLanguage == "ChineseSimplified")
  49. {
  50. EnviromentSetting.languageCode = "zh-cn";
  51. }
  52. // 默认语言为en
  53. // 读取操作系统,unique Id,版本
  54. EnviromentSetting.platform = Application.platform.ToString();
  55. EnviromentSetting.UUID = SystemInfo.deviceUniqueIdentifier;
  56. EnviromentSetting.version = Application.version.ToString();
  57. // 读取Dogbreeds 数据
  58. DogBreedController.LoadDogBreed();
  59. }
  60. //void LogMessage(string condition, string stackTrace, LogType type)
  61. //{
  62. // string logEntry = $"[{System.DateTime.Now}] [{type}] {condition}\n{stackTrace}\n";
  63. // File.AppendAllText(logFilePath, logEntry);
  64. //}
  65. void OnDestroy()
  66. {
  67. // 关闭log系统
  68. //Application.logMessageReceived -= LogMessage;
  69. }
  70. void PlayerPrefsLoader()
  71. {
  72. if (PlayerPrefs.HasKey("lastTrainingDate"))
  73. {
  74. // 读取存储的日期
  75. string lastTrainingDate = PlayerPrefs.GetString("lastTrainingDate");
  76. string today = System.DateTime.Now.ToString("yyyy-MM-dd");
  77. if (lastTrainingDate != today)
  78. {
  79. // 如果不是同一天,执行相应的操作
  80. GameData.isVoiceTrainingToday = false;
  81. }
  82. else
  83. {
  84. GameData.isVoiceTrainingToday = true;
  85. }
  86. }
  87. }
  88. // Update is called once per frame
  89. //void Update()
  90. //{
  91. //}
  92. }