Init.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. Options options = null;
  22. string[] args = "".Split(" ");
  23. Parser.Default.ParseArguments<Options>(args)
  24. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  25. .WithParsed(o => { options = o; });
  26. Game.AddSingleton(options);
  27. Game.AddSingleton<RandomGenerator>();
  28. Game.AddSingleton<TimeInfo>();
  29. Game.AddSingleton<Logger>().ILog = new UnityLogger();
  30. Game.AddSingleton<ObjectPool>();
  31. Game.AddSingleton<IdGenerater>();
  32. Game.AddSingleton<EventSystem>();
  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. Game.Update();
  41. }
  42. private void LateUpdate()
  43. {
  44. Game.LateUpdate();
  45. }
  46. private void OnApplicationQuit()
  47. {
  48. Game.Close();
  49. }
  50. }
  51. }