效果可以看下面的圖必怜,實(shí)現(xiàn)窗口的拖拽放大縮小
設(shè)置panel的瞄點(diǎn)為左上角,設(shè)置point值 X:0吩案,Y:1棚赔,按鈕的圖右下角放置,瞄點(diǎn)為panel的右下角徘郭,按鈕掛載當(dāng)前腳本靠益,賦值panel對(duì)象即可
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// unity實(shí)現(xiàn)窗口的放大縮小
/// </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;
}
}