当前位置:   article > 正文

Unity编辑器扩展-拖拽调整区域大小_限制unity在编辑器中拖拽物体的移动单位

限制unity在编辑器中拖拽物体的移动单位
using UnityEngine;
using UnityEditor;

public class ExampleEditorWindow : EditorWindow
{
    private Vector2 lScrollPosition, rScrollPosition;
    //分割线宽度
    private const float splitterWidth = 2f;
    //分割线位置
    private float splitterPos;
    private Rect splitterRect;
    //是否正在拖拽分割线
    private bool isDragging;

    [MenuItem("Tools/Example Editor Window")]
    public static void Open() {
        GetWindow<ExampleEditorWindow>().Show();
    }

    private void OnEnable() {
        splitterPos = position.width * .3f;
    }

    internal void OnGUI() {
        GUILayout.BeginHorizontal();
        {
            lScrollPosition = GUILayout.BeginScrollView(lScrollPosition , GUILayout.Width(splitterPos) , GUILayout.MaxWidth(splitterPos) , GUILayout.MinWidth(splitterPos));
            OnLeftGUI();
            GUILayout.EndScrollView();

            //分割线
            GUILayout.Box(string.Empty , GUILayout.Width(splitterWidth) , GUILayout.MaxWidth(splitterWidth) , GUILayout.MinWidth(splitterWidth) , GUILayout.ExpandHeight(true));
            splitterRect = GUILayoutUtility.GetLastRect();

            rScrollPosition = GUILayout.BeginScrollView(rScrollPosition , GUILayout.ExpandWidth(true));
            OnRightGUI();
            GUILayout.EndScrollView();
        }
        GUILayout.EndHorizontal();

        if (Event.current != null) {
            //光标
            EditorGUIUtility.AddCursorRect(splitterRect , MouseCursor.ResizeHorizontal);
            switch (Event.current.rawType) {
                //开始拖拽分割线
                case EventType.MouseDown:
                isDragging = splitterRect.Contains(Event.current.mousePosition);
                break;
                case EventType.MouseDrag:
                if (isDragging) {
                    splitterPos += Event.current.delta.x;
                    //限制其最大最小值
                    splitterPos = Mathf.Clamp(splitterPos , position.width * .2f , position.width * .8f);
                    Repaint();
                }
                break;
                //结束拖拽分割线
                case EventType.MouseUp:
                if (isDragging)
                    isDragging = false;
                break;
            }
        }
    }
    private void OnLeftGUI() {
        for (int i = 0 ; i < 10 ; i++) {
            if (GUILayout.Button("左侧按钮")) {
                Debug.Log("左侧按钮按下");
            }
        }
    }

    private void OnRightGUI() {
        if (GUILayout.Button("右侧按钮")) {
            Debug.Log("右侧按钮按下");
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/93023
推荐阅读
相关标签
  

闽ICP备14008679号