EnviromentController.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. public class EnviromentController : MonoBehaviour
  9. {
  10. // Start is called before the first frame update
  11. //private string logFilePath;
  12. void Awake()
  13. {
  14. InitialGameEnviroment();
  15. // 启动log功能
  16. //logFilePath = Path.Combine(Application.persistentDataPath, "game_log.txt");
  17. //Application.logMessageReceived += LogMessage;
  18. //Debug.Log("日志系统已启动,日志将保存到: " + logFilePath);
  19. }
  20. void Start()
  21. {
  22. // todo 开发代码,加载狗的信息,以后要移到ProgressBar页面
  23. //DogProperty puppy_1 = new();
  24. //UserProperty.dogs.Add(puppy_1);
  25. }
  26. public static void InitialGameEnviroment()
  27. {
  28. // 读取language.json
  29. string filePath = EnviromentSetting.langFilePath;
  30. //string jsonText = File.ReadAllText(filePath);
  31. // 正确的方式(跨平台工作)
  32. string jsonText = string.Empty;
  33. TextAsset textAsset = Resources.Load<TextAsset>(filePath);
  34. if (textAsset != null)
  35. {
  36. jsonText = textAsset.text;
  37. // 处理JSON数据
  38. }
  39. else
  40. {
  41. Debug.LogError("无法加载语言文件!");
  42. }
  43. EnviromentSetting.languageData = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonText);
  44. // 读取系统语言
  45. string systemLanguage = Application.systemLanguage.ToString();
  46. if (systemLanguage == "ChineseSimplified")
  47. {
  48. EnviromentSetting.languageCode = "zh-cn";
  49. }
  50. // 默认语言为en
  51. // 读取操作系统,unique Id,版本
  52. EnviromentSetting.platform = Application.platform.ToString();
  53. EnviromentSetting.UUID = SystemInfo.deviceUniqueIdentifier;
  54. EnviromentSetting.version = Application.version.ToString();
  55. // 读取Dogbreeds 数据
  56. DogBreedController.LoadDogBreed();
  57. }
  58. //void LogMessage(string condition, string stackTrace, LogType type)
  59. //{
  60. // string logEntry = $"[{System.DateTime.Now}] [{type}] {condition}\n{stackTrace}\n";
  61. // File.AppendAllText(logFilePath, logEntry);
  62. //}
  63. void OnDestroy()
  64. {
  65. // 关闭log系统
  66. //Application.logMessageReceived -= LogMessage;
  67. }
  68. // Update is called once per frame
  69. //void Update()
  70. //{
  71. //}
  72. }