一. 节点参考:
拖动的脚本放在子Panel上(需要一个父Panel)
修改大小脚本放在Panel下的Resize节点上
二. 拖动Panel
- 注意点:
- 此处的RectTransform.Rect.min为矩形左上角点相对pivot即中心点的位置,max为右下角,详细参考:https://docs.unity3d.com/ScriptReference/Rect.html
- 代码:
using UnityEngine;
using UnityEngine.EventSystems;
public class DragMovePanel : MonoBehaviour, IPointerDownHandler, IDragHandler
{
private Vector2 originalLocalPointerPosition;
private Vector3 originalPanelLocalPosition;
private RectTransform panelRectTransform;
private RectTransform parentRectTransform;
void Awake()
{
panelRectTransform = transform.parent as RectTransform;
parentRectTransform = panelRectTransform.parent as RectTransform;
}
public void OnPointerDown(PointerEventData data)
{
//计算Panel本地位置
originalPanelLocalPosition = panelRectTransform.localPosition;
//计算鼠标点击在Panel父Panel的位置
RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition);
}
public void OnDrag(PointerEventData data)
{
if (panelRectTransform == null || parentRectTransform == null)
return;
Vector2 localPointerPosition;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, data.position, data.pressEventCamera, out localPointerPosition))
{
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;//计算鼠标偏移
panelRectTransform.localPosition = originalPanelLocalPosition + offsetToOriginal;
}
ClampToWindow();
}
// Clamp panel to area of parent
void ClampToWindow()
{
//参考:https://docs.unity3d.com/ScriptReference/Rect.html
//以Pivot为中心,min是左上角相对Pivot的坐标,max是右下角相对Pivot的坐标
Vector3 pos = panelRectTransform.localPosition;
Vector3 minPosition = parentRectTransform.rect.min - panelRectTransform.rect.min;//相减为两中心点的向量
Vector3 maxPosition = parentRectTransform.rect.max - panelRectTransform.rect.max;
//Debug.Log("panelRectTransform.rect.min= " + panelRectTransform.rect.min);
//Debug.Log("panelRectTransform.rect.max = " + panelRectTransform.rect.max);
pos.x = Mathf.Clamp(panelRectTransform.localPosition.x, minPosition.x, maxPosition.x);
pos.y = Mathf.Clamp(panelRectTransform.localPosition.y, minPosition.y, maxPosition.y);
panelRectTransform.localPosition = pos;
}
}
三. 修改Panel大小
- 注意点
-
sizeDelta:
- 为Vector2类型
- 如果锚点在一起,那么sizeDelta为矩形的大小,即右边Inspector中Width和Heigh
- 如果锚点分开,那么sizeDelta表示为矩形比锚点组成的矩形大小多少,数值为右边Left+Right,Top+Buttom的绝对值
- 代码:
using UnityEngine;
using UnityEngine.EventSystems;
public class ResizePanel : MonoBehaviour, IPointerDownHandler, IDragHandler
{
public Vector2 minSize = new Vector2(100, 100);
public Vector2 maxSize = new Vector2(400, 400);
private RectTransform panelRectTransform;
private Vector2 originalLocalPointerPosition;
private Vector2 originalSizeDelta;
void Awake()
{
panelRectTransform = transform.parent.GetComponent<RectTransform>();
}
public void OnPointerDown(PointerEventData data)
{
originalSizeDelta = panelRectTransform.sizeDelta;
RectTransformUtility.ScreenPointToLocalPointInRectangle(panelRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition);
}
public void OnDrag(PointerEventData data)
{
if (panelRectTransform == null)
return;
//鼠标偏移量
Vector2 localPointerPosition;
RectTransformUtility.ScreenPointToLocalPointInRectangle(panelRectTransform, data.position, data.pressEventCamera, out localPointerPosition);
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
Vector2 sizeDelta = originalSizeDelta + new Vector2(offsetToOriginal.x, -offsetToOriginal.y);
sizeDelta = new Vector2(
Mathf.Clamp(sizeDelta.x, minSize.x, maxSize.x),
Mathf.Clamp(sizeDelta.y, minSize.y, maxSize.y)
);
panelRectTransform.sizeDelta = sizeDelta;
}
}