
UnityEditor编辑器的代码必须放在Editor文件夹下,因为这个文件夹下的代码是不会被打包出去,只允许程序员在Unity编辑器下使用,如果不放在Editor这个文件夹下,打包时若包含UnityEditor的代码,会报错。
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("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() { 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")); }
|
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); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [MenuItem("EditorExtensions/MenuItem/EditorTestWithShotCut _c")] private static void EditorTestWithShotCut() { Debug.Log("键盘C键的快捷键呼出菜单"); }
[MenuItem("EditorExtensions/MenuItem/OpenBilibiliWithShotCut %#e")] private static void OpenBilibiliWithShotCut() { EditorUtility.RevealInFinder(Application.persistentDataPath); }
|
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() { Debug.Log("键盘C键的快捷键呼出菜单"); }
[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; }
|
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"); }
|