| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace SFramework
- {
- [XLua.LuaCallCSharp]
- public class SLuaComponent : MonoBehaviour
- {
- public TextAsset luaScript;
- private XLua.LuaTable _luaTable;
- private XLua.LuaFunction _luaAwake;
- private XLua.LuaFunction _luaStart;
- private XLua.LuaFunction _luaUpdate;
- private XLua.LuaFunction _luaFixedUpdate;
- private XLua.LuaFunction _luaLateUpdate;
- private XLua.LuaFunction _luaOnDestroy;
- private XLua.LuaFunction _luaOnEnable;
- private XLua.LuaFunction _luaOnDisable;
- private XLua.LuaFunction _luaOnMouseUp;
- private XLua.LuaFunction _luaOnMouseDown;
- private XLua.LuaFunction _luaOnMouseDrag;
- private XLua.LuaFunction _luaOnApplicationFocus;
- private XLua.LuaFunction _luaOnApplicationPause;
- private XLua.LuaFunction _luaOnApplicationQuit;
- internal static XLua.LuaEnv luaEnv = SLuaEnv.Instance;
- internal static float lastGCTime = 0;
- internal const float GCInterval = 1;
- public static void Add(GameObject targetGameObject, XLua.LuaTable luaTable, string luaFileName)
- {
- SLuaComponent luaComponent = targetGameObject.AddComponent<SLuaComponent>();
- luaComponent.luaScript = new UnityEngine.TextAsset("print('The lua script add dynamic.')");
- luaComponent.luaScript.name = luaFileName;
- if (null == luaFileName) { Debug.LogWarning("SLuaComponent Add Script Dynamic But Do Not Set Script Name!"); }
- luaComponent.SetLuaTable(luaTable);
- }
- public void SetLuaTable(XLua.LuaTable luaTable)
- {
- Debug.Assert(null != luaTable, "SLuaComponent SetLuaTable Failed luaTable == null");
- Debug.Assert(null == _luaTable, "SLuaComponent SetLuaTable Failed _luaTable != null");
- _luaTable = luaTable;
- InitLuaFunction();
- // Try Call Awake Function();
- if (null != _luaAwake)
- _luaAwake.Call(gameObject);
- }
- private void InitLuaFunction()
- {
- if (null == _luaTable)
- {
- Debug.LogError("SLuaComponent InitLuaFunction Failed Because _luaTable == null");
- return;
- }
- _luaAwake = _luaTable.Get<XLua.LuaFunction>("Awake");
- _luaStart = _luaTable.Get<XLua.LuaFunction>("Start");
- _luaUpdate = _luaTable.Get<XLua.LuaFunction>("Update");
- _luaFixedUpdate = _luaTable.Get<XLua.LuaFunction>("FixedUpdate");
- _luaLateUpdate = _luaTable.Get<XLua.LuaFunction>("LateUpdate");
- _luaOnMouseUp = _luaTable.Get<XLua.LuaFunction>("OnMouseUp");
- _luaOnMouseDown = _luaTable.Get<XLua.LuaFunction>("OnMouseDown");
- _luaOnMouseDrag = _luaTable.Get<XLua.LuaFunction>("OnMouseDrag");
- _luaOnDestroy = _luaTable.Get<XLua.LuaFunction>("OnDestroy");
- _luaOnEnable = _luaTable.Get<XLua.LuaFunction>("OnEnable");
- _luaOnDisable = _luaTable.Get<XLua.LuaFunction>("OnDisable");
- _luaOnApplicationFocus = _luaTable.Get<XLua.LuaFunction>("OnApplicationFocus");
- _luaOnApplicationPause = _luaTable.Get<XLua.LuaFunction>("OnApplicationPause");
- _luaOnApplicationQuit = _luaTable.Get<XLua.LuaFunction>("OnApplicationQuit");
- }
- private void Awake()
- {
- if (null == luaScript)
- return;
- System.Object[] retValues = luaEnv.DoString(luaScript.text, "LuaComponent");
- if (null == retValues || retValues.Length != 1)
- {
- Debug.LogWarning("SLuaCompnent Need A Lua That Return A LuaTable.");
- Debug.LogError("SLuaComponent Load LuaScript Failed When Awake, ScriptName: " + luaScript.name);
- return;
- }
- _luaTable = retValues[0] as XLua.LuaTable;
- InitLuaFunction();
- if (null != _luaAwake)
- _luaAwake.Call(gameObject);
- }
- private void Start()
- {
- if (null != _luaStart)
- _luaStart.Call(gameObject);
- }
- private void Update()
- {
- if (Time.time - SLuaComponent.lastGCTime > GCInterval)
- {
- luaEnv.Tick();
- SLuaComponent.lastGCTime = Time.time;
- }
- if (null != _luaUpdate)
- _luaUpdate.Call();
- }
- private void FixedUpdate()
- {
- if (null != _luaFixedUpdate)
- _luaFixedUpdate.Call();
- }
- private void LateUpdate()
- {
- if (null != _luaLateUpdate)
- _luaLateUpdate.Call();
- }
-
- private void OnMouseUp()
- {
- if (null != _luaOnMouseUp)
- _luaOnMouseUp.Call();
- }
-
- private void OnMouseDown()
- {
- if (null != _luaOnMouseDown)
- _luaOnMouseDown.Call();
- }
-
- private void OnMouseDrag()
- {
- if (null != _luaOnMouseDrag)
- _luaOnMouseDrag.Call();
- }
- private void OnDestroy()
- {
- if (null != _luaOnDestroy)
- _luaOnDestroy.Call();
- if (null != _luaAwake)
- {
- _luaAwake.Dispose();
- _luaAwake = null;
- }
-
- if (null != _luaStart)
- {
- _luaStart.Dispose();
- _luaStart = null;
- }
- if (null != _luaUpdate)
- {
- _luaUpdate.Dispose();
- _luaUpdate = null;
- }
- if (null != _luaFixedUpdate)
- {
- _luaFixedUpdate.Dispose();
- _luaFixedUpdate = null;
- }
- if (null != _luaLateUpdate)
- {
- _luaLateUpdate.Dispose();
- _luaLateUpdate = null;
- }
- if (null != _luaOnDestroy)
- {
- _luaOnDestroy.Dispose();
- _luaOnDestroy = null;
- }
-
- if (null != _luaOnMouseUp)
- {
- _luaOnMouseUp.Dispose();
- _luaOnMouseUp = null;
- }
-
- if (null != _luaOnMouseDown)
- {
- _luaOnMouseDown.Dispose();
- _luaOnMouseDown = null;
- }
-
- if (null != _luaOnMouseDrag)
- {
- _luaOnMouseDrag.Dispose();
- _luaOnMouseDrag = null;
- }
- if (null != _luaOnEnable)
- {
- _luaOnEnable.Dispose();
- _luaOnEnable = null;
- }
- if (null != _luaOnDisable)
- {
- _luaOnDisable.Dispose();
- _luaOnDisable = null;
- }
- if (null != _luaOnApplicationFocus)
- {
- _luaOnApplicationFocus.Dispose();
- _luaOnApplicationFocus = null;
- }
- if (null != _luaOnApplicationPause)
- {
- _luaOnApplicationPause.Dispose();
- _luaOnApplicationPause = null;
- }
- if (null != _luaOnApplicationQuit)
- {
- _luaOnApplicationQuit.Dispose();
- _luaOnApplicationQuit = null;
- }
- }
- private void OnEnable()
- {
- if (null != _luaOnEnable)
- _luaOnEnable.Call();
- }
- private void OnDisable()
- {
- if (null != _luaOnDisable)
- _luaOnDisable.Call();
- }
- private void OnApplicationFocus(bool focus)
- {
- if (null != _luaOnApplicationFocus)
- _luaOnApplicationFocus.Call(focus);
- }
- private void OnApplicationPause(bool pause)
- {
- if (null != _luaOnApplicationPause)
- _luaOnApplicationPause.Call(pause);
- }
- private void OnApplicationQuit()
- {
- if (null != _luaOnApplicationQuit)
- _luaOnApplicationQuit.Call();
- }
- }
- }
|