天天看点

UGUI控件可拖拽移动类组件

由于Unity3d自带的UGUI不带拖拽功能,想要实现拖拽功能,必须自己实现拖拽类.所以我写了一个通用的UGUI拖拽组件,使用方便.

使用方法:

直接放到要拖拽的UI组件上,设置目标移动的对象即可,也可以不设置目标对象,默认是拖动对象自己.

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


public class DragBase : MonoBehaviour,IPointerDownHandler,IDragHandler{

	private Vector2 Local_Pointer_Position;
	private Vector3 Panel_Local_Position;
	public RectTransform targetObject; 
	private RectTransform parentRectTransform;  //父窗口矩阵
	private RectTransform targetRectTransform; //移动目标矩阵

	void Awake()
	{
		if (targetObject == null) {
			targetObject=transform as RectTransform;
		}
	
		parentRectTransform = targetObject.parent as RectTransform;
		targetRectTransform=targetObject as RectTransform;

	}

	public void OnPointerDown(PointerEventData data)
	{
		Panel_Local_Position = targetRectTransform.localPosition;
		RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, data.position, data.pressEventCamera, out Local_Pointer_Position);
		targetObject.gameObject.transform.SetAsLastSibling();//保证当前操作的对象能够优先渲染,即不会被其它对象遮挡住


	}




	public void OnDrag(PointerEventData data)
	{

		Vector2 localPointerPosition;
		if (RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, data.position, data.pressEventCamera, out localPointerPosition))
		{
			Vector3 offsetToOriginal = localPointerPosition - Local_Pointer_Position;

			targetObject.localPosition = Panel_Local_Position + offsetToOriginal;
		}

	}




}
           

以下是一个背包拖拽物品基础类,可以修改了变成可拖拽背包.

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


public class DragItem : MonoBehaviour,IPointerDownHandler,IDragHandler,IBeginDragHandler,IEndDragHandler {
	
	private Vector2 Local_Pointer_Position;
	private Vector3 Panel_Local_Position;
	public RectTransform targetObject; 
	private RectTransform parentRectTransform;  //父窗口矩阵
	private RectTransform targetRectTransform; //移动目标矩阵
	private CanvasGroup canvasgroup;
	void Awake()
	{
		if (targetObject == null) {
			targetObject=transform as RectTransform;
		}
		//	CanvasGroup
		canvasgroup=GetComponent<CanvasGroup>();
		if(!canvasgroup)
			canvasgroup=gameObject.AddComponent("CanvasGroup")as CanvasGroup;
		//panelRectTransform = transform.parent as RectTransform;
		parentRectTransform = targetObject.parent as RectTransform;
		targetRectTransform=targetObject as RectTransform;

		
	}
	
	public void OnPointerDown(PointerEventData data)
	{
		Panel_Local_Position = targetRectTransform.localPosition;
		RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, data.position, data.pressEventCamera, out Local_Pointer_Position);
		targetObject.gameObject.transform.SetAsLastSibling();//保证当前操作的对象能够优先渲染,即不会被其它对象遮挡住
	}
	
	public void OnBeginDrag (PointerEventData eventData)
	{
		canvasgroup.blocksRaycasts = false;
	}
	
	
	public void OnDrag(PointerEventData data)
	{
		//if (panelRectTransform == null || parentRectTransform == null)
		//	return;
		//print (data.position);
		//检测到拖拽物品的下放物品.
		if (data.pointerEnter != null) {
			if(data.pointerEnter.name!="test")
				print (data.pointerEnter.name);
		}
		
		
		Vector2 localPointerPosition;
		if (RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, data.position, data.pressEventCamera, out localPointerPosition))
		{
			Vector3 offsetToOriginal = localPointerPosition - Local_Pointer_Position;
			
			targetObject.localPosition = Panel_Local_Position + offsetToOriginal;
		}
		
	}
	
	public void OnEndDrag (PointerEventData eventData){
		canvasgroup.blocksRaycasts = true;
	}
	
	
}
           

继续阅读