Unity 拖動UI物體
阿新 • • 發佈:2019-02-03
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"); } }