EnviromentController.cs 3.5 KB

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