在unity 中使用xlua热更的时候,我们需要使用到lua模拟class
我先简单说一下思路。
第一,就是先建立一个表(我们起名字叫根表),在建立一个表(我们起起名字叫小表)在小表中我们需要预制出来相关的多个相同游戏物体,同时这写预制体上有相关的方法,可以调取操作
第二,将相关的参数与小表对应的放在根表中。例如 根表['预制体1'] = 小表 也就是下文中的
local instGoodInfo = A_shop:new(v)
A_shop.Inst_goodlist[goodlistID] = instGoodInfo
第三,也就是需要注意的地方 所有的你写的方法变量方法都是基于小表上的 , 比如 变量: 小表['id'] = 预制体的id
方法: function 小表:方法名字 () 记得是 : 二不是 .
第四,如何通过根表使用与控制相关的预制体 下面代码中的18行就是使用模拟中的class
A_shop={}
function Prefab_shopList_content()
--检测是否存在这个table 预制的货物列表 --------也就是根表 A_shop.Inst_goodlist
if A_shop['Inst_goodlist']==nil then
A_shop.Inst_goodlist={}
end
--将货物的id 与货物相连 我们需要预制出来不同id的列表-也就是预制体显示在页面上供玩家使用
for i,v in ipairs(shoplist) do
--检测预支表中没有这商品 则开始预支此商品
local goodlistID =tostring(v['listID'])
print("调取的是json的第几个 i="..i.." --------------------货物的id=="..goodlistID)
--if A_shop.Inst_goodlist[goodlistID] == nil then
--将此商品的信息传送 lua 模拟的C#的class中
local instGoodInfo = A_shop:new(v)
--将预制的商品放进这个预制出来的商品列表中
A_shop.Inst_goodlist[goodlistID] = instGoodInfo
if(A_shop.Inst_goodlist[goodlistID]~= nil) then
A_shop.Inst_goodlist[goodlistID]:Show_list_content()
end
--end
end
print("table 中的数量是------------------------------------------------------------==", #A_shop.Inst_goodlist)
下面是我们写的小表也就模拟的class --使用table
--调取这个方法的话我们就开始生成预制体并模拟类
function A_shop:new(goodInIfoTable)
local goodListCont={}--这个就是小表(建议使用local变量) --一下左右的变量方法在这个表格里面 方法的话 记得使用 模拟的类名:方法名字 也就是 A_shop:new
--将传进来table接受 同时这个表格中是 此商品的相关详细信息
goodListCont['goodInfo']= goodInIfoTable
goodListCont[tostring(goodInIfoTable.listID)]= goodInIfoTable
--将商店的货物预支出来
--将这个游戏体物体存放起来 以便继续调用
if goodListCont['goodInst']==nil then
goodListCont['goodInst']=UI.Instantiate(A_shopBase.shop_content_prefab,A_shopBase.shanggu_content,goodListCont.goodInfo['listID'])
end
--次货物的相关变量区------------------------------------------------------------------------
goodListCont['goodID']= 0--货物的id
goodListCont['jiage_num']=0--这个商品的价格值
goodListCont['haike_buy']=99999999--家还再买几个
goodListCont['player_buy_num']=0--购买的所有的数量
goodListCont['allMoney'] = 0--总的价格数
goodListCont['duqu']=false;
--此货物的变量赋值区-----------------------------------------------------------------------------
--货物的购买按钮--呼叫出提示板
goodListCont.button_yes.onClick:RemoveAllListeners()
goodListCont.button_yes.onClick:AddListener (function()
goodListCont:Opend_tishiban()
end)
----用于展示货物面板的显示
function goodListCont:Show_list_content()
print("-------------测试显示报错在哪里-----22---")
goodListCont['goodID'] = goodListCont.goodInfo['listID']--将 传动进来
print('我是在goodID='..goodListCont.goodID)
goodListCont['jiage_num']=(goodListCont.goodInfo['money']) --将价格传动进来
print('我是在goodtype='..goodListCont.goodInfo['goodtype'])
print(' ID='..goodListCont.goodInfo['ID'])
goodListCont.name.text =Public.GameMainData:Bcak_yuandata_name (goodListCont.goodInfo['goodtype'],goodListCont.goodInfo['ID'])--将名字显示出来
if (goodListCont.goodInfo['isLimit'] == 1) then--说明玩家需要进行显示数量
goodListCont.buy_num:GetComponent('Text').text = "(今日还可购买"..A_shop.Shangjia_num(goodListCont.goodInfo['listID'],Public.GameMainData.vip.."").."次)";
else
goodListCont.buy_num:GetComponent('Text').text= ""
end
goodListCont.jianjie.text = goodListCont.goodInfo['content']--简介写上去
goodListCont.money.text="价格: "..goodListCont.goodInfo['money']..""
print("测试从本都读取的json 此商品的价格的数据="..goodListCont.goodInfo['money'])
---提示板的内容也写上去
goodListCont.tishiban_momey.text = goodListCont.jiage_num
return goodListCont
end