Created
May 24, 2018 03:34
-
-
Save smallrice45/e8a1cb156b88f9910fda296379d5920f to your computer and use it in GitHub Desktop.
Unity UI簡易拖曳跟隨
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.EventSystems; | |
public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger | |
{ | |
public delegate void VoidDelegate(GameObject go); | |
public delegate void VoidDelegatePointer(GameObject go, Vector2 pos); | |
public VoidDelegate onClick; | |
public VoidDelegate onDown; | |
public VoidDelegate onEnter; | |
public VoidDelegate onExit; | |
public VoidDelegate onUp; | |
public VoidDelegate onSelect; | |
public VoidDelegate onUpdateSelect; | |
public VoidDelegatePointer onBeginDrag; | |
public VoidDelegatePointer onDrag; | |
public VoidDelegatePointer onEndDrag; | |
static public EventTriggerListener Get(GameObject go) | |
{ | |
EventTriggerListener listener = go.GetComponent<EventTriggerListener>(); | |
if (listener == null) listener = go.AddComponent<EventTriggerListener>(); | |
return listener; | |
} | |
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 OnPointerEnter(PointerEventData eventData) | |
{ | |
if (onEnter != null) onEnter(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 OnSelect(BaseEventData eventData) | |
{ | |
if (onSelect != null) onSelect(gameObject); | |
} | |
public override void OnUpdateSelected(BaseEventData eventData) | |
{ | |
if (onUpdateSelect != null) onUpdateSelect(gameObject); | |
} | |
public override void OnBeginDrag(PointerEventData eventData) | |
{ | |
if (onBeginDrag != null) onBeginDrag(gameObject, eventData.position); | |
} | |
public override void OnDrag(PointerEventData eventData) | |
{ | |
if (onDrag != null) onDrag(gameObject, eventData.position); | |
} | |
public override void OnEndDrag(PointerEventData eventData) | |
{ | |
if (onEndDrag != null) onEndDrag(gameObject, eventData.position); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class TestEvent : MonoBehaviour { | |
void Start () { | |
EventTriggerListener.Get(gameObject).onBeginDrag = OnBeginDrag; | |
EventTriggerListener.Get(gameObject).onDrag = OnDrag; | |
EventTriggerListener.Get(gameObject).onEndDrag = OnEndDrag; | |
} | |
void OnBeginDrag(GameObject go, Vector2 pos) { | |
Debug.Log("OnBeginDrag" + pos); | |
} | |
void OnDrag(GameObject go, Vector2 pos) | |
{ | |
Debug.Log("OnBegin" + pos); | |
go.transform.position = pos; | |
} | |
void OnEndDrag(GameObject go, Vector2 pos) | |
{ | |
Debug.Log("OnBeginEnd" + pos); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment