EnviromentController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. PlayerPrefsLoader();
  26. }
  27. public static void InitialGameEnviroment()
  28. {
  29. // 读取language.json
  30. string filePath = EnviromentSetting.langFilePath;
  31. //string jsonText = File.ReadAllText(filePath);
  32. // 正确的方式(跨平台工作)
  33. string jsonText = string.Empty;
  34. TextAsset textAsset = Resources.Load<TextAsset>(filePath);
  35. if (textAsset != null)
  36. {
  37. jsonText = textAsset.text;
  38. // 处理JSON数据
  39. }
  40. else
  41. {
  42. Debug.LogError("无法加载语言文件!");
  43. }
  44. EnviromentSetting.languageData = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonText);
  45. // 读取系统语言
  46. string systemLanguage = Application.systemLanguage.ToString();
  47. if (systemLanguage == "ChineseSimplified")
  48. {
  49. EnviromentSetting.languageCode = "zh-cn";
  50. }
  51. // 默认语言为en
  52. // 读取操作系统,unique Id,版本
  53. EnviromentSetting.platform = Application.platform.ToString();
  54. EnviromentSetting.UUID = SystemInfo.deviceUniqueIdentifier;
  55. EnviromentSetting.version = Application.version.ToString();
  56. // 读取Dogbreeds 数据
  57. DogBreedController.LoadDogBreed();
  58. }
  59. //void LogMessage(string condition, string stackTrace, LogType type)
  60. //{
  61. // string logEntry = $"[{System.DateTime.Now}] [{type}] {condition}\n{stackTrace}\n";
  62. // File.AppendAllText(logFilePath, logEntry);
  63. //}
  64. void OnDestroy()
  65. {
  66. // 关闭log系统
  67. //Application.logMessageReceived -= LogMessage;
  68. }
  69. void PlayerPrefsLoader()
  70. {
  71. if (PlayerPrefs.HasKey("lastTrainingDate"))
  72. {
  73. // 读取存储的日期
  74. string lastTrainingDate = PlayerPrefs.GetString("lastTrainingDate");
  75. string today = System.DateTime.Now.ToString("yyyy-MM-dd");
  76. if (lastTrainingDate != today)
  77. {
  78. // 如果不是同一天,执行相应的操作
  79. GameData.isVoiceTrainingToday = false;
  80. }
  81. else
  82. {
  83. GameData.isVoiceTrainingToday = true;
  84. }
  85. }
  86. if (PlayerPrefs.HasKey("lastFeedbackTime"))
  87. {
  88. // 读取存储的日期
  89. string lastFeedbackTime = PlayerPrefs.GetString("lastFeedbackTime");
  90. GameData.lastFeedbackTime = DateTime.Parse(lastFeedbackTime);
  91. }
  92. }
  93. // Update is called once per frame
  94. //void Update()
  95. //{
  96. //}
  97. }