(NGUI)监听事件方法

  1 using UnityEngine;
  2 
  3 namespace UI
  4 {
  5     public class UIEventListener : MonoBehaviour
  6     {
  7         public int id;
  8 
  9         public delegate void SubmitDelegate();
 10         public event SubmitDelegate onSubmit;
 11         void OnSubmit()
 12         {
 13             if (onSubmit != null)
 14                 onSubmit();
 15         }
 16 
 17         public delegate void ClickDelegate();
 18         public event ClickDelegate onClick;
 19         public delegate void ClickDelegateEX(int id);
 20         public event ClickDelegateEX onClickEX;
 21         /// <summary>
 22         /// 鼠标或触摸单击(按下并释放)时触发。
 23         /// </summary>
 24         void OnClick()
 25         {
 26             if (onClick != null)
 27                 onClick();
 28 
 29             if (onClickEX != null)
 30                 onClickEX(id);
 31         }
 32 
 33         public delegate void DoubleClickDelegate();
 34         public event DoubleClickDelegate onDoubleClick;
 35         /// <summary>
 36         /// 双击(双击时间间隔小于0.25秒)时触发。
 37         /// </summary>
 38         void OnDoubleClick()
 39         {
 40             if (onDoubleClick != null)
 41                 onDoubleClick();
 42         }
 43 
 44         public delegate void HoverDelegates(bool isOver);
 45         public event HoverDelegates onHover;
 46         /// <summary>
 47         /// 鼠标悬停或移出时触发。悬停时传入true,移出时传入false。
 48         /// </summary>
 49         /// <param name="isOver"></param>
 50         void OnHover(bool isOver)
 51         {
 52             if (onHover != null)
 53                 onHover(isOver);
 54         }
 55 
 56         public delegate void PressDelegate(bool isPressed);
 57         public event PressDelegate onPress;
 58         /// <summary>
 59         /// 鼠标或触摸按下或松开时触发,按下时传入true,松开时传入false。
 60         /// </summary>
 61         /// <param name="isPressed"></param>
 62         void OnPress(bool isPressed)
 63         {
 64             if (onPress != null)
 65                 onPress(isPressed);
 66         }
 67 
 68         public delegate void SelectDelegate(bool selected);
 69         public event SelectDelegate onSelect;
 70         /// <summary>
 71         /// 类似单击,区别在于选中一次之后再选中将不再触发OnSelect事件,除非期间选择了其他控件。 
 72         /// </summary>
 73         /// <param name="selected"></param>
 74         void OnSelect(bool selected)
 75         {
 76             if (onSelect != null)
 77                 onSelect(selected);
 78         }
 79 
 80         public delegate void ScrollDelegate(float delta);
 81         public event ScrollDelegate onScroll;
 82         /// <summary>
 83         /// 鼠标中键滚动时触发,delta为传入的滚动增量。
 84         /// </summary>
 85         /// <param name="delta"></param>
 86         void OnScroll(float delta)
 87         {
 88             if (onScroll != null)
 89                 onScroll(delta);
 90         }
 91 
 92         public delegate void VectorDelegate(Vector2 delta);
 93         public event VectorDelegate onDrag;
 94         /// <summary>
 95         /// 鼠标或触摸按下并移动时触发。delta为传入的位移。
 96         /// </summary>
 97         /// <param name="delta"></param>
 98         void OnDrag(Vector2 delta)
 99         {
100             if (onDrag!=null)
101             {
102                 onDrag(delta);
103             }
104         }
105 
106         public delegate void InputDelegate(string text);
107         public event InputDelegate onInput;
108         /// <summary>
109         /// 只用于输入控件,每次输入完成后触发,text传入本次输入的信息,而非输入控件中的文本信息。
110         /// </summary>
111         /// <param name="text"></param>
112         void OnInput(string text)
113         {
114             if (onInput != null)
115                 onInput(text);
116         }
117 
118 
119         public delegate void DropDelegate(GameObject go);
120         public event DropDelegate onDrop;
121         /// <summary>
122         /// 鼠标OnDrag后移入不同对象释放时触发.go是开始触发OnDrag的对象.
123         /// </summary>
124         /// <param name="go"></param>
125         void OnDrop(GameObject go)
126         {
127             if (onDrop != null)
128                 onDrop(go);
129         }
130 
131         public delegate void KeyCodeDelegate(KeyCode key);
132         public event KeyCodeDelegate onKey;
133         void OnKey(KeyCode key)
134         {
135             if (onKey != null)
136                 onKey(key);
137         }
138 
139         public delegate void TooltipDelegate(bool show);
140         public event TooltipDelegate onTooltip;
141         /// <summary>
142         /// 鼠标悬停一段时间或移开时触发,悬停时传入true,移开时传入false。
143         /// </summary>
144         /// <param name="show"></param>
145         void OnTooltip(bool show)
146         {
147             if (onTooltip!=null)
148             {
149                 onTooltip(show);
150             }
151         }
152         
153         static public UIEventListener Get(GameObject go)
154         {
155             UIEventListener listener = go.GetComponent<UIEventListener>();
156             if (listener == null) listener = go.AddComponent<UIEventListener>();
157             return listener;
158         }
159     }
160 }
原文地址:https://www.cnblogs.com/gamerLin/p/4441339.html