美文网首页unity3D技术分享unityUnity教程合集
unity3d用按钮进行拖拽放大或缩小panel

unity3d用按钮进行拖拽放大或缩小panel

作者: 好怕怕 | 来源:发表于2017-04-27 19:36 被阅读461次

    效果可以看下面的图,实现窗口的拖拽放大缩小


    测试.gif

    设置panel的瞄点为左上角,设置point值 X:0,Y:1,按钮的图右下角放置,瞄点为panel的右下角,按钮挂载当前脚本,赋值panel对象即可


    设置.png
    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;
        }
    }
    

    相关文章

      网友评论

        本文标题:unity3d用按钮进行拖拽放大或缩小panel

        本文链接:https://www.haomeiwen.com/subject/btynzttx.html