当前位置:网站首页>xlua获取ugui的button注册click事件
xlua获取ugui的button注册click事件
2022-06-26 18:18:00 【wodownload2】
local button = self.gameObject.transform:Find("Button"):GetComponent(typeof(CS.UnityEngine.UI.Button))
button.onClick:AddListener(ClickButton)
function ClickButton()
print("clickbutton")
end
或者:
local button = self.gameObject.transform:Find(“Button”):GetComponent(“Button”)
那如果多个button,但是处理函数都是差不多的,怎么区分是哪个button点击回来的呢?
总不能写n个类似的回调函数吧:
可以使用,如下的方法:
local function OnInitial(self)
XRNActivityWrapper.gameObject = self.gameObject
local button = self.gameObject.transform:Find("Button1"):GetComponent(typeof(CS.UnityEngine.UI.Button))
local openPageCallback = BindCallback(self, XRNActivityWrapper.ClickButton, button)
button.onClick:AddListener(openPageCallback)
local button = self.gameObject.transform:Find("Button2"):GetComponent(typeof(CS.UnityEngine.UI.Button))
local openPageCallback = BindCallback(self, XRNActivityWrapper.ClickButton, button)
button.onClick:AddListener(openPageCallback)
end
function XRNActivityWrapper:ClickButton(button)
print("clickbutton")
print(button.name)
if (button.name == "Button1") then
print("hello button1")
elseif (button.name == "Button2") then
print("hello button2")
end
end

你还可以bind的时候,传递很多参数哦,如下:
local openPageCallback = BindCallback(self, XRNActivityWrapper.ClickButton, button, 123, "nihao")
button.onClick:AddListener(openPageCallback)
function XRNActivityWrapper:ClickButton(button, a, b)
print("clickbutton")
print(button)
print(a)
print(b)
end
所以就这个BindCallback比较核心:
local unpack = unpack or table.unpack
-- 解决原生pack的nil截断问题,SafePack与SafeUnpack要成对使用
function SafePack(...)
local params = {
...}
params.n = select('#', ...)
return params
end
-- 解决原生unpack的nil截断问题,SafePack与SafeUnpack要成对使用
function SafeUnpack(safe_pack_tb)
return unpack(safe_pack_tb, 1, safe_pack_tb.n)
end
-- 对两个SafePack的表执行连接
function ConcatSafePack(safe_pack_l, safe_pack_r)
local concat = {
}
for i = 1,safe_pack_l.n do
concat[i] = safe_pack_l[i]
end
for i = 1,safe_pack_r.n do
concat[safe_pack_l.n + i] = safe_pack_r[i]
end
concat.n = safe_pack_l.n + safe_pack_r.n
return concat
end
-- 闭包绑定
function Bind(self, func, ...)
assert(self == nil or type(self) == "table")
assert(func ~= nil and type(func) == "function")
local params = nil
if self == nil then
params = SafePack(...)
else
params = SafePack(self, ...)
end
return function(...)
local args = ConcatSafePack(params, SafePack(...))
func(SafeUnpack(args))
end
end
-- 回调绑定
-- 重载形式:
-- 1、成员函数、私有函数绑定:BindCallback(obj, callback, ...)
-- 2、闭包绑定:BindCallback(callback, ...)
function BindCallback(...)
local bindFunc = nil
local params = SafePack(...)
assert(params.n >= 1, "BindCallback : error params count!")
if type(params[1]) == "table" and type(params[2]) == "function" then
bindFunc = Bind(...)
elseif type(params[1]) == "function" then
bindFunc = Bind(nil, ...)
else
error("BindCallback : error params list!")
end
return bindFunc
end
原理就是闭包
边栏推荐
- How about opening an account at Guojin securities? Is it safe to open an account?
- Determine whether a sequence is a stack pop-up sequence
- 刻录光盘的程序步骤
- DoS及攻擊方法詳解
- LeetCode 128最长连续序列
- 【Mysql系列】工作常用sql集锦(持续更新)
- How to open a stock account? Is it safe to open an account online now?
- 你了解如何比较两个对象吗
- Insert string B into string A. how many insertion methods can make the new string a palindrome string
- How to create and enforce indexes
猜你喜欢

Paging query and join Association query optimization

LeetCode 238 除自身以外数组的乘积

The cross compilation environment appears So link file not found problem

Decompilation of zero time technology smart contract security series articles

行锁分析和死锁

Solve the problem that each letter occupies a space in pycharm

VCD video disc

数字签名标准(DSS)

带你解决哈希冲突,并实现一个简单hash表,

Image binarization
随机推荐
Deep understanding of MySQL lock and transaction isolation level
LeetCode 面试题29 顺时针打印矩阵
字符串String转换为jsonArray并解析
Temporarily turn off MySQL cache
sqlite数据库的系统表sqlite_master
Case study of row lock and isolation level
I want to know. I am in Zhaoqing. Where can I open an account? Is it safe to open an account online?
System table SQLite of SQLite database_ master
Handwritten numeral recognition based on tensorflow
RSA concept explanation and tool recommendation - LMN
Map and filter methods for processing scarce arrays
行锁与隔离级别案例分析
(multi threading knowledge points that must be mastered) understand threads, create threads, common methods and properties of using threads, and the meaning of thread state and state transition
Eigen库计算两个向量夹角
DVD-数字通用光盘
爬取豆瓣读书Top250,导入sqlist数据库(或excel表格)中
Analysis of deep security definition and encryption technology
Map and list < map > transfer to corresponding objects
【Mysql系列】工作常用sql集锦(持续更新)
Insert string B into string A. how many insertion methods can make the new string a palindrome string