Unity3d从右下角用鼠标调整面板大小

时间:2018-01-04 20:24:43

标签: user-interface unity3d resize

我想用鼠标调整UI面板的大小。我自己也不知道怎么做。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

这是一个简短的问题,所以我不确定你是否想要任何非常具体的内容,但这是我在Unity Tutorials Website上找到的内容。似乎是你在寻找的东西:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ResizePanel : MonoBehaviour, IPointerDownHandler, IDragHandler {

    public Vector2 minSize;
    public Vector2 maxSize;

    private RectTransform rectTransform;
    private Vector2 currentPointerPosition;
    private Vector2 previousPointerPosition;

    void Awake () {
        rectTransform = transform.parent.GetComponent<RectTransform>();
    }

    public void OnPointerDown (PointerEventData data) {
        rectTransform.SetAsLastSibling();
        RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out previousPointerPosition);
    }

    public void OnDrag (PointerEventData data) {
        if (rectTransform == null)
            return;

        Vector2 sizeDelta = rectTransform.sizeDelta;

        RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out currentPointerPosition);
        Vector2 resizeValue = currentPointerPosition - previousPointerPosition;

        sizeDelta += new Vector2 (resizeValue.x, -resizeValue.y);
        sizeDelta = new Vector2 (
            Mathf.Clamp (sizeDelta.x, minSize.x, maxSize.x),
            Mathf.Clamp (sizeDelta.y, minSize.y, maxSize.y)
            );

        rectTransform.sizeDelta = sizeDelta;

        previousPointerPosition = currentPointerPosition;
    }
}