对这个比较好奇,所以来记载一下它的相关知识点和自己动手实验的过程。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
参考博客:
https://www.cnblogs.com/CaomaoUnity3d/p/4698538.html
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
首先来看Unity Manual手册:https://docs.unity3d.com/ScriptReference/EditorWindow.html
Derive from this class to create an editor window.
Create your own custom editor window that can float free or be docked as a tab, just like the native windows in the Unity interface.
Editor windows are typically opened using a menu item.
using UnityEngine;
using UnityEditor;
public class MyWindow : EditorWindow
{
string myString = "Hello World";
bool groupEnabled;
bool myBool = true;
float myFloat = 1.23f;
// Add menu named "My Window" to the Window menu
[MenuItem("Window/My Window")]
static void Init()
{
// Get existing open window or if none, make a new one:
MyWindow window = (MyWindow)EditorWindow.GetWindow(typeof(MyWindow));
window.Show();
}
void OnGUI()
{
GUILayout.Label("Base Settings", EditorStyles.boldLabel);
myString = EditorGUILayout.TextField("Text Field", myString);
groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);
myBool = EditorGUILayout.Toggle("Toggle", myBool);
myFloat = EditorGUILayout.Slider("Slider", myFloat, -3, 3);
EditorGUILayout.EndToggleGroup();
}
}
意思就是可以通过该类创建自定义的编辑器窗口,该窗口可以可以悬浮或者固定在menu栏里,就像Unity本身自带的接口一样自然。上面的代码做实验如下:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
接下来分析一些比较重要的函数:
GetWindow是静态方法,参数分别为:
-窗口类型,一定要继承自EditorWindow
-窗口是否浮动,如果是true则浮动,false则内嵌到其他窗口,默认是内嵌
-窗口的标题,如果是空就采用类的名称来当标题
-是否聚焦,如果GetWindow创建了一个新的窗口,则一定是“聚焦”在当前窗口
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
再来看一些重要的变量
首先来看focusedWindow变量
public static EditorWindow focusedWindow;
The EditorWindow which currently has keyboard focus. (Read Only)
focusedWindow
can be null if no window has focus.
意思就是focusedWindow是静态变量,它的类型是EditorWindow,当我们聚焦到哪个窗口,那么这个静态变量就是哪个窗口,其实也就是记录我们聚焦的窗口。
public static void Init()
{
GetWindow<MyWindow>("My Window");
}
void OnGUI()
{
GUILayout.Label(EditorWindow.focusedWindow.ToString());
}
再来看mouseOverWindow变量
public static EditorWindow mouseOverWindow;
The EditorWindow currently under the mouse cursor. (Read Only)
mouseOverWindow
can be null if there is no window under the cursor.
意思就是鼠标悬停在哪个Window,这个静态变量就是那个窗口。此时我鼠标悬停在场景上
using UnityEngine;
using UnityEditor;
public class MouseOverWindowExample : EditorWindow
{
string mouseOver = "Nothing...";
[MenuItem("Examples/mouseOver")]
static void Init()
{
GetWindow<MouseOverWindowExample>("mouseOver");
}
void OnGUI()
{
GUILayout.Label("Mouse over:\n" + mouseOver);
if (GUILayout.Button("Close"))
{
this.Close();
}
mouseOver = EditorWindow.mouseOverWindow ?
EditorWindow.mouseOverWindow.ToString() : "Nothing...";
}
void OnInspectorUpdate()
{
if (EditorWindow.mouseOverWindow)
{
EditorWindow.mouseOverWindow.Focus();
}
this.Repaint();
}
}
先试验到这里,还有的那些东西,可以看着手册进行实验理解。若有碰到再来补充。