Init.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Threading;
  3. using CommandLine;
  4. using Sirenix.OdinInspector;
  5. using UnityEngine;
  6. using YooAsset;
  7. namespace ET
  8. {
  9. public class Init: MonoBehaviour
  10. {
  11. private void Start()
  12. {
  13. DontDestroyOnLoad(gameObject);
  14. #if UNITY_EDITOR
  15. //Editor模式下去掉debug浮窗插件
  16. var debugObj = GameObject.Find("IngameDebugConsole");
  17. if(debugObj != null)
  18. {
  19. GameObject.Destroy(debugObj);
  20. }
  21. #endif
  22. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  23. {
  24. Log.Error(e.ExceptionObject.ToString());
  25. };
  26. Game.AddSingleton<MainThreadSynchronizationContext>();
  27. // 命令行参数
  28. string[] args = "".Split(" ");
  29. Parser.Default.ParseArguments<Options>(args)
  30. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  31. .WithParsed(Game.AddSingleton);
  32. Game.AddSingleton<TimeInfo>();
  33. Game.AddSingleton<Logger>().ILog = new UnityLogger();
  34. Game.AddSingleton<ObjectPool>();
  35. Game.AddSingleton<IdGenerater>();
  36. Game.AddSingleton<EventSystem>();
  37. Game.AddSingleton<TimerComponent>();
  38. Game.AddSingleton<CoroutineLockComponent>();
  39. ETTask.ExceptionHandler += Log.Error;
  40. Game.AddSingleton<CodeLoader>().Start();
  41. }
  42. private void Update()
  43. {
  44. Game.Update();
  45. }
  46. private void LateUpdate()
  47. {
  48. Game.LateUpdate();
  49. Game.FrameFinishUpdate();
  50. }
  51. private void OnApplicationQuit()
  52. {
  53. Game.Close();
  54. }
  55. }
  56. }