unity4.6 beta / uGUI ドラッグ編
ソース
UI Example Project | Unity Community
ここのサンプルプロジェクトを見ながら書いてます
uGUIとInterface
uGUI用の Image などのGameObjectに、以下のインターフェースを継承・メソッドを実装したスクリプトを付けると、様々なイベントを拾えるようになる。
例えば、マウスドラッグのイベントを拾うためには3つのインターフェースを継承し、メソッドを実装する必要がある。
void IBeginDragHandler.OnBeginDrag(PointerEventData) void IDragHandler.OnDrag(PointerEventData) void IEndDragHandler.OnEndDrag(PointerEventData)
PointerEventData
PointerEventDataは様々なプロパティを持っている。ドキュメントは無いっぽいのでプロパティの名前から判断するしか無いと思う
namespace UnityEngine.EventSystems { public class PointerEventData : BaseEventData { public int clickCount; public float clickTime; public Vector2 delta; public bool eligibleForClick; public RaycastResult pointerCurrentRaycast; public GameObject pointerDrag; public GameObject pointerEnter; public int pointerId; public GameObject pointerPress; public RaycastResult pointerPressRaycast; public Vector2 position; public Vector2 pressPosition; public GameObject rawPointerPress; public Vector2 scrollDelta; public Vector3 worldNormal; public Vector3 worldPosition; public PointerEventData(EventSystem eventSystem); public Camera enterEventCamera { get; } public Camera pressEventCamera { get; } public bool IsPointerMoving(); public bool IsScrolling(); public override string ToString(); } }
拾うだけの実装例
using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class UguiDragEventReceiver : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public void OnBeginDrag(PointerEventData ped) { Debug.Log("マウスドラッグ開始 position="+ped.position); } public void OnDrag(PointerEventData ped) { Debug.Log("マウスドラッグ中 position="+ped.position); } public void OnEndDrag(PointerEventData ped) { Debug.Log("マウスドラッグ終了 position="+ped.position); } }
からの
からの
できた。応用すればテキストを動かしたりも簡単にできるはず