ToLua(LuaFramework) - Tolua 中Button回调函数(三)

在我们的 Unity 前端中,给 Button 添加监听事件的时候,有两种方法:
1、通过 Inspector 信息面板上的进行拖拽添加
2、通过 代码: Button.OnClick.AddListener()方法

在 Lua 中添加 监听的话就是通过:
LuaBehaviour 脚本进行,在InitPanel函数中添加查找按钮的代码:

MainPanel = {}
local this = MainPanel
local transform;
local gameObject;
local Btn_StartPlay

--启动事件--
function this.Awake(obj)
    gameObject = obj
    transform = obj.transform
    --print(this.transform.name)
    this.InitPanel()
end

--初始化面板--
function this.InitPanel()
    this.Txt_Info = transform:Find("Txt_Info").gameObject
    this.Btn_ShowInfo = transform:Find("Btn_ShowInfo").gameObject
    this.Btn_HideInfo = transform:Find("Btn_HideInfo").gameObject
end

function this.Update()

end


--单击事件--
function this.OnDestroy()
    logWarn("OnDestroy---->>>");
end
MainCtrl = {};
local this = MainCtrl;

local transform
local gameObject

-- 构建函数--
function this.New()
    return this;
end

function this.Awake()
    panelMgr:CreatePanel('Main', this.OnCreate);
end

-- 启动事件--
function this.OnCreate(obj)
    print("Awake开始")
    gameObject = obj
    transform = obj.transform
    behaviour = gameObject:GetComponent('LuaBehaviour')
    behaviour:AddClick(MainPanel.Btn_ShowInfo,function()
        print("显示")
        MainPanel.Txt_Info.gameObject:SetActive(true)
    end)
    behaviour:AddClick(MainPanel.Btn_HideInfo,function()
        print("隐藏")
        MainPanel.Txt_Info.gameObject:SetActive(false)
    end)
end

AddClick方法有两个参数,第一个是按钮本身(上一步才引用过的),第二个是点击后的回调函数。
AddClick的具体实现可以可以在LuaBehaviour.cs中找到。
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值