Entrance.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using UnityEngine;
  3. using XGame.Framework.Asset;
  4. using XGame.Framework.Utils;
  5. namespace XGame.Framework
  6. {
  7. /// <summary>
  8. /// 入口
  9. /// </summary>
  10. internal static class Entrance
  11. {
  12. /// <summary>
  13. /// 程序入口
  14. /// </summary>
  15. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
  16. internal static void Access()
  17. {
  18. Profiler.BeginSample(typeof(Entrance), "Log.Initialize");
  19. Log.Initialize();
  20. Profiler.EndSample();
  21. var async = AssetManager.Init();
  22. async.On(_ =>
  23. {
  24. IsInvalid(errorCode =>
  25. {
  26. if (errorCode != 0)
  27. {
  28. return;
  29. }
  30. Profiler.BeginSample(typeof(Entrance), "AppManager.Initialize");
  31. AppManager.Initialize();
  32. Profiler.EndSample();
  33. });
  34. });
  35. }
  36. private static void IsInvalid(Action<int> callback)
  37. {
  38. var loadAsync = XGameConfig.LoadAsync();
  39. loadAsync.On(_ =>
  40. {
  41. var config = loadAsync.Result;
  42. if (config == null)
  43. {
  44. Log.Error($"Entrance error: 找不到 XGameConfig.asset.");
  45. callback(-1);
  46. return;
  47. }
  48. AppManager.IsSessionDebug = config.isSessionDebug;
  49. var launchSceneName = config.launchSceneName;
  50. var gameLogicTypeName = config.gameLogicTypeName;
  51. XGameConfig.Unload(config);
  52. if (string.IsNullOrEmpty(launchSceneName))
  53. {
  54. Log.Error($"Entrance error: XGameConfig.asset 没有设置启动场景.");
  55. callback(-2);
  56. return;
  57. }
  58. if (!UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.Equals(launchSceneName))
  59. {
  60. Log.Error($"Entrance error: XGameConfig.asset 启动场景没有激活.");
  61. callback(-3);
  62. return;
  63. }
  64. if (string.IsNullOrEmpty(gameLogicTypeName))
  65. {
  66. Log.Error($"Entrance error: XGameConfig.asset 没有设置启动脚本.");
  67. callback(-4);
  68. return;
  69. }
  70. if (Type.GetType(gameLogicTypeName) == null)
  71. {
  72. Log.Error($"Entrance error: XGameConfig.asset 找不到启动脚本. Type:{gameLogicTypeName}");
  73. callback(-5);
  74. return;
  75. }
  76. AppManager.GameLogicTypeName = gameLogicTypeName;
  77. //var assembly = System.Reflection.Assembly.Load(config.launchAssemblyName);
  78. //assembly.GetType(gameLogicTypeName);
  79. callback(0);
  80. });
  81. }
  82. }
  83. }