Init.cs 1.4 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. private Process process;
  10. private void Start()
  11. {
  12. DontDestroyOnLoad(gameObject);
  13. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  14. {
  15. Log.Error(e.ExceptionObject.ToString());
  16. };
  17. // 命令行参数
  18. string[] args = "".Split(" ");
  19. Parser.Default.ParseArguments<Options>(args)
  20. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  21. .WithParsed(Game.Instance.AddSingleton);
  22. process = Game.Instance.Create(false);
  23. process.AddSingleton<MainThreadSynchronizationContext>();
  24. process.AddSingleton<GlobalComponent>();
  25. Options.Instance.StartConfig = $"StartConfig/Localhost";
  26. process.AddSingleton<TimeInfo>();
  27. process.AddSingleton<Logger>().ILog = new UnityLogger();
  28. process.AddSingleton<ObjectPool>();
  29. process.AddSingleton<IdGenerater>();
  30. process.AddSingleton<EventSystem>();
  31. process.AddSingleton<TimerComponent>();
  32. process.AddSingleton<CoroutineLockComponent>();
  33. ETTask.ExceptionHandler += Log.Error;
  34. process.AddSingleton<CodeLoader>().Start();
  35. }
  36. private void Update()
  37. {
  38. process.Update();
  39. }
  40. private void LateUpdate()
  41. {
  42. process.LateUpdate();
  43. process.FrameFinishUpdate();
  44. }
  45. private void OnApplicationQuit()
  46. {
  47. Game.Instance.Remove(this.process.Id);
  48. }
  49. }
  50. }