SLuaComponent.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SFramework
  5. {
  6. [XLua.LuaCallCSharp]
  7. public class SLuaComponent : MonoBehaviour
  8. {
  9. public TextAsset luaScript;
  10. private XLua.LuaTable _luaTable;
  11. private XLua.LuaFunction _luaAwake;
  12. private XLua.LuaFunction _luaStart;
  13. private XLua.LuaFunction _luaUpdate;
  14. private XLua.LuaFunction _luaFixedUpdate;
  15. private XLua.LuaFunction _luaLateUpdate;
  16. private XLua.LuaFunction _luaOnDestroy;
  17. private XLua.LuaFunction _luaOnEnable;
  18. private XLua.LuaFunction _luaOnDisable;
  19. private XLua.LuaFunction _luaOnApplicationFocus;
  20. private XLua.LuaFunction _luaOnApplicationPause;
  21. private XLua.LuaFunction _luaOnApplicationQuit;
  22. internal static XLua.LuaEnv luaEnv = SLuaEnv.Instance;
  23. internal static float lastGCTime = 0;
  24. internal const float GCInterval = 1;
  25. public static void Add(GameObject targetGameObject, XLua.LuaTable luaTable, string luaFileName)
  26. {
  27. SLuaComponent luaComponent = targetGameObject.AddComponent<SLuaComponent>();
  28. luaComponent.luaScript = new UnityEngine.TextAsset("print('The lua script add dynamic.')");
  29. luaComponent.luaScript.name = luaFileName;
  30. if (null == luaFileName) { Debug.LogWarning("SLuaComponent Add Script Dynamic But Do Not Set Script Name!"); }
  31. luaComponent.SetLuaTable(luaTable);
  32. }
  33. public void SetLuaTable(XLua.LuaTable luaTable)
  34. {
  35. Debug.Assert(null != luaTable, "SLuaComponent SetLuaTable Failed luaTable == null");
  36. Debug.Assert(null == _luaTable, "SLuaComponent SetLuaTable Failed _luaTable != null");
  37. _luaTable = luaTable;
  38. InitLuaFunction();
  39. // Try Call Awake Function();
  40. if (null != _luaAwake)
  41. _luaAwake.Call(gameObject);
  42. }
  43. private void InitLuaFunction()
  44. {
  45. if (null == _luaTable)
  46. {
  47. Debug.LogError("SLuaComponent InitLuaFunction Failed Because _luaTable == null");
  48. return;
  49. }
  50. _luaAwake = _luaTable.Get<XLua.LuaFunction>("Awake");
  51. _luaStart = _luaTable.Get<XLua.LuaFunction>("Start");
  52. _luaUpdate = _luaTable.Get<XLua.LuaFunction>("Update");
  53. _luaFixedUpdate = _luaTable.Get<XLua.LuaFunction>("FixedUpdate");
  54. _luaLateUpdate = _luaTable.Get<XLua.LuaFunction>("LateUpdate");
  55. _luaOnDestroy = _luaTable.Get<XLua.LuaFunction>("OnDestroy");
  56. _luaOnEnable = _luaTable.Get<XLua.LuaFunction>("OnEnable");
  57. _luaOnDisable = _luaTable.Get<XLua.LuaFunction>("OnDisable");
  58. _luaOnApplicationFocus = _luaTable.Get<XLua.LuaFunction>("OnApplicationFocus");
  59. _luaOnApplicationPause = _luaTable.Get<XLua.LuaFunction>("OnApplicationPause");
  60. _luaOnApplicationQuit = _luaTable.Get<XLua.LuaFunction>("OnApplicationQuit");
  61. }
  62. private void Awake()
  63. {
  64. if (null == luaScript)
  65. return;
  66. System.Object[] retValues = luaEnv.DoString(luaScript.text, "LuaComponent");
  67. if (null == retValues || retValues.Length != 1)
  68. {
  69. Debug.LogWarning("SLuaCompnent Need A Lua That Return A LuaTable.");
  70. Debug.LogError("SLuaComponent Load LuaScript Failed When Awake, ScriptName: " + luaScript.name);
  71. return;
  72. }
  73. _luaTable = retValues[0] as XLua.LuaTable;
  74. InitLuaFunction();
  75. if (null != _luaAwake)
  76. _luaAwake.Call(gameObject);
  77. }
  78. private void Start()
  79. {
  80. if (null != _luaStart)
  81. _luaStart.Call(gameObject);
  82. }
  83. private void Update()
  84. {
  85. if (Time.time - SLuaComponent.lastGCTime > GCInterval)
  86. {
  87. luaEnv.Tick();
  88. SLuaComponent.lastGCTime = Time.time;
  89. }
  90. if (null != _luaUpdate)
  91. _luaUpdate.Call();
  92. }
  93. private void FixedUpdate()
  94. {
  95. if (null != _luaFixedUpdate)
  96. _luaFixedUpdate.Call();
  97. }
  98. private void LateUpdate()
  99. {
  100. if (null != _luaLateUpdate)
  101. _luaLateUpdate.Call();
  102. }
  103. private void OnDestroy()
  104. {
  105. if (null != _luaOnDestroy)
  106. _luaOnDestroy.Call();
  107. if (null != _luaAwake)
  108. {
  109. _luaAwake.Dispose();
  110. _luaAwake = null;
  111. }
  112. if (null != _luaStart)
  113. {
  114. _luaStart.Dispose();
  115. _luaStart = null;
  116. }
  117. if (null != _luaUpdate)
  118. {
  119. _luaUpdate.Dispose();
  120. _luaUpdate = null;
  121. }
  122. if (null != _luaFixedUpdate)
  123. {
  124. _luaFixedUpdate.Dispose();
  125. _luaFixedUpdate = null;
  126. }
  127. if (null != _luaLateUpdate)
  128. {
  129. _luaLateUpdate.Dispose();
  130. _luaLateUpdate = null;
  131. }
  132. if (null != _luaOnDestroy)
  133. {
  134. _luaOnDestroy.Dispose();
  135. _luaOnDestroy = null;
  136. }
  137. if (null != _luaOnEnable)
  138. {
  139. _luaOnEnable.Dispose();
  140. _luaOnEnable = null;
  141. }
  142. if (null != _luaOnDisable)
  143. {
  144. _luaOnDisable.Dispose();
  145. _luaOnDisable = null;
  146. }
  147. if (null != _luaOnApplicationFocus)
  148. {
  149. _luaOnApplicationFocus.Dispose();
  150. _luaOnApplicationFocus = null;
  151. }
  152. if (null != _luaOnApplicationPause)
  153. {
  154. _luaOnApplicationPause.Dispose();
  155. _luaOnApplicationPause = null;
  156. }
  157. if (null != _luaOnApplicationQuit)
  158. {
  159. _luaOnApplicationQuit.Dispose();
  160. _luaOnApplicationQuit = null;
  161. }
  162. }
  163. private void OnEnable()
  164. {
  165. if (null != _luaOnEnable)
  166. _luaOnEnable.Call();
  167. }
  168. private void OnDisable()
  169. {
  170. if (null != _luaOnDisable)
  171. _luaOnDisable.Call();
  172. }
  173. private void OnApplicationFocus(bool focus)
  174. {
  175. if (null != _luaOnApplicationFocus)
  176. _luaOnApplicationFocus.Call(focus);
  177. }
  178. private void OnApplicationPause(bool pause)
  179. {
  180. if (null != _luaOnApplicationPause)
  181. _luaOnApplicationPause.Call(pause);
  182. }
  183. private void OnApplicationQuit()
  184. {
  185. if (null != _luaOnApplicationQuit)
  186. _luaOnApplicationQuit.Call();
  187. }
  188. }
  189. }