Unity制作可拖动的弹窗

对于软件中弹出的窗口,要求可以进行拖动,需在弹窗上挂载此脚本。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class DragPanel:MonoBehaviour,IDragHandler,IPointerDownHandler
{
   private RectTransform dargTarget;
   private Canvas canvas;
   
   void Start()
   {
   		dragTarget = this.GetComponent<RectTransform>();
   		canvas = GameObject.Find("Canvas").GetComponenet<Canvas>();
   }

	public void OnDrag(PointerEventData eventData)
	{
		dragTarget.anchoredPosition+=eventData.delta/canvas.scaleFactor;
	}

	public void OnPointerDown(PointerEventData eventData)
	{
		dragTarget.SetAsLastSibling();
	}
}

将此类挂载到需要拖拽的面板上,然后就可以拖拽你的弹窗了。

你可能感兴趣的:(unity)