美文网首页
Unity的button监听

Unity的button监听

作者: 神大人korose | 来源:发表于2018-03-20 13:57 被阅读0次
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class EventTriggerListener : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerEnterHandler, IPointerExitHandler, IPointerUpHandler, IDragHandler, IEndDragHandler
    {
        public delegate void VoidDelegate(PointerEventData eventData);
        public VoidDelegate onClick;
        public VoidDelegate onDown;
        public VoidDelegate onEnter;
        public VoidDelegate onExit;
        public VoidDelegate onUp;
        public VoidDelegate onMove;
        public VoidDelegate onMoveEnd;
        static public EventTriggerListener Get(GameObject go)
        {
            EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
            if (listener == null) listener = go.AddComponent<EventTriggerListener>();
            return listener;
        }
        //当鼠标抬起时调用__按下抬起在同一物体上
        public void OnPointerClick(PointerEventData eventData)
        {
            if (onClick != null) onClick(eventData);
        }
        //当鼠标按下时调用
        public void OnPointerDown(PointerEventData eventData)
        {
            if (onDown != null) onDown(eventData);
        }
        //当鼠标进入时调用
        public void OnPointerEnter(PointerEventData eventData)
        {
            if (onEnter != null) onEnter(eventData);
        }
        //当鼠标退出时调用
        public void OnPointerExit(PointerEventData eventData)
        {
            if (onExit != null) onExit(eventData);
        }
        //当鼠标抬起时调用
        public void OnPointerUp(PointerEventData eventData)
        {
            if (onUp != null) onUp(eventData);
        }
        //当鼠标拖动时调用
        public void OnDrag(PointerEventData eventData)
        {
            if (onMove != null) onMove(eventData);
        }
        //当鼠标结束拖动时调用
        public void OnEndDrag(PointerEventData eventData)
        {
            if (onMoveEnd != null) onMoveEnd(eventData);
        }
    }
    //***********************
    //这是一个静态类,用委托来传递你想要的事件。比如你再某个类中要监听你的 YourButton。
    //EventTriggerListener.Get(YourButton).onClick = onClickButtonHandler;
    //private void onClickButtonHandler(PointerEventData eventData)
    //{
    //    Debug.log("点击到了你的按钮");
    //}
    

    相关文章

      网友评论

          本文标题:Unity的button监听

          本文链接:https://www.haomeiwen.com/subject/ixxbqftx.html