效果可以看下面的图,实现窗口的拖拽放大缩小
设置panel的瞄点为左上角,设置point值 X:0,Y:1,按钮的图右下角放置,瞄点为panel的右下角,按钮挂载当前脚本,赋值panel对象即可
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// unity实现窗口的放大缩小
/// </summary>
public class ResizePanel : MonoBehaviour, IPointerDownHandler, IDragHandler
{
// 控制的面板
public RectTransform panelRectTransform;
// 最小缩放
public Vector2 minSize = new Vector2(120, 120);
// 最大缩放
public Vector2 maxSize = new Vector2(800, 600);
private Vector2 originalLocalPointerPosition;
private Vector2 originalSizeDelta;
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;
}
}