您的位置:首页 > 编程语言 > Lua

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!");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: