[Unity熱更新]tolua# & LuaFramework(四):讀取資料

[Unity熱更新]tolua# & LuaFramework(四):讀取資料

效果圖:

這裡的邏輯就是:

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

工程下載(unity5.3.2 LuaFramework_UGUI-1.0.4.109):

http://pan.baidu.com/s/1bMu8d4