前言:在Unity的UGUI中,用户点击一个按钮触发一个方法时,需要将此方法绑定在这个按钮上。UGUI提供了一个十分便捷的方法,就是将绑定方法的物体拖到按钮的事件机制中,直接选择方法就可以了。这种方法十分便捷,但是不便于框架化,不便于脚本管理,只适合初学者使用,下面记录的方法就是使用委托将事件动态注册到按钮中,方法如下。
1、首先要写几个基础的脚本,第一个为EventTriggerListener.cs,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
namespace EventAndMessage
{
public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger
{
#region 参数
public delegate void VoidDelegate(GameObject go);
public VoidDelegate onClick;
public VoidDelegate onDown;
public VoidDelegate onEnter;
public VoidDelegate onExit;
public VoidDelegate onUp;
public VoidDelegate onSelect;
public VoidDelegate onUpdateSelect;
#endregion
#region 方法
/// <summary>
/// 得到“监听器组件”
/// </summary>
/// <param name="go"></param>
/// <returns></returns>
public static EventTriggerListener Get(GameObject go)
{
EventTriggerListener lister = go.GetComponent<EventTriggerListener>();
if (lister == null)
{
lister = go.AddComponent<EventTriggerListener>();
}
return lister;
}
public override void OnPointerClick(PointerEventData eventData)
{
if (onClick != null)
{
onClick(gameObject);
}
}
public override void OnPointerDown(PointerEventData eventData)
{
if (onDown != null)
{
onDown(gameObject);
}
}
public override void OnPointerEnter(PointerEventData eventData)
{
if (onEnter != null)
{
onEnter(gameObject);
}
}
public override void OnPointerExit(PointerEventData eventData)
{
if (onExit != null)
{
onExit(gameObject);
}
}
public override void OnPointerUp(PointerEventData eventData)
{
if (onUp != null)
{
onUp(gameObject);
}
}
public override void OnUpdateSelected(BaseEventData eventData)
{
if (onUpdateSelect != null)
{
onUpdateSelect(gameObject);
}
}
#endregion
}
}
2.第二个脚本为FindTransform.cs,它的作用是使用递归在查找Canvas下的子物体,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Common
{
public static class FindTransform
{
#region 方法
internal static Transform FindChildByName(this Transform tf,string childName)
{
Transform childTF = tf.Find(childName);
if (childTF != null) return childTF;
for (int i = 0;i < tf.childCount;i++)
{
childTF = tf.GetChild(i).FindChildByName(childName);
if (childTF != null) return childTF;
}
return null;
}
#endregion
}
}
3.在Unity场景中创建两个按钮,按钮名称如下所示:
3.创建ThisUIManager.cs脚本,在里面写上为按钮注册事件的方法,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EventAndMessage;
using Common;
namespace UIManager
{
public class ThisUIManager : MonoBehaviour
{
#region 参数
//初始化实例对象
public static ThisUIManager _instance;
#endregion
#region 常规方法
void Start()
{
ToSetButton();
}
#endregion
#region 私有方法
/// <summary>
/// 注册按钮事件
/// </summary>
/// <param name="buttonName"></param>
/// <param name="delHandle"></param>
protected void RigisterButtonObjectEvent(string buttonName,EventTriggerListener.VoidDelegate delHandle)
{
GameObject goButton = FindTransform.FindChildByName(this.transform,buttonName).transform.gameObject;
//给按钮注册事件方法
if (goButton != null)
{
EventTriggerListener.Get(goButton.gameObject).onClick = delHandle;
}
}
/// <summary>
/// 给Button添加事件
/// </summary>
void ToSetButton()
{
RigisterButtonObjectEvent("logoButton",
p => {
Debug.Log("点击登录按钮!");
}
);
RigisterButtonObjectEvent("registerButton",
p => {
Debug.Log("点击注册按钮!");
}
);
}
#endregion
}
}
4.将此脚本挂载到Canvas上,运行工程,点击登录按钮,发现已经执行了登录的方法。
5.同样的,点击注册按钮,发现已经执行了注册的方法。
6.是不是感觉又学到知识了,这只是简单的使用,把这些基本的方法多运用到自己的项目中,融会贯通,才能真正的成为自己的知识和技能。