美文网首页
UGUI里的EventListener

UGUI里的EventListener

作者: 迷途小路 | 来源:发表于2019-01-07 16:05 被阅读0次

    EventTrigger

    我们知道Button可以添加点击监听,Slider可以滑动,假如我们想给Image添加点击监听,或者更多的监听事件呢,比如选中、鼠标移入、拖动。我们可以用到EventTrigger。EventTrigger里继承了很多接口,正好可以用来封装

    public class EventTriggerTest : EventTrigger {
    
        public override void OnPointerClick (PointerEventData eventData) {
            Debug.Log ("OnPointerClick");
        }
    
        public override void OnPointerEnter (PointerEventData eventData) {
            Debug.Log ("OnPointerEnter");
        }
    
        public override void OnPointerExit (PointerEventData eventData) {
            Debug.Log ("OnPointerExit");
        }
    
    }
    

    这里重写了EventTrigger里的3个方法,分别是点击、鼠标移入、鼠标移出,我们把它挂载在一个Image上,运行,可以看到,根据操作,确实可以执行对应的方法。


    image.png

    EventListener

    要是有很多控件都需要绑定,那就需要在里面都挂上这个脚本,每个脚本都在对应的方法下重写内容,这样显得过于麻烦,我们可以就写一个脚本,继承自EventTrigger,通过委托的形式,触发EventTrigger下的事件是就执行委托的方法,具体的方法内容在各自的脚本里去实现就可以了。

    
    public class UIEventListener : EventTrigger {
        public delegate void VoidDelegate (GameObject go);
        public delegate void BoolDelegate (GameObject go, bool isValue);
        public delegate void FloatDelegate (GameObject go, float fValue);
        public delegate void IntDelegate (GameObject go, int iIndex);
        public delegate void StringDelegate (GameObject go, string strValue);
    
        public VoidDelegate onSubmit;
        public VoidDelegate onClick;
        public BoolDelegate onHover;
        public BoolDelegate onToggleChanged;
        public FloatDelegate onSliderChanged;
        public FloatDelegate onScrollbarChanged;
        public IntDelegate onDrapDownChanged;
        public StringDelegate onInputFieldChanged;
    
        public override void OnSubmit (BaseEventData eventData) {  //提交
            if (onSubmit != null)
                onSubmit (gameObject);
        }
        public override void OnPointerEnter (PointerEventData eventData) { //鼠标移入
            if (onHover != null)
                onHover (gameObject, true);
        }
        public override void OnPointerClick (PointerEventData eventData) { //点击
            if (onClick != null)
                onClick (gameObject);
            if (onToggleChanged != null)
                onToggleChanged (gameObject, gameObject.GetComponent<Toggle> ().isOn);
    
        }
        public override void OnPointerExit (PointerEventData eventData) { //鼠标移出
            if (onHover != null)
                onHover (gameObject, false);
        }
        public override void OnDrag (PointerEventData eventData) { //拖动
            if (onSliderChanged != null)
                onSliderChanged (gameObject, gameObject.GetComponent<Slider> ().value);
            if (onScrollbarChanged != null)
                onScrollbarChanged (gameObject, gameObject.GetComponent<Scrollbar> ().value);
    
        }
        public override void OnSelect (BaseEventData eventData) { //选中
            if (onDrapDownChanged != null)
                onDrapDownChanged (gameObject, gameObject.GetComponent<Dropdown> ().value);
        }
        public override void OnUpdateSelected (BaseEventData eventData) { //选中、每帧更新
            if (onInputFieldChanged != null)
                onInputFieldChanged (gameObject, gameObject.GetComponent<InputField> ().text);
        }
        public override void OnDeselect (BaseEventData eventData) { //不选中
            if (onInputFieldChanged != null)
                onInputFieldChanged (gameObject, gameObject.GetComponent<InputField> ().text);
        }
    
        public static UIEventListener Get (GameObject go) {
            UIEventListener listener = go.GetComponent<UIEventListener> ();
            if (listener == null) listener = go.AddComponent<UIEventListener> ();
            return listener;
        }
    }
    

    试一下把,我们还是实现上面的功能,现在不用手动去挂载专门的脚本,只要在Image的执行脚本里添加就可以了,运行后程序会自动添加上UIEventListener.cs。

    public class Test1 : MonoBehaviour {
    
        // Use this for initialization
        void Start () {
            UIEventListener.Get (gameObject).onClick = OnClick;
            UIEventListener.Get (gameObject).onHover = OnHover;
        }
    
        // Update is called once per frame
        void Update () { }
    
        void OnClick (GameObject go) {
            Debug.Log ("OnPointerClick");
        }
    
        void OnHover (GameObject go, bool _flag) {
            if (_flag) {
                Debug.Log ("OnPointerEnter");
            } else {
                Debug.Log ("OnPointerExit");
            }
        }
    
    }
    
    image.png

    不足之处,多谢指正。

    相关文章

      网友评论

          本文标题:UGUI里的EventListener

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