unity3D提供了自定义编辑器的接口

  • 在Assets中新建一个C# 脚本,并让public类继承EditorWindow
    • using UnityEngine;
      using UnityEditor;
       
      public class ResizablePanels : EditorWindow
      {
       
      }

       

  •  编辑器窗口需要初始化一个静态方法OpenWindow(),然后在这个窗口中获得一个窗口并添加标题, 仅靠静态方法是不够的,我们还需要添加一个按钮或类似的按钮,以使我们的用户在Unity中打开窗口。幸运的是,已经有一个名为MenuItem的属性,该属性可将菜单项添加到Unity的菜单栏中并运行应用于该菜单项的方法。
    • using UnityEngine;
      using UnityEditor;
       
      public class ResizablePanels : EditorWindow
      {
          [MenuItem("Window/Resizable Panels")]
          private static void OpenWindow()
          {
              ResizablePanels window = GetWindow();
              window.titleContent = new GUIContent("Resizable Panels"); // 添加标题
          }
      }

       

  •  为了在窗口中绘制图案,我们需要调用OnGUI()方法,首先我们需要两个矩形来定义面板
    • using UnityEngine;
      using UnityEditor;
       
      public class ResizablePanels : EditorWindow
      {
          private Rect upperPanel;
          private Rect lowerPanel;
       
          [MenuItem("Window/Resizable Panels")]
          private static void OpenWindow()
          {
              ResizablePanels window = GetWindow();
              window.titleContent = new GUIContent("Resizable Panels");
          }
       
          private void OnGUI()
          {
              DrawUpperPanel();
              DrawLowerPanel();
          }
       
          private void DrawUpperPanel()
          {
          }
       
          private void DrawLowerPanel()
          {
          }
      }

       

  • GUILayout.BeginArea(Rect rect)将创建一个矩形区域,GUILayout.EndArea()标记结束。这些区域将定义我们的面板。我还将在两个区域中添加标签,以便我们可以看到它们的外观。

    • using UnityEngine;
      using UnityEditor;
       
      public class ResizablePanels : EditorWindow
      {
          private Rect upperPanel;
          private Rect lowerPanel;
       
          [MenuItem("Window/Resizable Panels")]
          private static void OpenWindow()
          {
              ResizablePanels window = GetWindow();
              window.titleContent = new GUIContent("Resizable Panels");
          }
       
          private void OnGUI()
          {
              DrawUpperPanel();
              DrawLowerPanel();
          }
       
          private void DrawUpperPanel()
          {
              upperPanel = new Rect(0, 0, position.width, position.height * 0.5f);
       
              GUILayout.BeginArea(upperPanel);
              GUILayout.Label("Upper Panel");
              GUILayout.EndArea();
          }
       
          private void DrawLowerPanel()
          {
              lowerPanel = new Rect(0, position.height * 0.5f, position.width, position.height * 0.5f);
       
              GUILayout.BeginArea(lowerPanel);
              GUILayout.Label("Lower Panel");
              GUILayout.EndArea();
          }
      }

       

  •  但是,这两个面板分别设置为覆盖窗口的一半,但是如果我们希望它们可调整大小,则它们需要具有可变的高度。因此,我将添加一个大小比例变量;这样,当一个面板覆盖一定数量的窗口时,另一个面板可以覆盖其余部分。
    • using UnityEngine;
      using UnityEditor;
       
      public class ResizablePanels : EditorWindow
      {
          private Rect upperPanel;
          private Rect lowerPanel;
       
          private float sizeRatio = 0.5f;
       
          [MenuItem("Window/Resizable Panels")]
          private static void OpenWindow()
          {
              ResizablePanels window = GetWindow();
              window.titleContent = new GUIContent("Resizable Panels");
          }
       
          private void OnGUI()
          {
              DrawUpperPanel();
              DrawLowerPanel();
          }
       
          private void DrawUpperPanel()
          {
              upperPanel = new Rect(0, 0, position.width, position.height * sizeRatio);
       
              GUILayout.BeginArea(upperPanel);
              GUILayout.Label("Upper Panel");
              GUILayout.EndArea();
          }
       
          private void DrawLowerPanel()
          {
              lowerPanel = new Rect(0, (position.height * sizeRatio), position.width, position.height * (1 - sizeRatio));
       
              GUILayout.BeginArea(lowerPanel);
              GUILayout.Label("Lower Panel");
              GUILayout.EndArea();
          }
      }

       

  • 接下来,我们使用EditorGUIUtility.AddCursorRect(Rect rect,MouseCursor光标) 方法来完成用鼠标调节区域大小的功能

  • using UnityEngine;
    using UnityEditor;
     
    public class ResizablePanels : EditorWindow
    {
        private Rect upperPanel;
        private Rect lowerPanel;
        private Rect resizer;
     
        private float sizeRatio = 0.5f;
        private bool isResizing;
     
        private GUIStyle resizerStyle;
     
        [MenuItem("Window/Resizable Panels")]
        private static void OpenWindow()
        {
            ResizablePanels window = GetWindow();
            window.titleContent = new GUIContent("Resizable Panels");
        }
     
        private void OnEnable()
        {
            resizerStyle = new GUIStyle();
            resizerStyle.normal.background = EditorGUIUtility.Load("icons/d_AvatarBlendBackground.png") as Texture2D;
        }
     
        private void OnGUI()
        {
            DrawUpperPanel();
            DrawLowerPanel();
            DrawResizer();
        }
     
        private void DrawUpperPanel()
        {
            upperPanel = new Rect(0, 0, position.width, position.height * sizeRatio);
     
            GUILayout.BeginArea(upperPanel);
            GUILayout.Label("Upper Panel");
            GUILayout.EndArea();
        }
     
        private void DrawLowerPanel()
        {
            lowerPanel = new Rect(0, (position.height * sizeRatio) + 5, position.width, position.height * (1 - sizeRatio) - 5);
     
            GUILayout.BeginArea(lowerPanel);
            GUILayout.Label("Lower Panel");
            GUILayout.EndArea();
        }
     
        private void DrawResizer()
        {
            resizer = new Rect(0, (position.height * sizeRatio) - 5f, position.width, 10f); // 这里是确定位置的
     
            GUILayout.BeginArea(new Rect(resizer.position + (Vector2.up * 5f), new Vector2(position.width, 2)), resizerStyle); // 这里是绘制调节的横条,宽度为整个面板的宽度,高度为2
            GUILayout.EndArea();
     
            EditorGUIUtility.AddCursorRect(resizer, MouseCursor.ResizeVertical);
        }
    }

     

  •  最后,我们给鼠标来配置事件
    • using UnityEngine;
      using UnityEditor;
       
      public class ResizablePanels : EditorWindow
      {
          private Rect upperPanel;
          private Rect lowerPanel;
          private Rect resizer;
       
          private float sizeRatio = 0.5f;
          private bool isResizing;
       
          private GUIStyle resizerStyle;
       
          [MenuItem("Window/Resizable Panels")]
          private static void OpenWindow()
          {
              ResizablePanels window = GetWindow();
              window.titleContent = new GUIContent("Resizable Panels");
          }
       
          private void OnEnable()
          {
              resizerStyle = new GUIStyle();
              resizerStyle.normal.background = EditorGUIUtility.Load("icons/d_AvatarBlendBackground.png") as Texture2D;
          }
       
          private void OnGUI()
          {
              DrawUpperPanel();
              DrawLowerPanel();
              DrawResizer();
       
              ProcessEvents(Event.current);
       
              if (GUI.changed) Repaint();
          }
       
          private void DrawUpperPanel()
          {
              upperPanel = new Rect(0, 0, position.width, position.height * sizeRatio);
       
              GUILayout.BeginArea(upperPanel);
              GUILayout.Label("Upper Panel");
              GUILayout.EndArea();
          }
       
          private void DrawLowerPanel()
          {
              lowerPanel = new Rect(0, (position.height * sizeRatio) + 5, position.width, position.height * (1 - sizeRatio) - 5);
       
              GUILayout.BeginArea(lowerPanel);
              GUILayout.Label("Lower Panel");
              GUILayout.EndArea();
          }
       
          private void DrawResizer()
          {
              resizer = new Rect(0, (position.height * sizeRatio) - 5f, position.width, 10f);
       
              GUILayout.BeginArea(new Rect(resizer.position + (Vector2.up * 5f), new Vector2(position.width, 2)), resizerStyle);
              GUILayout.EndArea();
       
              EditorGUIUtility.AddCursorRect(resizer, MouseCursor.ResizeVertical);
          }
       
          private void ProcessEvents(Event e)
          {
              switch (e.type)
              {
                  case EventType.MouseDown:
                      if (e.button == 0 && resizer.Contains(e.mousePosition)) // 鼠标左键按下且鼠标在调节条的位置处
                      {
                          isResizing = true; // 调节为true
                      }
                      break;
       
                  case EventType.MouseUp:
                      isResizing = false;
                      break;
              }
       
              Resize(e);
          }
       
          private void Resize(Event e)
          {
              if (isResizing) // 如果调节为true
              {
                  sizeRatio = e.mousePosition.y / position.height; // 开始计算比例
                  Repaint();
              }
          }
      }

        

本文参考(https://gram.gs/gramlog/creating-editor-windows-in-unity/)

效果如下

unity3D提供了自定义编辑器的接口_第1张图片

你可能感兴趣的:(unity3D,c#,unity3d)