UGUI 컨트롤 이동식 클래스 구성 요소 드래그

3708 단어 Unity3d
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();
		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;
	}
	
	
}

좋은 웹페이지 즐겨찾기