Unity3D实现UGUI 图片拖拽旋转和拖拽移动

拖拽旋转

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class RotateImage : MonoBehaviour, IDragHandler
{
	public void OnDrag(PointerEventData eventData)
	{
	  //拖拽旋转图片
	  SetDraggedRotation(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);
	}
}
拖拽移动

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class MoveImage : MonoBehaviour, IDragHandler
{
	public void OnDrag(PointerEventData eventData)
	{
	  //拖拽移动图片
	  SetDraggedPosition(eventData);
	}
	
	private void SetDraggedPosition(PointerEventData eventData)
	{
		var rt = gameObject.GetComponent();
		Vector3 globalMousePos;
		if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position,eventData.pressEventCamera, out globalMousePos))
		{
		  rt.position = globalMousePos;
		}
	}
}



你可能感兴趣的:(Unity3D)