封装游戏按钮事件(悬浮,进入,点击)
【摘要】 首先,思路是这样,根据按钮的名称,获取到按钮的组件,然后再为按钮加上按钮事件 第一步,获取按钮的transform 可以放在你的工具类中 /// <summary> /// 查找节点对象 /// </summary> /// <param name="goParent">父对象的GameObject</param> /// <param name="childNam...
首先,思路是这样,根据按钮的名称,获取到按钮的组件,然后再为按钮加上按钮事件 第一步,获取按钮的transform 可以放在你的工具类中
/// <summary>
/// 查找节点对象
/// </summary>
/// <param name="goParent">父对象的GameObject</param>
/// <param name="childName">子对象</param>
/// <returns></returns>
public static Transform FindChildNode(GameObject goParent, string childName)
{
Transform searchTrans = null;
searchTrans = goParent.transform.Find(childName);
if (searchTrans == null)
{
//遍历父物体的所有子节点
foreach (Transform item in goParent.transform)
{
searchTrans = FindChildNode(item.gameObject, childName);
if (searchTrans != null)
{
return searchTrans;
}
}
}
return searchTrans;
}
这样使用递归算法获取你所需要查找的组件,然后在Getcomponent<Button>(),获取到button组件, 第二步,封装按钮事件,也就是重写按钮组件的3个方法
public class EventTriggerListener : EventTrigger
{
public delegate void MyEventHandler(GameObject go);
public MyEventHandler onClick;
public MyEventHandler onMouseEnter;
public MyEventHandler onMouseExit;
public static EventTriggerListener GetListener (GameObject go)
{
EventTriggerListener eventTriggerListener = go.GetComponent<EventTriggerListener>();
if (eventTriggerListener == null)
{
eventTriggerListener = go.AddComponent<EventTriggerListener>();
}
return eventTriggerListener;
}
public override void OnPointerClick(PointerEventData eventData)
{
onClick?.Invoke(gameObject);
}
public override void OnPointerEnter(PointerEventData eventData)
{
onMouseEnter?.Invoke(gameObject);
}
public override void OnPointerExit(PointerEventData eventData)
{
onMouseExit?.Invoke(gameObject);
}
}
然后是按钮的事件,可以直接调用
public class btnEvent : MonoBehaviour
{
/// <summary>
/// 注册按钮点击事件
/// </summary>
/// <param name="buttonName">按钮名称</param>
/// <param name="myEventHandler">点击的事件</param>
public static void RigisterButtonClickEvent(GameObject buttonObj, EventTriggerListener.MyEventHandler myEventHandler)
{
if (buttonObj != null)
{
EventTriggerListener.GetListener(buttonObj).onClick = myEventHandler;
}
}
/// <summary>
/// 注册按钮悬停事件
/// </summary>
/// <param name="buttonName"></param>
/// <param name="myEventHandler"></param>
public static void RigisterButtonEnterEvent(GameObject buttonObj, EventTriggerListener.MyEventHandler myEventHandler)
{
if (buttonObj !=null)
{
EventTriggerListener.GetListener(buttonObj).onMouseEnter = myEventHandler;
}
}
/// <summary>
/// 注册按钮离开事件
/// </summary>
/// <param name="buttonName"></param>
/// <param name="myEventHandler"></param>
public static void RigisterButtonExitEvent(GameObject buttonObj, EventTriggerListener.MyEventHandler myEventHandler)
{
if (buttonObj != null)
{
EventTriggerListener.GetListener(buttonObj).onMouseExit = myEventHandler;
}
}
调用的优雅代码:
btnEvent.RigisterButtonClickEvent( Btnlogin.gameObject//按钮的名称, p => {
Eventbtnlogin.Invoke();//按钮调用的方法
});
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)