新增“添加自定义编辑器”菜单项
我写了这个小脚本,它将一个新的添加自定义编辑器
菜单项添加到Assets
上下文菜单中:
![new MenuItem](https://istack.dev59.com/2wABx.webp)
用法
- 右键单击
Scripts
文件夹中的Component
脚本。
- 选择
添加自定义编辑器
。
- 组件现在有一个
Editor
,会自动选中它,所以你可以随意修改它。它的名称相同(加上Editor
),位于相同的相对路径下,但在Scripts/Editor
文件夹中。
以下截图显示了一个示例:给定脚本Scripts/Test/TestScript
,它会创建一个新的编辑器Scripts/Editor/Test/TestScriptEditor
。
注意:除非你选择了以下脚本,否则菜单项将被禁用:
- 以
.cs
文件结尾
- 位于
Scripts
文件夹的某个位置(可以嵌套在任何子目录中)
- 不在
Editor
文件夹中
- 没有编辑器
![after using it](https://istack.dev59.com/oKohX.webp)
安装
Create
-> C# Script
- 将其命名为
AddCustomEditorMenuItem
- 用这里的代码替换其内容
- 完成!
- 测试它:右键单击
Scripts
目录中的一个脚本文件。
代码亮点
scriptName = scriptAsset.name;
scriptPath = Path.GetFullPath(ProjectRoot + AssetDatabase.GetAssetPath (scriptAsset));
editorFileName = GetEditorFileNameFor (scriptName);
var results = scriptPathRegex.Matches (scriptPath).GetEnumerator ();
results.MoveNext ();
var match = (Match)results.Current;
scriptsPath = match.Groups [1].Value;
scriptRelativePath = match.Groups [2].Value;
editorPath = Path.Combine (scriptsPath, "Editor");
editorPath = Path.Combine (editorPath, scriptRelativePath);
editorPath = Path.Combine (editorPath, editorFileName);
editorPath = Path.GetFullPath(editorPath);
editorRelativeAssetPath = editorPath.Substring(ProjectRoot.Length);
public void WriteCustomEditorFile ()
{
Directory.CreateDirectory (Path.GetDirectoryName (editorPath));
File.WriteAllText (editorPath, BuildCustomEditorCode(scriptName));
AssetDatabase.Refresh();
var os = AssetDatabase.LoadAllAssetsAtPath(editorRelativeAssetPath);
EditorGUIUtility.PingObject (os[0]);
Debug.Log("Created new custom Editor at: " + editorRelativeAssetPath);
}
[MenuItem ("Assets/Add Custom Editor %#e", false, 0)]
public static void AddCustomEditor ()
{
var scriptAsset = Selection.activeObject;
var scriptPathInfo = new ScriptPathInfo (scriptAsset);
scriptPathInfo.WriteCustomEditorFile ();
}
- 如果您不喜欢新创建的编辑器的默认内容,可以随意编辑此部分:
static string BuildCustomEditorCode (string name)
{
return @"using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(" + name + @"))]
public class " + name + @"Editor : Editor {
public override void OnInspectorGUI ()
{
base.OnInspectorGUI ();
var obj = (" + name + @") target;
if (GUILayout.Button (""Hi!"")) {
// do something with obj when button is clicked
Debug.Log(""Button pressed for: "" + obj.name);
EditorGUIUtility.PingObject (obj);
}
}
}";
}
- 如果您的菜单项始终是灰色不可用的,请先考虑查看我上面的解释,然后再调试确定是否选择了有效脚本的代码:
[MenuItem ("Assets/Add Custom Editor %#e", true, 0)]
public static bool ValidateAddCustomEditor ()
{
var scriptAsset = Selection.activeObject;
if (scriptAsset == null) {
return false;
}
var path = ProjectRoot + AssetDatabase.GetAssetPath (scriptAsset);
if (!scriptPathRegex.IsMatch (path)) {
return false;
}
if (editorScriptPathRegex.IsMatch (path)) {
return false;
}
if (Directory.Exists (path)) {
return false;
}
var scriptPathInfo = new ScriptPathInfo (scriptAsset);
if (File.Exists (scriptPathInfo.editorPath)) {
return false;
}
return true;
}