在NGUI中,我们可以很容易的对UI事件进行监听,点击,拖动等等……
那UGUI中,Unity却还没有给我们很好的封装使用。
那我们就自己来封装监听事件。
https://docs.unity3d.com/460/Documentation/Manual/SupportedEvents.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
using UnityEngine; using System.Collections; using UnityEngine.EventSystems; public class EventTriggerListener : EventTrigger{ public delegate void VoidDelegate(GameObject go); public VoidDelegate onEnter; public VoidDelegate onClick; public VoidDelegate onDown; public VoidDelegate onExit; public VoidDelegate onUp; public VoidDelegate onBeginDrag; public VoidDelegate onDrag; public VoidDelegate onEndDrag; public VoidDelegate onSelect; public VoidDelegate onUpdateSelect; static public EventTriggerListener Get (GameObject go) { EventTriggerListener listener = go.GetComponent<EventTriggerListener>(); if (listener == null) listener = go.AddComponent<EventTriggerListener>(); return listener; } public override void OnPointerEnter(PointerEventData eventData) { if (onEnter != null) onEnter(gameObject); } public override void OnPointerClick(PointerEventData eventData) { if(onClick != null) onClick(gameObject); } public override void OnPointerDown (PointerEventData eventData){ if(onDown != null) onDown(gameObject); } public override void OnPointerExit (PointerEventData eventData){ if(onExit != null) onExit(gameObject); } public override void OnPointerUp (PointerEventData eventData){ if(onUp != null) onUp(gameObject); } public override void OnBeginDrag(PointerEventData eventData) { if (onBeginDrag != null) onBeginDrag(gameObject); } public override void OnDrag(PointerEventData eventData) { if (onDrag != null) onDrag(gameObject); } public override void OnEndDrag(PointerEventData eventData) { if (onEndDrag != null) onEndDrag(gameObject); } public override void OnSelect(BaseEventData eventData) { if (onSelect != null) onSelect(gameObject); } public override void OnUpdateSelected(BaseEventData eventData) { if (onUpdateSelect != null) onUpdateSelect(gameObject); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using UnityEngine; using System.Collections; using UnityEngine.UI; public class Test : MonoBehaviour { [SerializeField] Image myImage; // Use this for initialization void Start () { EventTriggerListener.Get(myImage.gameObject).onClick = OnClick; } void OnClick(GameObject go) { Debug.Log(go.name); } } |
- 本文固定链接: http://www.u3d8.com/?p=830
- 转载请注明: 网虫虫 在 u3d8.com 发表过
0.0