Unity5において,UGUI(PanelとかButtonなど)をドラッグで移動させます.
通常のGameObjectだとググレばすぐ出てくるのですが,GUIを動かすとなるとちょっと困りました
ソースはこれです
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections; public class Drop : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler{ // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void OnBeginDrag(PointerEventData eventData) { } public void OnDrag(PointerEventData eventData) { GetComponent<RectTransform>().position += new Vector3(eventData.delta.x, eventData.delta.y, 0.0f); } public void OnEndDrag(PointerEventData eventData) { } }
ポイントとしては
・I~Handlerの継承
・ドラッグ中のイベント「OnDrag」内で,移動量eventData.deltaを足していく
です
【追記】
GetComponent