Unity中自动添加自定义脚本编辑器的快捷方式

3
Assets的Unity上下文菜单拥有各种有用的快捷方式,但是没有快捷方式可以快速创建自定义编辑器。如果我想创建自定义编辑器,我必须经历所有这些烦人的样板步骤:
  1. 创建Editor目录(如果尚未创建)并进入正确的子目录
  2. Create -> C#脚本(实际上应该称为C#组件脚本
  3. 删除所有MonoBehavior部分
  4. 在类名后追加Editor
  5. 添加using UnityEditor
  6. 添加CustomEditor属性
  7. Editor继承
在实际开始编写之前,我还需要执行以上步骤。
是否有任何快捷方式可以实现这个过程?
1个回答

5

新增“添加自定义编辑器”菜单项

我写了这个小脚本,它将一个新的添加自定义编辑器 菜单项添加到Assets上下文菜单中:

new MenuItem

用法

  1. 右键单击Scripts文件夹中的Component脚本。
  2. 选择添加自定义编辑器
  3. 组件现在有一个Editor,会自动选中它,所以你可以随意修改它。它的名称相同(加上Editor),位于相同的相对路径下,但在Scripts/Editor文件夹中。

以下截图显示了一个示例:给定脚本Scripts/Test/TestScript,它会创建一个新的编辑器Scripts/Editor/Test/TestScriptEditor

注意:除非你选择了以下脚本,否则菜单项将被禁用:

  1. .cs文件结尾
  2. 位于Scripts文件夹的某个位置(可以嵌套在任何子目录中)
  3. 不在Editor文件夹中
  4. 没有编辑器

after using it

安装

  1. Create -> C# Script
  2. 将其命名为AddCustomEditorMenuItem
  3. 这里的代码替换其内容
  4. 完成!
  5. 测试它:右键单击Scripts目录中的一个脚本文件。

代码亮点

  • 找出所有路径花费了大部分时间:
scriptName = scriptAsset.name;

// get system file path
scriptPath = Path.GetFullPath(ProjectRoot + AssetDatabase.GetAssetPath (scriptAsset));

// get file name of the editor file
editorFileName = GetEditorFileNameFor (scriptName);

// split the script path
var results = scriptPathRegex.Matches (scriptPath).GetEnumerator ();
results.MoveNext ();
var match = (Match)results.Current;
scriptsPath = match.Groups [1].Value;
scriptRelativePath = match.Groups [2].Value;

// re-combine editor path
editorPath = Path.Combine (scriptsPath, "Editor");
editorPath = Path.Combine (editorPath, scriptRelativePath);
editorPath = Path.Combine (editorPath, editorFileName);

// nicely formatted file path
editorPath = Path.GetFullPath(editorPath);
editorRelativeAssetPath = editorPath.Substring(ProjectRoot.Length);
  • 一旦确定了路径,实际编写文件就变得非常简单易懂!
public void WriteCustomEditorFile ()
{
  // create all missing directories in the hierarchy
  Directory.CreateDirectory (Path.GetDirectoryName (editorPath));

  // write file
  File.WriteAllText (editorPath, BuildCustomEditorCode(scriptName));

  // let Asset DB pick up the new file
  AssetDatabase.Refresh();

  // highlight in GUI
  var os = AssetDatabase.LoadAllAssetsAtPath(editorRelativeAssetPath);
  EditorGUIUtility.PingObject (os[0]);

  // log
  Debug.Log("Created new custom Editor at: " + editorRelativeAssetPath);
}

// ...

/// <summary>
/// The menu item entry
/// </summary>
[MenuItem ("Assets/Add Custom Editor %#e", false, 0)]
public static void AddCustomEditor ()
{
  var scriptAsset = Selection.activeObject;

  // figure out paths
  var scriptPathInfo = new ScriptPathInfo (scriptAsset);

  // write file
  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) {
    // nothing selected? (should probably not happen)
    return false;
  }

  var path = ProjectRoot + AssetDatabase.GetAssetPath (scriptAsset);

  if (!scriptPathRegex.IsMatch (path)) {
    // not a Script in the Script folder
    return false;
  }

  if (editorScriptPathRegex.IsMatch (path)) {
    // we are not interested in Editor scripts
    return false;
  }
    

  if (Directory.Exists (path)) {
    // it's a directory, but we want a file
    return false;
  }

  var scriptPathInfo = new ScriptPathInfo (scriptAsset);

  //        Debug.Log (scriptPathInfo.scriptPath);
  //        Debug.Log (Path.GetFullPath(AssetsPath + "/../"));
  //        Debug.Log (scriptPathInfo.editorRelativeAssetPath);
  //        Debug.Log (scriptPathInfo.editorPath);

  if (File.Exists (scriptPathInfo.editorPath)) {
    // editor has already been created
    return false;
  }

  // all good!
  return true;
}

1
这真的很有帮助。如果您能为我看一下这个Unity编辑器问题,我会很高兴的。https://stackoverflow.com/questions/52790971/scene-view-cannot-recognise-movement-of-several-objects-in-the-editor-until-obje/52791435?noredirect=1#52791435 - Bane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接