UGUI事件监听

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UGUI事件监听相关的知识,希望对你有一定的参考价值。

思路是NGUI的监听方法方便在代码中添加监听:

 1 using UnityEngine;
 2 using UnityEngine.UI;
 3 using UnityEngine.EventSystems;
 4 
 5 [SLua.CustomLuaClass]
 6 public class UIEventListener : EventTrigger 
 7 {
 8     public delegate void VoidDelegate(GameObject go);
 9     public delegate void DataDelegate(GameObject go, PointerEventData data);
10     public delegate void BaseDataDelegate(GameObject go, BaseEventData data);
11     public delegate void AxisDataDelegate(GameObject go, AxisEventData data);
12 
13     public VoidDelegate onClick;
14     public VoidDelegate onDown;
15     public VoidDelegate onEnter;
16     public VoidDelegate onExit;
17     public VoidDelegate onUp;
18     public VoidDelegate onSelect;
19     public VoidDelegate onUpdateSelect;
20 
21     public DataDelegate onBeginDrag;
22     public DataDelegate onDrag;
23     public DataDelegate onDrop;
24     public DataDelegate onEndDrag;
25     public DataDelegate onScroll;
26 
27     public AxisDataDelegate onMove;
28     public BaseDataDelegate onSubmit;
29     public BaseDataDelegate onCancel;
30 
31     static public UIEventListener Get(GameObject go)
32     {
33         UIEventListener listener = go.GetComponent<UIEventListener>();
34         if (listener == null) listener = go.AddComponent<UIEventListener>();
35         return listener;
36     }
37     public override void OnPointerClick(PointerEventData eventData)
38     {
39         if (onClick != null) onClick(gameObject);
40     }
41     public override void OnPointerDown(PointerEventData eventData)
42     {
43         if (onDown != null) onDown(gameObject);
44     }
45     public override void OnPointerEnter(PointerEventData eventData)
46     {
47         if (onEnter != null) onEnter(gameObject);
48     }
49     public override void OnPointerExit(PointerEventData eventData)
50     {
51         if (onExit != null) onExit(gameObject);
52     }
53     public override void OnPointerUp(PointerEventData eventData)
54     {
55         if (onUp != null) onUp(gameObject);
56     }
57     public override void OnSelect(BaseEventData eventData)
58     {
59         if (onSelect != null) onSelect(gameObject);
60     }
61     public override void OnUpdateSelected(BaseEventData eventData)
62     {
63         if (onUpdateSelect != null) onUpdateSelect(gameObject);
64     }
65     public override void OnBeginDrag(PointerEventData eventData)
66     {
67         if (onBeginDrag != null) onBeginDrag(gameObject,eventData);
68     }
69     public override void OnDrag(PointerEventData eventData)
70     {
71         if (onDrag != null) onDrag(gameObject, eventData);
72     }
73     public override void OnCancel(BaseEventData eventData)
74     {
75         if (onCancel != null) onCancel(gameObject, (BaseEventData)eventData);
76     }
77     public override void OnDrop(PointerEventData eventData)
78     {
79         if (onDrop != null) onDrop(gameObject, eventData);
80     }
81     public override void OnEndDrag(PointerEventData eventData)
82     {
83         if (onEndDrag != null) onEndDrag(gameObject, eventData);
84     }
85     public override void OnScroll(PointerEventData eventData)
86     {
87         if (onEndDrag != null) onEndDrag(gameObject, eventData);
88     }
89     public override void OnSubmit(BaseEventData eventData)
90     {
91         if (onSubmit != null) onSubmit(gameObject, eventData);
92     }
93     public override void OnMove(AxisEventData eventData)
94     {
95         if (onMove != null) onMove(gameObject, eventData);
96     }
97 }

 

以上是关于UGUI事件监听的主要内容,如果未能解决你的问题,请参考以下文章

UGUI中鼠标点击进入等事件的监听

在同一个片段中实现多个事件监听器 - Android

UGUI优化总结

unity ugui 怎么制作下拉动态刷新

Unity中的事件监听

JavaFX窗口拖动