[Unity熱更新]LuaFramework04.UI界面


先來看下框架是怎么展示一個界面的:

lua層的入口是Game.OnInitOK,然后調用PromptCtrl.Awake,加載並創建PromptPanel,給界面掛上LuaBehaviour,在LuaBehaviour中,回調PromptPanel.lua的Awake、Start方法

涉及到的lua文件:界面對應的xxxCtrl和xxxPanel,Game,define,CtrlManager

 

接下來弄一個新的界面替代原來的:

1.創建一個界面TestPanel並打包

 

2.修改PanelManager.cs,使接口更加靈活通用

 

3.針對xxxCtrl和xxxPanel,創建lua文件模板。方法是找到unity安裝目錄\Editor\Data\Resources\ScriptTemplates,把下面兩個模板文件放進去,重啟unity,即可方便地創建lua文件

100-LuaFramework__Panel Script-Panel.lua.txt

 1 --Find,GetComponent
 2 
 3 #SCRIPTNAME# = {};
 4 local this = #SCRIPTNAME#;
 5  
 6 local gameObject;
 7 local transform;
 8  
 9 --由LuaBehaviour自動調用
10 function #SCRIPTNAME#.Awake(obj)
11     gameObject = obj;
12     transform = obj.transform;
13 end
14  
15 --由LuaBehaviour自動調用
16 function #SCRIPTNAME#.Start()
17 end

101-LuaFramework__Ctrl Script-Ctrl.lua.txt

 1 #SCRIPTNAME# = {};
 2 local this = #SCRIPTNAME#;
 3 
 4 function #SCRIPTNAME#.New()
 5     return this;
 6 end
 7  
 8 function #SCRIPTNAME#.Awake()
 9     panelMgr:CreatePanel("Prefab/Test","TestPanel");
10 end

 

4.通過上面的方法創建好TestCtrl.lua和TestPanel.lua,然后參考框架自帶例子修改Game,define,CtrlManager,即可展示出這個新的界面TestPanel

5.添加點擊事件

TestPanel.lua

 1 --Find,GetComponent
 2 
 3 TestPanel = {};
 4 local this = TestPanel;
 5  
 6 local gameObject;
 7 local transform;
 8 local luaBehaviour;
 9 
10 --由LuaBehaviour自動調用
11 function TestPanel.Awake(obj)
12     gameObject = obj;
13     transform = obj.transform;
14     luaBehaviour = transform:GetComponent('LuaBehaviour');
15 
16     this.btn = transform:Find("BG/Button"):GetComponent("Button");
17     luaBehaviour:AddClick(this.btn.gameObject, function ()
18         logWarn("click!!!");
19     end);
20 end
21  
22 --由LuaBehaviour自動調用
23 function TestPanel.Start()
24 end

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM