标签:
也许你也跟我一样。喜欢直接这样拖拽。绑定事件
但实际项目中是不这样的。因为很多对象都是动态添加的。然后动态绑定事件。这样才够灵活。所以收集了下面的代码:
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.EventSystems; 4 5 public class EventClickListener : MonoBehaviour,IPointerClickHandler,IPointerDownHandler,IPointerUpHandler 6 { 7 public delegate void VoidDelegate(GameObject go); 8 public VoidDelegate onClick; 9 public VoidDelegate onDown; 10 public VoidDelegate onUp; 11 public VoidDelegate onLongClick; // 长按事件 12 13 public object parameter; 14 public object parameterLong; 15 public byte testLongClickFlag = 0; // 用于长按检测,当长按过程中同时进行了拖动,我们将取消长按事件 0:未进行长按检测或长按已经取消 1:正在检测 2:长按已触发 16 17 static public EventClickListener Get(GameObject go) 18 { 19 EventClickListener listener = go.GetComponent<EventClickListener>(); 20 if (listener == null) listener = go.AddComponent<EventClickListener>(); 21 return listener; 22 } 23 24 public void OnPointerClick(PointerEventData eventData) 25 { 26 if (testLongClickFlag == 2) return; // 长按已经触发,则取消单击事件 27 if (onClick != null) onClick(gameObject); 28 } 29 30 public void OnPointerDown(PointerEventData eventData) 31 { 32 if (onDown != null) onDown(gameObject); 33 if (onLongClick != null) 34 { 35 testLongClickFlag = 1; 36 Invoke("LongClickFunc", 0.5f); // 长按0.5秒钟 37 } 38 } 39 40 public void OnPointerUp(PointerEventData eventData) 41 { 42 if (onUp != null) onUp(gameObject); 43 if (testLongClickFlag == 1) 44 { 45 testLongClickFlag = 0; 46 CancelInvoke("LongClickFunc"); 47 } 48 } 49 50 private void LongClickFunc() 51 { 52 testLongClickFlag = 2; 53 if (onLongClick != null) onLongClick(gameObject); 54 } 55 }
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.EventSystems; 4 5 public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger 6 { 7 public delegate void VoidDelegate_New(GameObject go, PointerEventData eventData); 8 public delegate void VoidDelegate(GameObject go); 9 public VoidDelegate onClick; 10 public VoidDelegate_New onDrag; 11 public VoidDelegate_New onBeginDrag; 12 public VoidDelegate_New onEndDrag; 13 public VoidDelegate_New onDown; 14 public VoidDelegate onEnter; 15 public VoidDelegate onExit; 16 public VoidDelegate_New onUp; 17 public VoidDelegate onSelect; 18 public VoidDelegate onUpdateSelect; 19 public VoidDelegate onLongClick; // 长按事件 20 21 public object parameter; 22 public byte testLongClickFlag = 0; // 用于长按检测,当长按过程中同时进行了拖动,我们将取消长按事件 0:未进行长按检测或长按已经取消 1:正在检测 2:长按已触发 23 24 static public EventTriggerListener Get(GameObject go) 25 { 26 EventTriggerListener listener = go.GetComponent<EventTriggerListener>(); 27 if (listener == null) listener = go.AddComponent<EventTriggerListener>(); 28 return listener; 29 } 30 public override void OnPointerClick(PointerEventData eventData) 31 { 32 if (testLongClickFlag == 2) return; // 长按已经触发,则取消单击事件 33 if (onClick != null) onClick(gameObject); 34 } 35 public override void OnPointerDown(PointerEventData eventData) 36 { 37 if (onDown != null) onDown(gameObject, eventData); 38 if (onLongClick != null) 39 { 40 testLongClickFlag = 1; 41 Invoke("LongClickFunc", 0.5f); // 长按0.5秒钟 42 } 43 } 44 private void LongClickFunc() 45 { 46 testLongClickFlag = 2; 47 if (onLongClick != null) onLongClick(gameObject); 48 } 49 public override void OnPointerUp(PointerEventData eventData) 50 { 51 if (onUp != null) onUp(gameObject, eventData); 52 if (testLongClickFlag == 1) 53 { 54 testLongClickFlag = 0; 55 CancelInvoke("LongClickFunc"); 56 } 57 } 58 public override void OnPointerEnter(PointerEventData eventData) 59 { 60 if (onEnter != null) onEnter(gameObject); 61 } 62 public override void OnPointerExit(PointerEventData eventData) 63 { 64 if (onExit != null) onExit(gameObject); 65 } 66 public override void OnSelect(BaseEventData eventData) 67 { 68 if (onSelect != null) onSelect(gameObject); 69 } 70 public override void OnUpdateSelected(BaseEventData eventData) 71 { 72 if (onUpdateSelect != null) onUpdateSelect(gameObject); 73 } 74 public override void OnDrag(PointerEventData eventData) 75 { 76 //Core.Log.Trace("OnEndDrag: " + eventData.pressPosition.x + "," + eventData.position.x); 77 if (onDrag != null) onDrag(gameObject, eventData); 78 if (testLongClickFlag == 1) 79 { 80 float dx = Mathf.Abs(eventData.position.x - eventData.pressPosition.x); 81 float dy = Mathf.Abs(eventData.position.y - eventData.pressPosition.y); 82 if (dx > 5 || dy > 5) // 如果只是手指抖动,我们不当拖动处理 83 { 84 testLongClickFlag = 0; 85 CancelInvoke("LongClickFunc"); 86 } 87 } 88 } 89 public override void OnBeginDrag(PointerEventData eventData) 90 { 91 if (onBeginDrag != null) onBeginDrag(gameObject, eventData); 92 } 93 public override void OnEndDrag(PointerEventData eventData) 94 { 95 if (onEndDrag != null) onEndDrag(gameObject, eventData); 96 } 97 }
实际上EventTrigger是继承至IPointerUpHandler接口
1 GameObject message = this.Find("Message").gameObject;//对象 2 EventClickListener.Get(tab).parameter = index;//额外的参数 3 EventClickListener.Get(tab).onClick = DownChangeTab;//回调方法
标签:
原文地址:http://www.cnblogs.com/nsky/p/4988573.html