You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.8 KiB
51 lines
1.8 KiB
11 months ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.EventSystems;
|
||
|
|
||
|
public class ResizePanel : MonoBehaviour, IDragHandler, IPointerDownHandler,IPointerUpHandler
|
||
|
{
|
||
|
public Vector2 minSize = new Vector2(100, 100);
|
||
|
public Vector2 maxSize = new Vector2(400, 400);
|
||
|
public Transform canvas;
|
||
|
public Camera thirdPersonCamera;
|
||
|
private RectTransform _panelRectTransform; //父物体
|
||
|
private Vector2 _originalLocalPointerPos; //初始位置
|
||
|
private Vector2 _originalSizeDelta; //初始大小
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
_panelRectTransform = transform.parent.GetComponent<RectTransform>();
|
||
|
}
|
||
|
|
||
|
public void OnPointerDown(PointerEventData eventData)
|
||
|
{
|
||
|
thirdPersonCamera.GetComponent<ThirdPersonCamera>().enabled = false;
|
||
|
_originalSizeDelta = _panelRectTransform.sizeDelta;
|
||
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas as RectTransform, eventData.position, eventData.pressEventCamera, out _originalLocalPointerPos);
|
||
|
}
|
||
|
|
||
|
public void OnDrag(PointerEventData eventData)
|
||
|
{
|
||
|
if (_panelRectTransform == null)
|
||
|
return;
|
||
|
|
||
|
Vector2 vec;
|
||
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas as RectTransform, eventData.position, eventData.pressEventCamera, out vec);
|
||
|
|
||
|
Vector3 offect = vec - _originalLocalPointerPos;
|
||
|
Vector2 sizeDelta = _originalSizeDelta + new Vector2(offect.x, -offect.y);
|
||
|
sizeDelta = new Vector2(
|
||
|
Mathf.Clamp(sizeDelta.x, minSize.x, maxSize.x),
|
||
|
Mathf.Clamp(sizeDelta.y, minSize.y, maxSize.y)
|
||
|
);
|
||
|
|
||
|
_panelRectTransform.sizeDelta = sizeDelta;
|
||
|
}
|
||
|
|
||
|
public void OnPointerUp(PointerEventData eventData)
|
||
|
{
|
||
|
thirdPersonCamera.GetComponent<ThirdPersonCamera>().enabled = true;
|
||
|
}
|
||
|
}
|