像游戏排行榜,活动可能用到这种事件绑定方式,代码如下:
print("LuaMarketWindow.lua");
goodsperfab = nil;
GoodsGrid = nil;
MarketScrollView =nil;
function CreateMarketWindow()
print("商城界面显示");
local marketwindow = LoadResource.Load("marketWindow","NiuMarketWindow","HallPanel/MarketWindow/NiuMarketWindow",true);
goodsperfab = LoadResource.Load("","goods","Prefab/goods",true);
HallUISubPanel = GameObject.Find('Window')
nMARKET_WINDOW = GameObject.Instantiate(marketwindow);
--for i,v in ipairs(AllWindows) do
-- print(i.."="..tostring(v))
--end
--ChangeWindow(nMARKET_WINDOW);
GoodsGrid = GameObject.Find("GoodsGrid");
MarketScrollView = GameObject.Find("MarketScrollView");
print("GoodsGrid-----",GoodsGrid,MarketScrollView)
SetGameObjectProperty(nMARKET_WINDOW,'MarketWindow',HallUISubPanel);
AddMarketGoods(10);
--UIEventListener
--RegisterButtonClick(closebutton,CloseButtonClick)
end
--设置克隆物体属性
function SetGameObjectProperty(igameobject,iname,iparent)
igameobject.transform.parent = iparent.transform;
igameobject.transform.localScale = Vector3.one;
igameobject.transform.localPosition = Vector3.zero;
igameobject.name=iname;
igameobject:SetActive(true);
end
--UIGrid grid = widgetpanelGrid.GetComponent<UIGrid>();
-- grid.enabled = true;
-- grid.Reposition();
-- GoToTop();
--GameObject friend = NGUITools.AddChild(widgetpanelGrid,widgetFriendModel);
function AddMarketGoods(goodsnum)
for i=1,goodsnum do
--local mgoods = GameObject.Instantiate(goodsperfab);
local mgoods = NGUITools.AddChild(GoodsGrid,goodsperfab);
mgoods.name = "goods_"..i;
--mgoods.transform.Find("Name").GetComponent<UILabel>().text = item.Name;
goodslabel = LuaHelper.GetComponentInChildren(mgoods,"UILabel");
goodslabel.text = "价钱:"..tostring(i*100).."元宝";
--RegisterButtonClick(mgoods,OnGoodsClick(i*100));
UIEventListener.Get(mgoods).onClick =LuaHelper.VoidDelegate(OnGoodsClick(i*100));
--print("ConstWrap",Vector2.one);
end
gird = GoodsGrid:GetComponent('UIGrid');
gird.enabled = true;
gird:Reposition();
goodsscrollview = MarketScrollView:GetComponent('UIScrollView');
goodsscrollview:ResetPosition();
end
function CloseButtonClick()
ChangeWindow(nHALL_WINDOW);
end
function OnGoodsClick(i)
print("iiiiiiiiiiiii=",i);
return function ()
CallBack(i)
end
end
function CallBack(i)
print("我的价格是:",i);
end
下面这段函数用到了Lua的闭包函数,
function OnGoodsClick(i)
print("iiiiiiiiiiiii=",i);
return function ()
CallBack(i)
end
end
function CallBack(i)
print("我的价格是:",i);
end
下面是CS添加绑定事件方法
public static UIEventListener.VoidDelegate VoidDelegate(LuaFunction func) {
UIEventListener.VoidDelegate action = (go) => {
func.Call(go);
};
return action;
}