Init.cs 1.7 KB

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