SLuaComponent.cs 9.5 KB

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