LuaLooper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. namespace LuaInterface
  23. {
  24. public class LuaLooper : MonoBehaviour
  25. {
  26. public LuaBeatEvent UpdateEvent
  27. {
  28. get;
  29. private set;
  30. }
  31. public LuaBeatEvent LateUpdateEvent
  32. {
  33. get;
  34. private set;
  35. }
  36. public LuaBeatEvent FixedUpdateEvent
  37. {
  38. get;
  39. private set;
  40. }
  41. public LuaState luaState = null;
  42. void Start()
  43. {
  44. try
  45. {
  46. UpdateEvent = GetEvent("UpdateBeat");
  47. LateUpdateEvent = GetEvent("LateUpdateBeat");
  48. FixedUpdateEvent = GetEvent("FixedUpdateBeat");
  49. }
  50. catch (Exception e)
  51. {
  52. Destroy(this);
  53. throw e;
  54. }
  55. }
  56. LuaBeatEvent GetEvent(string name)
  57. {
  58. LuaTable table = luaState.GetTable(name);
  59. if (table == null)
  60. {
  61. throw new LuaException(string.Format("Lua table {0} not exists", name));
  62. }
  63. LuaBeatEvent e = new LuaBeatEvent(table);
  64. table.Dispose();
  65. table = null;
  66. return e;
  67. }
  68. void ThrowException()
  69. {
  70. string error = luaState.LuaToString(-1);
  71. luaState.LuaPop(2);
  72. throw new LuaException(error, LuaException.GetLastError());
  73. }
  74. void Update()
  75. {
  76. #if UNITY_EDITOR
  77. if (luaState == null)
  78. {
  79. return;
  80. }
  81. #endif
  82. if (luaState.LuaUpdate(Time.deltaTime, Time.unscaledDeltaTime) != 0)
  83. {
  84. ThrowException();
  85. }
  86. luaState.LuaPop(1);
  87. luaState.Collect();
  88. #if UNITY_EDITOR
  89. luaState.CheckTop();
  90. #endif
  91. }
  92. void LateUpdate()
  93. {
  94. #if UNITY_EDITOR
  95. if (luaState == null)
  96. {
  97. return;
  98. }
  99. #endif
  100. if (luaState.LuaLateUpdate() != 0)
  101. {
  102. ThrowException();
  103. }
  104. luaState.StepCollect();
  105. luaState.LuaPop(1);
  106. }
  107. void FixedUpdate()
  108. {
  109. #if UNITY_EDITOR
  110. if (luaState == null)
  111. {
  112. return;
  113. }
  114. #endif
  115. if (luaState.LuaFixedUpdate(Time.fixedDeltaTime) != 0)
  116. {
  117. ThrowException();
  118. }
  119. luaState.LuaPop(1);
  120. }
  121. public void Destroy()
  122. {
  123. if (luaState != null)
  124. {
  125. if (UpdateEvent != null)
  126. {
  127. UpdateEvent.Dispose();
  128. UpdateEvent = null;
  129. }
  130. if (LateUpdateEvent != null)
  131. {
  132. LateUpdateEvent.Dispose();
  133. LateUpdateEvent = null;
  134. }
  135. if (FixedUpdateEvent != null)
  136. {
  137. FixedUpdateEvent.Dispose();
  138. FixedUpdateEvent = null;
  139. }
  140. luaState = null;
  141. }
  142. }
  143. void OnDestroy()
  144. {
  145. if (luaState != null)
  146. {
  147. Destroy();
  148. }
  149. }
  150. }
  151. }