Init.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Threading;
  3. using CommandLine;
  4. using UnityEngine;
  5. namespace ET
  6. {
  7. public class Init: MonoBehaviour
  8. {
  9. public static Init Instance;
  10. public GlobalConfig GlobalConfig;
  11. private void Awake()
  12. {
  13. Instance = this;
  14. DontDestroyOnLoad(gameObject);
  15. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  16. {
  17. Log.Error(e.ExceptionObject.ToString());
  18. };
  19. Game.AddSingleton<MainThreadSynchronizationContext>();
  20. // 命令行参数
  21. string[] args = "".Split(" ");
  22. Parser.Default.ParseArguments<Options>(args)
  23. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  24. .WithParsed(Game.AddSingleton);
  25. Game.AddSingleton<TimeInfo>();
  26. Game.AddSingleton<Logger>().ILog = new UnityLogger();
  27. Game.AddSingleton<ObjectPool>();
  28. Game.AddSingleton<IdGenerater>();
  29. Game.AddSingleton<EventSystem>();
  30. Game.AddSingleton<TimerComponent>();
  31. Game.AddSingleton<CoroutineLockComponent>();
  32. ETTask.ExceptionHandler += Log.Error;
  33. Game.AddSingleton<CodeLoader>().Start();
  34. }
  35. private void Update()
  36. {
  37. Game.Update();
  38. }
  39. private void LateUpdate()
  40. {
  41. Game.LateUpdate();
  42. Game.FrameFinishUpdate();
  43. }
  44. private void OnApplicationQuit()
  45. {
  46. Game.Close();
  47. }
  48. }
  49. }