插入排序

UnityEditor编辑器的代码必须放在Editor文件夹下,因为这个文件夹下的代码是不会被打包出去,只允许程序员在Unity编辑器下使用,如果不放在Editor这个文件夹下,打包时若包含UnityEditor的代码,会报错。

1.使用方法:[MenuItem(“path”)]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using UnityEditor;
using UnityEngine;

namespace EditorExtensions
{
public class MenuItemExample
{
// [MenuItem()] 修饰的必须是一个静态方法
// 才会在Unity菜单栏出现先对应的按键
// ()括号里的是这个方法在菜单栏的树形路径
[MenuItem("EditorExtensions/MenuItem/EditorTest")]
private static void EditorTest()
{
Debug.Log("Hello Editor");
}
}
}
2.Application.OpenURL(“path”) 打开一个网址或者一个Application应用
1
2
3
4
5
6
[MenuItem("EditorExtensions/MenuItem/OpenBilibili")]
private static void OpenBilibili()
{
// 可以通过这个Api接口打开一个网址或者一个Application应用
Application.OpenURL("Http://bilibili.com");
}
3.EditorUtility.RevealInFinder(“path”) 打开一个指定的文件目录
1
2
3
4
5
6
[MenuItem("EditorExtensions/MenuItem/打开策划目录")]
private static void OpenDesignFolder()
{
// 打开一个指定的文件目录 参数是一个文件的路径
EditorUtility.RevealInFinder(Application.dataPath.Replace("Assets", "Library"));
}
4.Menu.SetChecked(“path”,isChecked) 可勾选的菜单栏
1
2
3
4
5
6
7
8
private static bool openShotCut = false; // 记录菜单栏勾选状态

[MenuItem("EditorExtensions/MenuItem/快捷键开关")]
private static void ToggleShotCut()
{
openShotCut = !openShotCut; // 切换状态
Menu.SetChecked("EditorExtensions/MenuItem/快捷键开关", openShotCut);
}
5.MenuItem的快捷键设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[MenuItem("EditorExtensions/MenuItem/EditorTestWithShotCut _c")]
private static void EditorTestWithShotCut()
{
// 常用快捷键码
// # -> Shift & -> Alt % -> Ctrl/Command
// _a-z A-Z - a-z A-Z
Debug.Log("键盘C键的快捷键呼出菜单");
}

[MenuItem("EditorExtensions/MenuItem/OpenBilibiliWithShotCut %#e")]
private static void OpenBilibiliWithShotCut()
{
EditorUtility.RevealInFinder(Application.persistentDataPath);
}
6.[MenuItem(“path”, validate = true)]
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
private static bool openShotCut = false; // 记录菜单栏勾选状态

[MenuItem("EditorExtensions/MenuItem/快捷键开关")]
private static void ToggleShotCut()
{
openShotCut = !openShotCut; // 切换状态
Menu.SetChecked("EditorExtensions/MenuItem/快捷键开关", openShotCut);
}
[MenuItem("EditorExtensions/MenuItem/EditorTestWithShotCut _c")]
private static void EditorTestWithShotCut()
{
// 常用快捷键码
// # -> Shift & -> Alt % -> Ctrl/Command
// _a-z A-Z - a-z A-Z
Debug.Log("键盘C键的快捷键呼出菜单");
}
// openShotCut 为true时才能被使用
[MenuItem("EditorExtensions/MenuItem/EditorTestWithShotCut %#e", validate = true)]
private static bool EditorTestWithShotCutValidate()
{
return openShotCut;
}

[MenuItem("EditorExtensions/MenuItem/OpenBilibiliWithShotCut %#e")]
private static void OpenBilibiliWithShotCut()
{
EditorUtility.RevealInFinder(Application.persistentDataPath);
}

[MenuItem("EditorExtensions/MenuItem/OpenBilibiliWithShotCut %#e", validate = true)]
private static bool OpenBilibiliWithShotCutValidate()
{
return openShotCut;
}
7. EditorApplication.ExecuteMenuItem(“同名path”) 复用MenuItem
1
2
3
4
5
[MenuItem("EditorExtensions/MenuItem/EditorTestWithShotCut _c")]
private static void EditorTestWithShotCut()
{
EditorApplication.ExecuteMenuItem("EditorExtensions/MenuItem/EditorTest");
}

EditorWindow

1.绘制一个编辑器窗口
1
2
3
4
5
6
7
8
9
10
11
[MenuItem("EditorExtensions/IMGUI/OpenGUILayoutExample")]
private static void OpenGUILayoutExample()
{
GUILayoutExample window = GetWindow<GUILayoutExample>();
window.Show();
}

private void OnGUI()
{
GUILayout.Label("Hello IMGUI");
}