LuaLooper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Copyright (c) 2015-2017 topameng(topameng@qq.com)
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. using System;
  20. using UnityEngine;
  21. using LuaInterface;
  22. public class LuaLooper : MonoBehaviour
  23. {
  24. public LuaBeatEvent UpdateEvent
  25. {
  26. get;
  27. private set;
  28. }
  29. public LuaBeatEvent LateUpdateEvent
  30. {
  31. get;
  32. private set;
  33. }
  34. public LuaBeatEvent FixedUpdateEvent
  35. {
  36. get;
  37. private set;
  38. }
  39. public LuaState luaState = null;
  40. void Start()
  41. {
  42. try
  43. {
  44. UpdateEvent = GetEvent("UpdateBeat");
  45. LateUpdateEvent = GetEvent("LateUpdateBeat");
  46. FixedUpdateEvent = GetEvent("FixedUpdateBeat");
  47. }
  48. catch (Exception e)
  49. {
  50. Destroy(this);
  51. throw e;
  52. }
  53. }
  54. LuaBeatEvent GetEvent(string name)
  55. {
  56. LuaTable table = luaState.GetTable(name);
  57. if (table == null)
  58. {
  59. throw new LuaException(string.Format("Lua table {0} not exists", name));
  60. }
  61. LuaBeatEvent e = new LuaBeatEvent(table);
  62. table.Dispose();
  63. table = null;
  64. return e;
  65. }
  66. void ThrowException()
  67. {
  68. string error = luaState.LuaToString(-1);
  69. luaState.LuaPop(2);
  70. throw new LuaException(error, LuaException.GetLastError());
  71. }
  72. void Update()
  73. {
  74. #if UNITY_EDITOR
  75. if (luaState == null)
  76. {
  77. return;
  78. }
  79. #endif
  80. if (luaState.LuaUpdate(Time.deltaTime, Time.unscaledDeltaTime) != 0)
  81. {
  82. ThrowException();
  83. }
  84. luaState.LuaPop(1);
  85. luaState.Collect();
  86. #if UNITY_EDITOR
  87. luaState.CheckTop();
  88. #endif
  89. }
  90. void LateUpdate()
  91. {
  92. #if UNITY_EDITOR
  93. if (luaState == null)
  94. {
  95. return;
  96. }
  97. #endif
  98. if (luaState.LuaLateUpdate() != 0)
  99. {
  100. ThrowException();
  101. }
  102. luaState.StepCollect();
  103. luaState.LuaPop(1);
  104. }
  105. void FixedUpdate()
  106. {
  107. #if UNITY_EDITOR
  108. if (luaState == null)
  109. {
  110. return;
  111. }
  112. #endif
  113. if (luaState.LuaFixedUpdate(Time.fixedDeltaTime) != 0)
  114. {
  115. ThrowException();
  116. }
  117. luaState.LuaPop(1);
  118. }
  119. public void Destroy()
  120. {
  121. if (luaState != null)
  122. {
  123. if (UpdateEvent != null)
  124. {
  125. UpdateEvent.Dispose();
  126. UpdateEvent = null;
  127. }
  128. if (LateUpdateEvent != null)
  129. {
  130. LateUpdateEvent.Dispose();
  131. LateUpdateEvent = null;
  132. }
  133. if (FixedUpdateEvent != null)
  134. {
  135. FixedUpdateEvent.Dispose();
  136. FixedUpdateEvent = null;
  137. }
  138. luaState = null;
  139. }
  140. }
  141. void OnDestroy()
  142. {
  143. if (luaState != null)
  144. {
  145. Destroy();
  146. }
  147. }
  148. }