当前位置:网站首页>Xlua get button registration click event of ugui
Xlua get button registration click event of ugui
2022-06-26 18:27:00 【wodownload2】
local button = self.gameObject.transform:Find("Button"):GetComponent(typeof(CS.UnityEngine.UI.Button))
button.onClick:AddListener(ClickButton)
function ClickButton()
print("clickbutton")
end
perhaps :
local button = self.gameObject.transform:Find(“Button”):GetComponent(“Button”)
Then if there are more than one button, But the processing functions are the same , How to distinguish which is button Click back ?
You can't write n A similar callback function :
have access to , Here's how :
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

You can also bind When , Pass many parameters , as follows :
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
So that's it BindCallback Compare the core :
local unpack = unpack or table.unpack
-- Solve native pack Of nil Cut off the problem ,SafePack And SafeUnpack Use in pairs
function SafePack(...)
local params = {
...}
params.n = select('#', ...)
return params
end
-- Solve native unpack Of nil Cut off the problem ,SafePack And SafeUnpack Use in pairs
function SafeUnpack(safe_pack_tb)
return unpack(safe_pack_tb, 1, safe_pack_tb.n)
end
-- The two one. SafePack Table execution join
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
-- Closure binding
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
-- Callback binding
-- Heavy duty form :
-- 1、 Member functions 、 Private function binding :BindCallback(obj, callback, ...)
-- 2、 Closure binding :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
The principle is closure
边栏推荐
猜你喜欢

Padding percentage operation

Image binarization

Runtimeerror: CUDA error: out of memory own solution (it is estimated that it is not applicable to most people in special circumstances)

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

必须要掌握的面试重点——索引和事务(附讲B-树与B+树)

Deep understanding of MySQL lock and transaction isolation level

Leetcode interview question 29 clockwise print matrix

手机影像内卷几时休?

自己创建一个时间拦截器

in和exsits、count(*)查询优化
随机推荐
Eigen库计算两个向量夹角
Vscode 基础必备 常用插件
CD-CompactDisk
手机影像内卷几时休?
新手炒股开户选哪个证券公司比较好?怎样炒股比较安全??
知識點總結
Idea collection code, quickly open favorites collection window
sql 中的alter操作总结
最小生成树、最短路径、拓扑排序、关键路径
Map和List<Map>转相应的对象
Clion compiling catkin_ WS (short for ROS workspace package) loads cmakelists Txt problems
SQL中的并、交、差运算
DAPP丨LP单双币流动性质押挖矿系统开发原理分析及源码
Request method 'POST' not supported
Chinese (Simplified) language pack
深度学习之Numpy篇
50 lines of code to crawl TOP500 books and import TXT documents
(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
ARM裸板调试之串口打印及栈初步分析
比较两个对象的大小关系原来可以如此花里胡哨