1. 程式人生 > >Unity 拖動UI物體

Unity 拖動UI物體

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class Tool : MonoBehaviour, IDragHandler,IPointerDownHandler,IPointerUpHandler{

	// Use this for initialization
	void Start () {
	}

	// Update is called once per frame
	void Update () {
	}

	public void OnPointerDown(PointerEventData eventDate) {
		Debug.Log ("start");
	}

	public void OnDrag (PointerEventData eventData) {
        GameObject pointerDrag = eventData.pointerDrag;

		Vector3 globalMousePosition;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(pointerDrag.GetComponent<RectTransform>(), eventData.position, Camera.main, out globalMousePosition))
        {
            pointerDrag.transform.position = globalMousePosition;
		} 
	}

	public void OnPointerUp(PointerEventData eventData) {
		Debug.Log ("end");
	}
}