uluaFramework--Lua编写
2020-04-08 22:30
162 查看
LuaInterface包括两个核心库一个是LuaInterface.dll,一个是Luanet.dll,我们可以通过LuaInterface完成Lua和C#(CLR)之间的互相调用。lua对应C#内数据类型
nil null string System.String number System.Double boolean System.Boolean table LuaInterface.LuaTable function LuaInterface.LuaFunction
uluaframework 采用得是MVC框架,C#完成游戏资源更新后游戏主体逻辑使用Lua脚本编写完成
--初始化完成,发送链接服务器信息-- function Game.OnInitOK() --注册LuaView-- this.InitViewPanels(); --加载View对应得Control CtrlManager.Init(); --创建第一个窗口,初始UI local ctrl = CtrlManager.GetCtrl(CtrlNames.Prompt); if ctrl ~= nil and AppConst.ExampleMode == 1 then ctrl:Awake(); end logWarn('LuaFramework InitOK--->>>'); end --加载所有窗口文件 function Game.InitViewPanels() for i = 1, #PanelNames do require ("View/"..tostring(PanelNames[i])) end end
CtrlManager.lua文件
require "Common/define" require "Controller/PromptCtrl" require "Controller/MessageCtrl" CtrlManager = {}; local this = CtrlManager; local ctrlList = {}; --控制器列表-- function CtrlManager.Init() logWarn("CtrlManager.Init----->>>"); ctrlList[CtrlNames.Prompt] = PromptCtrl.New(); ctrlList[CtrlNames.Message] = MessageCtrl.New(); return this; end --添加控制器-- function CtrlManager.AddCtrl(ctrlName, ctrlObj) ctrlList[ctrlName] = ctrlObj; end --获取控制器-- function CtrlManager.GetCtrl(ctrlName) return ctrlList[ctrlName]; end --移除控制器-- function CtrlManager.RemoveCtrl(ctrlName) ctrlList[ctrlName] = nil; end --关闭控制器-- function CtrlManager.Close() logWarn('CtrlManager.Close---->>>'); end
MessageCtrl.lua文件,执行C#接口创建Message生成Prefab,回调OnCreate去通过LuaBehaviour去添加按钮点击事件
MessageCtrl = {}; local this = MessageCtrl; local message; local transform; local gameObject; --构建函数-- function MessageCtrl.New() logWarn("MessageCtrl.New--->>"); return this; end function MessageCtrl.Awake() logWarn("MessageCtrl.Awake--->>"); panelMgr:CreatePanel('Message', this.OnCreate); end --启动事件-- function MessageCtrl.OnCreate(obj) gameObject = obj; message = gameObject:GetComponent('LuaBehaviour'); message:AddClick(MessagePanel.btnClose, this.OnClick); logWarn("Start lua--->>"..gameObject.name); end --单击事件-- function MessageCtrl.OnClick(go) destroy(gameObject); end --关闭事件-- function MessageCtrl.Close() panelMgr:ClosePanel(CtrlNames.Message); end
MessagePanel.lua文件,绑定Panel下组件
local transform; local gameObject; MessagePanel = {}; local this = MessagePanel; --启动事件-- function MessagePanel.Awake(obj) gameObject = obj; transform = obj.transform; this.InitPanel(); logWarn("Awake lua--->>"..gameObject.name); end --初始化面板-- function MessagePanel.InitPanel() this.btnClose = transform:FindChild("Button").gameObject; end --单击事件-- function MessagePanel.OnDestroy() logWarn("OnDestroy---->>>"); end
LuaBehaviour 来获取所有UI组件生命周期事件同时执行对应Lua脚本内方法
public class LuaBehaviour : View { private string data = null; private Dictionary<string, LuaFunction> buttons = new Dictionary<string, LuaFunction>(); protected void Awake() { Util.CallMethod(name, "Awake", gameObject); } protected void Start() { Util.CallMethod(name, "Start"); } protected void OnClick() { Util.CallMethod(name, "OnClick"); } protected void OnClickEvent(GameObject go) { Util.CallMethod(name, "OnClick", go); } /// <summary> /// 添加单击事件 /// </summary> public void AddClick(GameObject go, LuaFunction luafunc) { if (go == null || luafunc == null) return; buttons.Add(go.name, luafunc); go.GetComponent<Button>().onClick.AddListener( delegate () { luafunc.Call(go); } ); } /// <summary> /// 删除单击事件 /// </summary> /// <param name="go"></param> public void RemoveClick(GameObject go) { if (go == null) return; LuaFunction luafunc = null; if (buttons.TryGetValue(go.name, out luafunc)) { luafunc.Dispose(); luafunc = null; buttons.Remove(go.name); } } /// <summary> /// 清除单击事件 /// </summary> public void ClearClick() { foreach (var de in buttons) { if (de.Value != null) { de.Value.Dispose(); } } buttons.Clear(); } //----------------------------------------------------------------- protected void OnDestroy() { ClearClick(); #if ASYNC_MODE string abName = name.ToLower().Replace("panel", ""); ResManager.UnloadAssetBundle(abName + AppConst.ExtName); #endif Util.ClearMemory(); Debug.Log("~" + name + " was destroy!"); } }
相关文章推荐
- Lua 代码编写技巧
- Lua语言如何调用自己编写的C DLL -- 转
- Lua cmodule编写
- 使用 Lua 编写一个 Nginx 认证模块 - 技术翻译 - 开源中国社区
- 编写高性能Lua代码的方法
- 使用lua脚本编写wireshark协议插件
- 使用 C 编写 Lua 模块
- 用Lua编写iOS程序
- 编写高性能的Lua代码
- 编译Lua以及编写动态扩展
- 使用lua脚本编写wireshark协议插件
- 编写高性能的Lua代码
- wireshark lua: How to add your own tab to "Packet Bytes" pane (just like "reassembled TCP" tab) -- 用wireshark lua编写协议解析器dissecto
- 如何编写Lua扩展库 之编写原则
- Lua业务脚本编写规范
- cocos3.x 编写C++控件在lua上面调用(二)
- FCEUX金手指加强版 - 使用Lua脚本语言编写FC/NES金手指脚本
- 基础补充18:Lua的模块编写与module函数
- 如何编写简单的lua小脚本@@
- Lua 任意调用WindowAPI 之 编写简单的音乐播放器