UGUI的EventSystem事件
1 using UnityEngine; 2 using UnityEngine.EventSystems; 3 using System; 4 5 public class EventListener : EventTrigger 6 { 7 public delegate void OnEvent(); 8 public Action onClick; 9 public Action onDoubleClick; 10 public Action onLeftUp; 11 public Action onRightUp; 12 public Action onMiddleUp; 13 public Action onLeftDown; 14 public Action onRightDown; 15 public Action onMiddleDown; 16 public Action onEnter; 17 public Action onExit; 18 public Action onInitializePotentialDrag; 19 public Action onBeginDrag; 20 public Action onDrop; 21 public Action<Vector3> onDrag; 22 public Action<GameObject,GameObject> onEndDrag; 23 public Action<Vector2> onScroll; 24 public Action onSelect; 25 public Action onUpdateSelected; 26 public Action onSubmit; 27 public Action onCancel; 28 public Action onMove; 29 public Action onDeselect; 30 31 static public EventListener Get(GameObject go) 32 { 33 EventListener listener = go.GetComponent<EventListener>(); 34 if (listener == null) listener = go.AddComponent<EventListener>(); 35 return listener; 36 } 37 38 //点击事件 39 public override void OnPointerClick(PointerEventData eventData) 40 { 41 EventSystem.current.SetSelectedGameObject(gameObject); 42 if (eventData.button == PointerEventData.InputButton.Left) 43 { 44 if (eventData.clickCount == 1) 45 { 46 if (onClick != null) onClick(); 47 } 48 else if (eventData.clickCount == 2) 49 { 50 if (onDoubleClick != null) onDoubleClick(); 51 } 52 } 53 } 54 55 //鼠标抬起事件 56 public override void OnPointerUp(PointerEventData eventData) 57 { 58 switch (eventData.button) 59 { 60 case PointerEventData.InputButton.Left: 61 if (onLeftUp != null) onLeftUp();break; 62 case PointerEventData.InputButton.Right: 63 if (onRightUp != null) onRightUp();break; 64 case PointerEventData.InputButton.Middle: 65 if (onRightUp != null) onMiddleUp(); break; 66 } 67 } 68 69 //鼠标按下事件 70 public override void OnPointerDown(PointerEventData eventData) 71 { 72 EventSystem.current.SetSelectedGameObject(gameObject); 73 switch (eventData.button) 74 { 75 case PointerEventData.InputButton.Left: 76 if (onLeftDown != null) onLeftDown(); break; 77 case PointerEventData.InputButton.Right: 78 if (onRightDown != null) onRightDown(); break; 79 case PointerEventData.InputButton.Middle: 80 if (onMiddleDown != null) onMiddleDown(); break; 81 } 82 } 83 84 //鼠标进入事件 85 public override void OnPointerEnter(PointerEventData eventData) 86 { 87 if (onEnter != null) 88 onEnter(); 89 } 90 91 //输入离开事件 92 public override void OnPointerExit(PointerEventData eventData) 93 { 94 if (onExit != null) 95 onExit(); 96 } 97 98 //可能发生拖拽 99 public override void OnInitializePotentialDrag(PointerEventData eventData) 100 { 101 if (onInitializePotentialDrag != null) 102 onInitializePotentialDrag(); 103 } 104 105 //开始拖拽事件 106 public override void OnBeginDrag(PointerEventData eventData) 107 { 108 if (onBeginDrag != null) 109 onBeginDrag(); 110 } 111 112 //正在拖拽事件 113 public override void OnDrag(PointerEventData eventData) 114 { 115 if (onDrag != null) 116 onDrag(eventData.position); 117 } 118 119 //拖拽结束【在拖拽结束的对象上发生】 120 public override void OnDrop(PointerEventData eventData) 121 { 122 if (onDrop != null) 123 onDrop(); 124 } 125 126 //结束拖拽事件 127 public override void OnEndDrag(PointerEventData eventData) 128 { 129 if (onEndDrag != null) 130 onEndDrag(gameObject, eventData.pointerEnter); 131 } 132 133 //滚轮事件 134 public override void OnScroll(PointerEventData eventData) 135 { 136 if (onScroll != null) 137 onScroll(eventData.scrollDelta); 138 } 139 140 //选中事件 141 public override void OnSelect(BaseEventData eventData) 142 { 143 if (onSelect != null) 144 onSelect(); 145 } 146 147 //选中事件每帧事件 148 public override void OnUpdateSelected(BaseEventData eventData) 149 { 150 if (onUpdateSelected != null) 151 onUpdateSelected(); 152 } 153 154 //点击Submit键(默认是Enter键) 155 public override void OnSubmit(BaseEventData eventData) 156 { 157 if (onSubmit != null) 158 onSubmit(); 159 } 160 161 //点击Cancel键(默认是Esc键) 162 public override void OnCancel(BaseEventData eventData) 163 { 164 if (onCancel != null) 165 onCancel(); 166 } 167 168 //点击方向键 169 public override void OnMove(AxisEventData eventData) 170 { 171 if (onMove != null) 172 onMove(); 173 } 174 175 //不再选中对象 176 public override void OnDeselect(BaseEventData eventData) 177 { 178 if (onDeselect != null) 179 onDeselect(); 180 } 181 }