SLuaComponent.cs 8.1 KB

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