Init.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using CommandLine;
  4. using UnityEngine;
  5. namespace ET
  6. {
  7. public class Init: MonoBehaviour
  8. {
  9. public static Init Instance { get; private set; }
  10. public ThreadSynchronizationContext ThreadSynchronizationContext = new();
  11. public bool IsStart;
  12. public 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<UnityScheduler>();
  28. Process process = Game.Instance.Create();
  29. process.AddSingleton<MainThreadSynchronizationContext>();
  30. process.AddSingleton<GlobalComponent>();
  31. Options.Instance.StartConfig = $"StartConfig/Localhost";
  32. process.AddSingleton<TimeInfo>();
  33. process.AddSingleton<ObjectPool>();
  34. process.AddSingleton<IdGenerater>();
  35. process.AddSingleton<EventSystem>();
  36. process.AddSingleton<TimerComponent>();
  37. process.AddSingleton<CoroutineLockComponent>();
  38. UnityScheduler.Instance.Add(process);
  39. ETTask.ExceptionHandler += Log.Error;
  40. process.AddSingleton<CodeLoader>().Start();
  41. }
  42. private void Update()
  43. {
  44. this.ThreadSynchronizationContext.Update();
  45. if (!this.IsStart)
  46. {
  47. return;
  48. }
  49. this.Process.Update();
  50. }
  51. private void LateUpdate()
  52. {
  53. this.Process.LateUpdate();
  54. this.Process.FrameFinishUpdate();
  55. }
  56. private void OnApplicationQuit()
  57. {
  58. Game.Instance.Dispose();
  59. }
  60. }
  61. }