Launch.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using XGame.Framework.Asset;
  4. namespace XGame.Framework
  5. {
  6. internal class Launch : MonoBehaviour
  7. {
  8. private bool _isDispose = false;
  9. private FrameworkController _controller;
  10. private IGameLogicProxy _gameLogic;
  11. private void Awake()
  12. {
  13. }
  14. private void Start()
  15. {
  16. _controller = new FrameworkController();
  17. _controller.Init(IsDestroy);
  18. _gameLogic = GameLogicCreator.Create();
  19. _gameLogic.Start();
  20. }
  21. private int _elapseUpdate = 0;
  22. private void Update()
  23. {
  24. AppManager.FrameCount++;
  25. try
  26. {
  27. var elapse = _controller.Update();
  28. if (_isDispose)
  29. return;
  30. _elapseUpdate += elapse;
  31. if (OnDemandRendering.willCurrentFrameRender)
  32. {
  33. _gameLogic?.Update(_elapseUpdate);
  34. _elapseUpdate = 0;
  35. }
  36. }
  37. catch (System.Exception ex)
  38. {
  39. Debug.LogException(ex);
  40. }
  41. }
  42. private int _elapseLateUpdate = 0;
  43. private void LateUpdate()
  44. {
  45. if (_isDispose) return;
  46. try
  47. {
  48. var elapse = _controller.LateUpdate();
  49. _elapseLateUpdate += elapse;
  50. if (OnDemandRendering.willCurrentFrameRender)
  51. {
  52. _gameLogic?.LateUpdate(_elapseLateUpdate);
  53. _elapseLateUpdate = 0;
  54. }
  55. }
  56. catch (System.Exception ex)
  57. {
  58. Debug.LogException(ex);
  59. }
  60. }
  61. private void OnDestroy()
  62. {
  63. //Debug.Log($"Launch OnDestroy.");
  64. Dispose();
  65. }
  66. private void OnApplicationQuit()
  67. {
  68. try
  69. {
  70. //Debug.Log($"Launch OnApplicationQuit.");
  71. Dispose();
  72. AppManager.Dispose();
  73. AssetManager.Dispose();
  74. Log.Dispose();
  75. }
  76. catch (System.Exception ex)
  77. {
  78. Debug.LogException(ex);
  79. }
  80. }
  81. internal void Dispose()
  82. {
  83. if (!_isDispose)
  84. {
  85. _isDispose = true;
  86. _gameLogic?.Dispose();
  87. _gameLogic = null;
  88. _controller.Dispose();
  89. _controller = null;
  90. }
  91. }
  92. private bool IsDestroy()
  93. {
  94. return _isDispose;
  95. }
  96. }
  97. }