效果图:
这里的逻辑就是:
1.选择区域后,如果选择副本的界面没有实例化,那么就实例化并且刷新数据;否则将隐藏的界面显示并且刷新数据。界面初次实例化后读取数据。
2.刷新数据的过程就是,根据选择的区域挑选对应的数据,并将数据绑定到UI上,给UI换图片。当点击UI时就把绑定到UI的数据输出。加载完的图片该给哪个UI呢?这里我设置了一个index,逐一赋值给界面的UI列表就可以了。
注意点:
1.框架中的ResourceManager只提供了LoadPrefab这个方法用于实例化,如果想加载AB包中的其他资源(例如本例就需要加载文本和图片),那么就要在ResourceManager中添加相应的方法。最后不要忘了重新生成wrap文件。
2.不要忘了在CustomSettings.cs中注册需要用到的类。
这里给出核心的脚本:
--SelectTranScriptPanel.lua require "Logic/UITranScript" local gameObject; local transform; SelectTranScriptPanel = {}; local this = SelectTranScriptPanel; function SelectTranScriptPanel.Awake(obj) gameObject = obj; transform = obj.transform; this.InitPanel(); end function SelectTranScriptPanel.Start(obj) end function SelectTranScriptPanel.InitPanel() this.btnClose = transform:FindChild("Close").gameObject; --此时key默认从1开始递增 SelectTranScriptPanel.Sprites = { UITranScript:new(transform:FindChild("Panel/TranScript").gameObject,0), UITranScript:new(transform:FindChild("Panel/TranScript2").gameObject,0), UITranScript:new(transform:FindChild("Panel/TranScript3").gameObject,0), UITranScript:new(transform:FindChild("Panel/TranScript4").gameObject,0), UITranScript:new(transform:FindChild("Panel/TranScript5").gameObject,0), UITranScript:new(transform:FindChild("Panel/TranScript6").gameObject,0), } end function SelectTranScriptPanel.OnDestroy() end
--SelectTranScriptCtrl.lua local list = require "system/list" require "Logic/TranScriptInfo" TranScriptInfoList = list:new(); SelectTranScriptCtrl = {}; local this = SelectTranScriptCtrl; local gameObject; local transform; local luaBehaviour; function SelectTranScriptCtrl.New() return this; end function SelectTranScriptCtrl.Awake() end function SelectTranScriptCtrl.OnCreate(obj) gameObject = obj; transform = obj.transform; luaBehaviour = gameObject:GetComponent('LuaBehaviour'); luaBehaviour:AddClick(SelectTranScriptPanel.btnClose, this.Close); for index,value in ipairs(SelectTranScriptPanel.Sprites) do luaBehaviour:AddClick(value.gameObject, this.ImageClick); end resMgr:LoadTextAsset('data', { 'area' }, this.GetInfo); end local nowAreaID = 0; function SelectTranScriptCtrl.GetInfo(objs) local str = System.String.New(objs[0]:ToString()) local strArray = str:Split('\r\n',System.StringSplitOptions.RemoveEmptyEntries); for i = 2, strArray.Length - 1 do local temp = System.String.New(strArray[i]); local strArray2 = temp:Split(',',System.StringSplitOptions.RemoveEmptyEntries); local l = TranScriptInfo:new(strArray2[0],strArray2[1],strArray2[2],strArray2[3],strArray2[4],strArray2[5]); TranScriptInfoList:push(l); end SelectTranScriptCtrl.Refresh(nowAreaID); end local nowImageIndex; function SelectTranScriptCtrl.Refresh(areaID) nowImageIndex = 1; local now = nil; local spriteIndex = 1; for i = 1,TranScriptInfoList.length,1 do now = TranScriptInfoList:next(now); local v = now.value; if(v.id == areaID) then SelectTranScriptPanel.Sprites[spriteIndex].data = v; spriteIndex = spriteIndex + 1; resMgr:LoadSprite('selecttranscript_asset', { v.scriptIcon }, this.ImageInit); end end end function SelectTranScriptCtrl.ImageInit(objs) --print(nowImageIndex); local go = SelectTranScriptPanel.Sprites[nowImageIndex].gameObject; go:GetComponent('Image').sprite = objs[0]; go.transform:FindChild("Text"):GetComponent('Text').text = SelectTranScriptPanel.Sprites[nowImageIndex].data.scriptName; nowImageIndex = nowImageIndex + 1; end function SelectTranScriptCtrl.ImageClick(go) for index,value in ipairs(SelectTranScriptPanel.Sprites) do if(value.gameObject == go) then local v = value.data; if(v ~= nil and v ~= 0) then print(v.id..v.areaName..v.scriptName..v.scriptIcon..v.scriptTable..v.scriptScene); end end end end ------------------------------------------------------------------------------- function SelectTranScriptCtrl.Open(areaID) nowAreaID = areaID; if(TranScriptInfoList.length > 0) then SelectTranScriptCtrl.Refresh(nowAreaID); end --print(nowAreaID.." "..TranScriptInfoList.length) if gameObject == nil then panelMgr:CreatePanel('SelectTranScript', this.OnCreate); else gameObject:SetActive(true); end end function SelectTranScriptCtrl.Close(go) gameObject:SetActive(false); end