UGUI 图片拖动旋转和拖动移动

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;
        }
    }

}

猜你喜欢

转载自my.oschina.net/u/698044/blog/846344