Launch.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. Dispose();
  64. }
  65. private void OnApplicationQuit()
  66. {
  67. try
  68. {
  69. Dispose();
  70. AssetManager.Dispose();
  71. Log.Dispose();
  72. }
  73. catch (System.Exception ex)
  74. {
  75. Debug.LogException(ex);
  76. }
  77. }
  78. internal void Dispose()
  79. {
  80. if (!_isDispose)
  81. {
  82. _isDispose = true;
  83. _gameLogic?.Dispose();
  84. _gameLogic = null;
  85. _controller.Dispose();
  86. _controller = null;
  87. }
  88. }
  89. private bool IsDestroy()
  90. {
  91. return _isDispose;
  92. }
  93. }
  94. }