Init.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. SynchronizationContext.SetSynchronizationContext(ThreadSynchronizationContext.Instance);
  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<RandomGenerator>();
  26. Game.AddSingleton<TimeInfo>();
  27. Game.AddSingleton<Logger>().ILog = new UnityLogger();
  28. Game.AddSingleton<ObjectPool>();
  29. Game.AddSingleton<IdGenerater>();
  30. Game.AddSingleton<EventSystem>();
  31. Game.AddSingleton<TimerComponent>();
  32. Game.AddSingleton<CoroutineLockComponent>();
  33. Game.AddSingleton<NetServices>();
  34. Game.AddSingleton<Root>();
  35. ETTask.ExceptionHandler += Log.Error;
  36. Game.AddSingleton<CodeLoader>().Start();
  37. }
  38. private void Update()
  39. {
  40. ThreadSynchronizationContext.Instance.Update();
  41. Game.Update();
  42. }
  43. private void LateUpdate()
  44. {
  45. Game.LateUpdate();
  46. Game.FrameFinishUpdate();
  47. }
  48. private void OnApplicationQuit()
  49. {
  50. Game.Close();
  51. }
  52. }
  53. }