UGUI按钮绑定事件

1、无参数方法:

Button btn;
btn=GameObject.Find(“按钮Obj”).GetComponent();
btn.onClick.AddListener(OnBtnClick);
public void OnBtnClick()
{
print(“触发按钮事件”);
}

2、有参数的按钮事件

UIEventTrigger脚本组件导入工程 其代码如下:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class UIEventTrigger : UnityEngine.EventSystems.EventTrigger
{
    public delegate void BaseEventDelegate(BaseEventData eventData);
 
    public delegate void PointerEventDelegate(PointerEventData eventData);
 
    public delegate void AxisBaseEventDelegate(AxisEventData eventData);
 
    public PointerEventDelegate onBeginDrag;
    public BaseEventDelegate onCancel;
    public BaseEventDelegate onDeselect;
    public PointerEventDelegate onDrag;
    public PointerEventDelegate onDrop;
    public PointerEventDelegate onEndDrag;
    public PointerEventDelegate onInitializePotentialDrag;
    public AxisBaseEventDelegate onMove;
    public PointerEventDelegate onPointerClick;
    public PointerEventDelegate onPointerDown;
    public PointerEventDelegate onPointerEnter;
    public PointerEventDelegate onPointerExit;
    public PointerEventDelegate onPointerUp;
    public PointerEventDelegate onScroll;
    public BaseEventDelegate onSelect;
    public BaseEventDelegate onSubmit;
    public BaseEventDelegate onUpdateSelected;
 
    public static UIEventTrigger Get(GameObject go)
    {
        UIEventTrigger proxy = go.GetComponent<UIEventTrigger>();
        if (proxy == null) proxy = go.AddComponent<UIEventTrigger>();
        return proxy;
    }
 
    public override void OnBeginDrag(PointerEventData eventData)
    {
        if (onBeginDrag != null) onBeginDrag(eventData);
    }
 
    public override void OnCancel(BaseEventData eventData)
    {
        if (onCancel != null) onCancel(eventData);
    }
 
    public override void OnDeselect(BaseEventData eventData)
    {
        if (onDeselect != null) onDeselect(eventData);
    }
 
    public override void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null) onDrag(eventData);
    }
 
    public override void OnDrop(PointerEventData eventData)
    {
        if (onDrop != null) onDrop(eventData);
    }
 
    public override void OnEndDrag(PointerEventData eventData)
    {
        if (onEndDrag != null) onEndDrag(eventData);
    }
 
    public override void OnInitializePotentialDrag(PointerEventData eventData)
    {
        if (onInitializePotentialDrag != null) onInitializePotentialDrag(eventData);
    }
 
    public override void OnMove(AxisEventData eventData)
    {
        if (onMove != null) onMove(eventData);
    }
 
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onPointerClick != null) onPointerClick(eventData);
    }
 
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onPointerDown != null) onPointerDown(eventData);
    }
 
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onPointerEnter != null) onPointerEnter(eventData);
    }
 
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onPointerExit != null) onPointerExit(eventData);
    }
 
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onPointerUp != null) onPointerUp(eventData);
    }
 
    public override void OnScroll(PointerEventData eventData)
    {
        if (onScroll != null) onScroll(eventData);
    }
 
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(eventData);
    }
 
    public override void OnSubmit(BaseEventData eventData)
    {
        if (onSubmit != null) onSubmit(eventData);
    }
 
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelected != null) onUpdateSelected(eventData);
    }
}

控制按钮事件脚本:
创建脚本,并绑定对象,脚本如下:
UIEventTrigger trigger=UIEventTrigger.Get(按钮Object);
trigger.onPointClick+=OnClickBtn;
//按钮方法
public void OnClickBtn(PointerEventData eventdata)
{
print(“按钮预设物名字”+eventdata.name);
print(“按钮text名字”+eventdata.selectedObject.GetComponent ().text);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

unity学院

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值