当前位置:网站首页>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
原理就是闭包
边栏推荐
- Clion breakpoint single step debugging
- JVM入個門(1)
- 同花顺开户怎么样安全吗?怎么炒股开户
- How to create and enforce indexes
- Deep understanding of MySQL lock and transaction isolation level
- JS 常用正则表达式
- CD-CompactDisk
- Properties file garbled
- Conditional compilation in precompiling instructions
- Leetcode 238 product of arrays other than itself
猜你喜欢

in和exsits、count(*)查询优化

Connected to surface test questions

决策树与随机森林

Detailed explanation of dos and attack methods

(必须掌握的多线程知识点)认识线程,创建线程,使用Thread的常见方法及属性,以及线程的状态和状态转移的意义

JVM入個門(1)

最小生成树、最短路径、拓扑排序、关键路径

Leetcode interview question 29 clockwise print matrix

DoS及攻擊方法詳解

Tag dynamic programming - preliminary knowledge for question brushing -2 0-1 knapsack theory foundation and two-dimensional array solution template
随机推荐
Li Kou daily question - day 28 -566 Reshape matrix
Runtimeerror: CUDA error: out of memory own solution (it is estimated that it is not applicable to most people in special circumstances)
爬取豆瓣读书Top250,导入sqlist数据库(或excel表格)中
微信小程序 uniapp 左滑 删除 带删除icon
SQL中的并、交、差运算
in和exsits、count(*)查询优化
深层次安全定义剖析及加密技术
将字符串B插入字符串A,有多少种插入办法可以使新串是一个回文串
JVM entry door (1)
GDB installation
Conditional compilation in precompiling instructions
Binary search-2
几种常见的UML关系图汇总
交叉编译环境出现.so链接文件找不到问题
MYSQL的下载与配置 mysql远程操控
PC端录制扫515地机器人/scan数据
数字签名论述及生成与优点分析
深度学习之Numpy篇
(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
VCD video disc