Init.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using CommandLine;
  3. using UnityEngine;
  4. namespace ET
  5. {
  6. public class Init: MonoBehaviour
  7. {
  8. private void Awake()
  9. {
  10. #if UNITY_EDITOR
  11. var globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  12. if (globalConfig.PlayMode != YooAsset.YooAssets.EPlayMode.EditorSimulateMode)
  13. {
  14. var window = UnityEditor.EditorWindow.GetWindow(typeof(UnityEditor.EditorWindow), true, "Game");
  15. if(window != null)
  16. {
  17. window.ShowNotification(new GUIContent($"PlayMode is {globalConfig.PlayMode}"), 5);
  18. }
  19. return;
  20. }
  21. #endif
  22. DontDestroyOnLoad(gameObject);
  23. //登陆过程限定帧率为30fps
  24. Application.targetFrameRate = (int)FPS.Login;
  25. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  26. {
  27. Log.Error(e.ExceptionObject.ToString());
  28. };
  29. Game.AddSingleton<MainThreadSynchronizationContext>();
  30. // 命令行参数
  31. string[] args = "".Split(" ");
  32. Parser.Default.ParseArguments<Options>(args)
  33. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  34. .WithParsed(Game.AddSingleton);
  35. Game.AddSingleton<TimeInfo>();
  36. Game.AddSingleton<Logger>().ILog = new UnityLogger();
  37. Game.AddSingleton<ObjectPool>();
  38. Game.AddSingleton<IdGenerater>();
  39. Game.AddSingleton<EventSystem>();
  40. Game.AddSingleton<TimerComponent>();
  41. Game.AddSingleton<CoroutineLockComponent>();
  42. ETTask.ExceptionHandler += Log.Error;
  43. Game.AddSingleton<CodeLoader>().Start();
  44. }
  45. private void Update()
  46. {
  47. Game.Update((int)(Time.deltaTime * 1000));
  48. }
  49. private void LateUpdate()
  50. {
  51. Game.LateUpdate();
  52. Game.FrameFinishUpdate();
  53. }
  54. private void OnApplicationQuit()
  55. {
  56. Game.Close();
  57. }
  58. }
  59. }