using UnityEngine; using System.Collections; using UnityEngine.EventSystems; public class RotateImage : MonoBehaviour, IDragHandler { public void OnDrag(PointerEventData eventData) { SetDraggedRotation(eventData); //SetDraggedPosition(eventData); } //旋轉圖片 private void SetDraggedRotation(PointerEventData eventData) { Vector2 curScreenPosition = RectTransformUtility.WorldToScreenPoint(eventData.pressEventCamera, transform.position); Vector2 directionTo = curScreenPosition - eventData.position; Vector2 directionFrom = directionTo - eventData.delta; this.transform.rotation *= Quaternion.FromToRotation(directionTo, directionFrom); } //拖動圖片 private void SetDraggedPosition(PointerEventData eventData) { var rt = gameObject.GetComponent<RectTransform>(); Vector3 globalMousePos; if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out globalMousePos)) { rt.position = globalMousePos; } } }