EventListener

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 }

 

转载于:https://www.cnblogs.com/diantujiang/p/6973844.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值