0评论

UGUI之UI组件及GameObject的事件监听系统封装

清风 2018-09-20 64浏览

想免费获取内部独家PPT资料库?观看行业大牛直播?点击加入腾讯游戏学院游戏美术行业精英群167422913

第一种:用代码动态的给组件加监听事件,都是利用组件本身的事件接口去实现相应事件的方法,只不过是用代码实现的动态添加的事件监听。下面是放个按钮的添加监听事件的方法,其中一个是带参数的添加方法。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public static class Develop
{
    public static void AddEventTrigger(this GameObject obj, EventTriggerType eventType, UnityAction<BaseEventData> myFunction)
    {
        EventTrigger eventTrigger = obj.GetComponent<EventTrigger>();
        if (eventTrigger == null)
        {
            eventTrigger = obj.AddComponent<EventTrigger>();
            eventTrigger.triggers = new List<EventTrigger.Entry>();
        }
        UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(myFunction);
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = eventType;
        entry.callback.AddListener(callback);
        eventTrigger.triggers.Add(entry);
    }
    public static void RemoveEventTrigger(this GameObject obj)
    {
        EventTrigger eventTrigger = obj.GetComponent<EventTrigger>();
        if (eventTrigger == null)
        {
            return;
        }
        eventTrigger.triggers.Clear();
    }
    public static void AddEventTrigger(this Image obj, EventTriggerType eventType, UnityAction<BaseEventData> myFunction)
    {
        EventTrigger eventTrigger = obj.GetComponent<EventTrigger>();
        if (eventTrigger == null)
        {
            eventTrigger = obj.gameObject.AddComponent<EventTrigger>();
            eventTrigger.triggers = new List<EventTrigger.Entry>();
        }
        UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(myFunction);
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = eventType;
        entry.callback.AddListener(callback);
        eventTrigger.triggers.Add(entry);
    }
    public static void RemoveEventTrigger(this Image obj)
    {
        EventTrigger eventTrigger = obj.gameObject.GetComponent<EventTrigger>();
        if (eventTrigger == null)
        {
            return;
        }
        eventTrigger.triggers.Clear();
    }
    public static void AddEventTrigger(this RawImage obj, EventTriggerType eventType, UnityAction<BaseEventData> myFunction)
    {
        EventTrigger eventTrigger = obj.GetComponent<EventTrigger>();
        if (eventTrigger == null)
        {
            eventTrigger = obj.gameObject.AddComponent<EventTrigger>();
            eventTrigger.triggers = new List<EventTrigger.Entry>();
        }
        UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(myFunction);
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = eventType;
        entry.callback.AddListener(callback);
        eventTrigger.triggers.Add(entry);
    }
    public static void RemoveEventTrigger(this RawImage obj)
    {
        EventTrigger eventTrigger = obj.gameObject.GetComponent<EventTrigger>();
        if (eventTrigger == null)
        {
            return;
        }
        eventTrigger.triggers.Clear();
    }
}

第二种:是根据NGUI的UIEventListener衍生出来的,直接加入UI对象进行事件注册即可,也可以使用在非UI的GameObject上,这种方法如果用在非UI的物体上,需要在主相机上添加一个PhysicRaycaster组件才能使用。
using UnityEngine;
using UnityEngine.EventSystems;
public class UIEventListener : EventTrigger
{
    public delegate void VoidDelegate(GameObject go);
    public delegate void VectorDelegate(GameObject go, Vector2 screenPosition);
    public delegate void BoolDelegate(GameObject go, bool value);
    public VoidDelegate onClick;
    public VoidDelegate onDown;
    public VoidDelegate onUp;
    public BoolDelegate onHover;
    public VectorDelegate onBeginDrag;
    public VectorDelegate onDrag;
    public VectorDelegate onEndDrag;
    static public UIEventListener Get(GameObject go)
    {
        PhysicsRaycaster raycaster = Camera.main.gameObject.GetComponent<PhysicsRaycaster>();
        if (raycaster == null) raycaster = Camera.main.gameObject.AddComponent<PhysicsRaycaster>();
        UIEventListener listener = go.GetComponent<UIEventListener>();
        if (listener == null) listener = go.AddComponent<UIEventListener>();
        return listener;
    }
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null) onClick.Invoke(gameObject);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null) onDown.Invoke(gameObject);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null) onUp.Invoke(gameObject);
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onHover != null) onHover.Invoke(gameObject, true);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onHover != null) onHover.Invoke(gameObject, false);
    }
    public override void OnBeginDrag(PointerEventData eventData)
    {
        Vector2 position;
        Canvas canvas = FindObjectOfType<Canvas>();
        RectTransform rect = canvas.GetComponent<RectTransform>();
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, eventData.position, canvas.worldCamera, out position))
        {
            if (onBeginDrag != null) onBeginDrag.Invoke(gameObject, position);
        }
    }
    public override void OnDrag(PointerEventData eventData)
    {
        Vector2 position;
        Canvas canvas = FindObjectOfType<Canvas>();
        RectTransform rect = canvas.GetComponent<RectTransform>();
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, eventData.position, canvas.worldCamera, out position))
        {
            if (onDrag != null) onDrag.Invoke(gameObject, position);
        }
    }
    public override void OnEndDrag(PointerEventData eventData)
    {
        Vector2 position;
        Canvas canvas = FindObjectOfType<Canvas>();
        RectTransform rect = canvas.GetComponent<RectTransform>();
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, eventData.position, canvas.worldCamera, out position))
        {
            if (onEndDrag != null) onEndDrag.Invoke(gameObject, position);
        }
    }
}
来自:https://blog.csdn.net/weixin_39706943/article/details/80507509