Init.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using Google.Protobuf;
  5. using UnityEngine;
  6. namespace ETModel
  7. {
  8. public class Init : MonoBehaviour
  9. {
  10. private void Start()
  11. {
  12. this.StartAsync().NoAwait();
  13. }
  14. private async ETVoid StartAsync()
  15. {
  16. try
  17. {
  18. if (!Application.unityVersion.StartsWith("2017.4"))
  19. {
  20. Log.Error($"新人请使用Unity2017.4版本,减少跑demo遇到的问题! 下载地址:\n https://unity3d.com/cn/unity/qa/lts-releases?_ga=2.227583646.282345691.1536717255-1119432033.1499739574");
  21. }
  22. SynchronizationContext.SetSynchronizationContext(OneThreadSynchronizationContext.Instance);
  23. DontDestroyOnLoad(gameObject);
  24. Game.EventSystem.Add(DLLType.Model, typeof(Init).Assembly);
  25. Game.Scene.AddComponent<GlobalConfigComponent>();
  26. Game.Scene.AddComponent<NetOuterComponent>();
  27. Game.Scene.AddComponent<ResourcesComponent>();
  28. Game.Scene.AddComponent<PlayerComponent>();
  29. Game.Scene.AddComponent<UnitComponent>();
  30. Game.Scene.AddComponent<UIComponent>();
  31. // 下载ab包
  32. await BundleHelper.DownloadBundle();
  33. Game.Hotfix.LoadHotfixAssembly();
  34. // 加载配置
  35. Game.Scene.GetComponent<ResourcesComponent>().LoadBundle("config.unity3d");
  36. Game.Scene.AddComponent<ConfigComponent>();
  37. Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle("config.unity3d");
  38. Game.Scene.AddComponent<OpcodeTypeComponent>();
  39. Game.Scene.AddComponent<MessageDispatherComponent>();
  40. Game.Hotfix.GotoHotfix();
  41. Game.EventSystem.Run(EventIdType.TestHotfixSubscribMonoEvent, "TestHotfixSubscribMonoEvent");
  42. }
  43. catch (Exception e)
  44. {
  45. Log.Error(e);
  46. }
  47. }
  48. private void Update()
  49. {
  50. OneThreadSynchronizationContext.Instance.Update();
  51. Game.Hotfix.Update?.Invoke();
  52. Game.EventSystem.Update();
  53. }
  54. private void LateUpdate()
  55. {
  56. Game.Hotfix.LateUpdate?.Invoke();
  57. Game.EventSystem.LateUpdate();
  58. }
  59. private void OnApplicationQuit()
  60. {
  61. Game.Hotfix.OnApplicationQuit?.Invoke();
  62. Game.Close();
  63. }
  64. }
  65. }