|
设计思路:
全局只创建一个LuaState, 所有lua类都用Table实现
主要文件:- LuaScriptManager.cs //lua脚本管理,创建LuaState
- LuaBehaviour.cs //让lua控制游戏对象的中间Component
- class.lua //所有lua脚本的基类
复制代码 LuaScriptManager.cs- public class LuaScriptManager {
- private static LuaState _luaState;
- public static LuaState luaState {
- get
- {
- if (null == _luaState)
- {
- Init();
- }
- return _luaState;
- }
- }
- public static void Init()
- {
- new LuaResLoader();
- _luaState = new LuaState();
- _luaState.LogGC = true;
- _luaState.Start();
- LuaBinder.Bind(_luaState);
- DelegateFactory.Init();
- //添加搜索路径
- _luaState.AddSearchPath(Application.dataPath + "\\Resources/Lua");
- _luaState.AddSearchPath(Application.dataPath + "\\Resources/LuaScripts");
- _luaState.AddSearchPath(Application.dataPath + "\\Resources/LuaScripts/UIModule");
- }
- public static object RunLuaFile(string filePath)
- {
- return luaState.DoFile<object>(filePath);
- }
- public static object RunLuaString(string luaStr)
- {
- return luaState.DoString<object>(luaStr);
- }
- }
复制代码 LuaBehaviour.cs- public class LuaBehaviour : UIBase
- {
- //模块文件的路径
- public string luaFile;
- //对应的Lua实例的Table
- public LuaTable luaInstance;
- //lua实例名
- private string luaClassName;
- //lua中的函数
- private LuaFunction m_Awake = null;
- private LuaFunction m_Start = null;
- private LuaFunction m_OnEnable = null;
- private LuaFunction m_OnOpen = null;
- private LuaFunction m_Update = null;
- private LuaFunction m_OnClose = null;
- private LuaFunction m_OnDisable = null;
- private LuaFunction m_OnDestory = null;
- private void Awake()
- {
- luaInstance = LuaScriptManager.RunLuaFile(luaFile) as LuaTable;
- luaClassName = (string)luaInstance["className"];
- //初始化lua类
- Dictionary<string, object> param = new Dictionary<string, object>();
- luaInstance["transform"] = transform;
- luaInstance["gameObject"] = gameObject;
- //获取lua中的函数
- m_Awake = (LuaFunction)luaInstance["Awake"];
- m_Start = (LuaFunction)luaInstance["Start"];
- m_OnEnable = (LuaFunction)luaInstance["OnEnable"];
- m_OnOpen = (LuaFunction)luaInstance["OnOpen"];
- m_Update = (LuaFunction)luaInstance["Update"];
- m_OnClose = (LuaFunction)luaInstance["OnClose"];
- m_OnDisable = (LuaFunction)luaInstance["OnDisable"];
- m_OnDestory = (LuaFunction)luaInstance["OnDestory"];
- if (null != m_Awake)
- {
- m_Awake.Call<LuaTable>(luaInstance);
- }
- }
-
- private void Start()
- {
- if (null != m_Start)
- {
- m_Start.Call<LuaTable>(luaInstance);
- }
- }
- private void OnEnable()
- {
- if (null != m_OnEnable)
- {
- m_OnEnable.Call<LuaTable>(luaInstance);
- }
- }
- public override void OnOpen()
- {
- base.OnOpen();
- if (null != m_OnOpen)
- {
- m_OnOpen.Call<LuaTable>(luaInstance);
- }
- }
- private void Update()
- {
- if (null != m_Update)
- {
- m_Update.Call<LuaTable>(luaInstance);
- }
- }
- public override void OnClose()
- {
- base.OnClose();
- if (null != m_OnClose)
- {
- m_OnClose.Call<LuaTable>(luaInstance);
- }
- }
- private void OnDisable()
- {
- if (null != m_OnDisable)
- {
- m_OnDisable.Call<LuaTable>(luaInstance);
- }
- }
- private void OnDestory()
- {
- if (null != m_OnDestory)
- {
- m_OnDestory.Call<LuaTable>(luaInstance);
- }
- }
- }
复制代码 class.lua- function clone(object)
- local lookup_table = {}
- local function _copy(object)
- if type(object) ~= "table" then
- return object
- elseif lookup_table[object] then
- return lookup_table[object]
- end
- local new_table = {}
- lookup_table[object] = new_table
- for key, value in pairs(object) do
- new_table[_copy(key)] = _copy(value)
- end
- return setmetatable(new_table, getmetatable(object))
- end
- return _copy(object)
- end
- --Create an class.
- function class(className, superName, superModulePath)
- if superName ~= nil then
- require(superModulePath)
- end
- if _G[className] ~= nil then
- if _G[className].className == nil or _G[className].isLuaClass == nil then
- Debugger.LogError("Class name has been in the global,Please change the class name")
- end
- return
- end
- _G[className] = {}
- _G[className].className = className
- _G[className].isLuaClass = true
- _G[className].InitLuaClass = function(luaTable, valueTable)
- for key, value in pairs(valueTable) do
- luaTable[key] = value
- end
- end
-
-
- if _G[superName] ~= nil then
- setmetatable(_G[className], _G[superName])
- _G[superName].__index = _G[superName]
- end
-
- _G[className].New = function()
- local o = o or {}
- setmetatable(o, _G[className])
- _G[className].__index = _G[className]
- return o
- end
- return _G[className]
- end
复制代码 |
|