尺有所短,寸有所长
物有所不足,智有所不明

Unity-UGUI之拖动鼠标改变部件窗口大小

1.先上效果图

【Unity3D】UGUI之拖动鼠标改变部件窗口大小 - 第1å¼  | Unity3D虚拟世界 

2.调整Panel的Rect transform属性

3.挂载代码即可

using UnityEngine;
using UnityEngine.UI;
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.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;
    }
    }
赞(1) 打赏
转载请注明出处:芝麻粒儿博客芝麻粒儿 » Unity-UGUI之拖动鼠标改变部件窗口大小
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏