1. 程式人生 > >UGUI控制元件可拖拽移動類元件

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