Unity3D 编辑器扩展开发指南
前言
Unity 编辑器扩展(Editor Scripting Extensions)是 Unity 提供的强大功能,允许开发者通过 C# 脚本自定义和扩展 Unity 编辑器界面。通过编写编辑器脚本,你可以创建自定义工具、优化工作流程,并为特定项目需求添加专属功能。
对惹,这里有一个游戏开发交流小组,希望大家可以点击进来一起交流一下开发经验呀!
核心概念
- Editor 脚本位置
所有编辑器扩展脚本必须放在Assets/Editor
文件夹(或其子目录)中,否则无法被 Unity 识别。 - 命名空间
使用UnityEditor
命名空间访问编辑器 API:
using UnityEditor;
编译顺序
编辑器脚本与常规游戏代码分开编译,确保不会将编辑器代码打包到最终游戏中。
常用类与方法
MenuItem
属性
用于在菜单栏添加自定义菜单项:
[MenuItem("Tools/My Custom Tool")]
static void MyTool()
{Debug.Log("Custom tool activated!");
}
EditorWindow
类
创建自定义编辑器窗口:
public class MyWindow : EditorWindow
{[MenuItem("Window/My Window")]static void ShowWindow(){GetWindow<MyWindow>("My Window");}void OnGUI(){GUILayout.Label("Hello, Editor Window!");}
}
CustomEditor
属性
自定义特定组件的 Inspector 面板:
[CustomEditor(typeof(MyComponent))]
public class MyComponentEditor : Editor
{public override void OnInspectorGUI(){base.OnInspectorGUI();if (GUILayout.Button("Custom Button")){// 自定义逻辑}}
}
SerializedObject
与SerializedProperty
安全地操作序列化数据:
SerializedObject so;
SerializedProperty myProperty;void OnEnable()
{so = new SerializedObject(target);myProperty = so.FindProperty("myField");
}public override void OnInspectorGUI()
{so.Update();EditorGUILayout.PropertyField(myProperty);so.ApplyModifiedProperties();
}
常用场景示例
1. 批量重命名工具
[MenuItem("Tools/Batch Rename Selected Objects")]
static void BatchRename()
{GameObject[] selected = Selection.gameObjects;for (int i = 0; i < selected.Length; i++){selected[i].name = $"Object_{i + 1}";}
}
2. 快速创建预设按钮
[MenuItem("GameObject/Create Prefab", false, 0)]
static void CreatePrefab()
{GameObject selected = Selection.activeGameObject;if (selected != null){string path = "Assets/Prefabs/" + selected.name + ".prefab";PrefabUtility.SaveAsPrefabAsset(selected, path);}
}
3. 自定义 Inspector 面板
[CustomEditor(typeof(HealthComponent))]
public class HealthEditor : Editor
{public override void OnInspectorGUI(){HealthComponent hc = (HealthComponent)target;EditorGUILayout.LabelField("Current Health", hc.CurrentHealth.ToString());if (GUILayout.Button("Reset Health")){hc.ResetHealth();}DrawDefaultInspector(); // 显示默认字段}
}
进阶技巧
Undo 支持
使用Undo.RecordObject
实现操作撤销:
Undo.RecordObject(targetObject, "Change Property");
targetObject.myProperty = newValue;
EditorGUILayout 控件
利用丰富的 UI 控件构建复杂界面:
EditorGUILayout.Toggle()
EditorGUILayout.ColorField()
EditorGUILayout.ObjectField()
AssetPostprocessor
监听资源导入事件:
public class MyTextureProcessor : AssetPostprocessor
{void OnPreprocessTexture(){TextureImporter importer = (TextureImporter)assetImporter;importer.textureType = TextureImporterType.Sprite;}
}
Handles 与 SceneGUI
在场景视图中绘制自定义 Gizmos:
[CustomEditor(typeof(PathNode))]
public class PathNodeEditor : Editor
{void OnSceneGUI(){PathNode node = target as PathNode;Handles.color = Color.red;Handles.DrawWireCube(node.transform.position, Vector3.one);}
}
注意事项
- 版本兼容性
Unity 编辑器 API 在不同版本间可能有变化,需注意向后兼容性。 - 性能优化
避免在OnInspectorGUI
中执行耗时操作,尤其是在处理大量数据时。 - 条件编译
使用#if UNITY_EDITOR
防止编辑器代码进入构建版本:
#if UNITY_EDITOR
// 编辑器专用代码
#endif
学习资源
- 官方文档:Unity Editor Scripting
- Unity Learn 教程:Extending the Unity Editor
- GitHub 示例库:Unity Editor Extensions
通过灵活使用编辑器扩展,可以显著提升开发效率,尤其适合需要频繁处理重复任务或管理复杂数据结构的项目。
更多教学视频
Unity3Dwww.bycwedu.com/promotion_channels/2146264125