【笔记】拓展编辑器理解点
使用平台:Unity引擎
开发工具:Visual Studio
开发语言:C#
拓展前须知
- Editor文件夹:仅存放用于编辑模式下显示的功能选项。
若在Game场景中应用,相关脚本不应该存放至Editor文件夹。 - Eidtor文件夹存在数量不唯一
Attribulte 标签
1.1 MenuItem
MenuItem(string itemName, bool isValidateFunction, int priority, bool internalMenu)
[MenuItem("Assets/My Tools/Tool_01", false, 2)]
private static void MyTools01()
{
Debug.Log("I m Tool 01");
}
- 参数:
itemName(string)
描述:菜单选项路径,例如 Tools/MyTool01、UniversalTool/Tool02 等会在控制面板菜单栏上拓展展示。 - 参数:
isValidateFunction(bool)
描述:验证该功能选项是否可用。【不可用状态 - 灰色】
[MenuItem("GameObject/我的工具/工具03n", true)]
private static bool NewMenuOptionValidation()
{
if (Selection.activeGameObject != null)
{
return Selection.activeObject.GetType() == typeof(GameObject);
}
return false;
}
注意:该功能使用严格于对应MenuItem()下的选项路径,通过 bool 值判断是否可用。默认情况下(无需判断是否可用),使用False,即可菜单直接显示。
- 参数:
priority(int)
描述:菜单项显示顺序。【通常情况下,根据使用频率来决定各菜单选项的排列顺序】 - 参数:
internalMenu(bool)
描述:是否在原生菜单中出现
使用Flase:当出现操作某一选项后,出现更多选项时,是基于该同名选项内产生新选项
使用True:与该选项同名但前缀 internal 的选项内容
关于实现快捷键
- 一般的:在路径后添加 “ %g”[Ctrl + G] ,注意这种写法仅在 %#& 在前下支持
- 通常的:格式按照 “XXXX/XXXX/XXX _快捷键” 方式进行
[MenuItem("Assets/My Tools/Tool_01 %g", false, 2)]
基于不同平台下的字符表现内容:
| 字符 | Windows | macOS |
|---|---|---|
| % | Ctrl | cmd |
| # | Shift | ? |
| & | Alt | ? |
| 使用组合按键 | ||
| 字符 | Windows | macOS |
| – | – | – |
| %#G | Ctrl + Shift + G | ? |
| #%A | Shift + Ctrl + A | ? |
| &Z | Alt + Z | ? |
特殊的:除菜单栏外,其他面板可操作
Assets/Name/Fuction Name _HotKeyProject视窗右键GameObject/Name/Fuction Name _HotKeyHierarchy视窗右键CONTEXT/Component Name _HotKey组件右上角 [更多] 操作菜单
注:如果想在所有组件中添加此方法,Component Name 应命名为Component即可。- 在已有选项中添加选项.例如:
GameObject/Add Component/Transform可用。在GameObject下添加遵循GameObject/[Component Name];在GameObject下的Add Component扩展添加遵循GameObject/Add Component/[Component Name]
其他备注:[Component Name] = [Scripts Name]
1.2 InitializedOnLoadMethod
描述:在Unity加载过程中初始化制作的编辑器【UnityEditor】方法
[InitializedOnLoadMethod]
public static void Init() { //This is a method which init first; }
1.3 CustomEditor
描述:指定 Unity内置组件、在Inspector上序列化展示的自定脚本组件以特殊视图方式进行扩展,调整。
[CustomEditor(typeof(Transform))]
public class Scripts_03_09 : Editor
{
public override void OnInspectorGUI()
{
if (GUILayout.Button("拓展组件 [重置Transoform]"))
{
Selection.activeTransform.position = Vector3.zero;
Selection.activeTransform.localEulerAngles = Vector3.zero;
Selection.activeTransform.localScale = Vector3.one;
}
base.OnInspectorGUI();
}
}
为避免覆写编辑器出现Debug视图模式,声明Editor类成员变量及OnEnable(),添加下列代码。将OnInspectorGUI()方法交付给_Editor来完成
private Editor _Editor;
private void OnEnable(){
//防覆盖
_Editor = Editor.CreateEditor(target, Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.TransformInspector", true));
}
1.4 ExcuteAlways/ExcuteInEditMode
[ExcuteAlways] public class classA {}
[ExcuteInEditMode] public class classB {}
ExcuteInEditMode:允许脚本程序在非Play模式下运行
Update():仅在场景中出现变化时执行
Awake():仅在脚本初始化阶段执行
OnGUI():当Game窗口GUI出现响应时执行
OnRenderObject():回调方法重新绘制Game/Scene窗口ExcuteAlways:任何模式下均运行
1.5 DrawGizmos
描述:设置绘制Gizmos模式【Unity 文档查询不到,疑似弃用?或使用 Excute 替代?】
public class CLASSNAME {
[DrawGzimos(GizmosType.NotInSelectionHierarchy | GizmosType.Active)]
public static void OnDrawGizmos() {}
}
GizmosType.Active:当对象SetActive(true)启用绘制GizmosType.Selected:当对象 被选中时 启用绘制GizmosType.NoSelected:当对象 未被选中 启用绘制GizmosType.SelectedOrChild:对象为 自身或某父对象下子物体,对根父对象 启用绘制GizmosType.Pickable:允许启用绘制的对象 被点选???GizmosType.NotInSelectionHierarchy:允许启用绘制的对象 被点选???
EditorApplication 应用程序类
2.1 projectWindowChange - 资源窗口监视
描述:监视资源窗口的变化【包括 增删查改】
[InitializedOnLoadMethod]
static void Init()
{
EditopApplication.projectWindowChanged = delegate() { Debug.Log("The Assets Has Changed"); };
}
补充说明:关于AssetMoveResult 与 AssetDeleteResult
static void OnCreateAsset(string assetPath) {}
static string[] OnSaveAsset(string[] paths) {}
// 以上可忽略
// 以下为UnityEidtor提供的 AssetMoveResult / AssetDeleteResult
static AssetMoveResult OnMoveAsset(string oldPath, string newPath) {}
static AssetDeleteResult OnDeleteAsset(string assetPath, RemoveAssetOptions option) {}
- 等同参考
UnityWebRequest.Result - 另参考
AssetBundleLoadResult
2.2 hierarchyWindowItem - Project面板拓展
[InitializeOnLoadMethod]
private static void Init()
{
EditorApplication.hierarchyWindowItemOnGUI = delegate (int instanceID, Rect selectionRect)
{
if (Selection.activeObject && instanceID == Selection.activeObject.GetInstanceID())
{
float width = 50f;
float height = 20f;
selectionRect.x += (selectionRect.width - width);
selectionRect.width = width;
selectionRect.height = height;
if (GUI.Button(selectionRect, AssetDatabase.LoadAssetAtPath<Texture>("Assets/01.png"))) {
Debug.Log("Click: " + Selection.activeObject.name);
}
}
};
}
EditorGUILayout GUI层级类
3.1 BeginHorizontal() / EndHorizontal()
描述:绘制函数,用于绘制变量横向布局。常应用于OnInspectorGUI()
private override void OnInspectorGUI() {
EditorGUILayout.BeginHorizontal();
GUILayout.Button("Button 01");
....
EditorGUILayout.EndHorizontal;
}
3.2 BeginToggleGroup() / EndToggleGroup()
描述:Toggle组功能,当且仅当前置bool条件成立时,Toggle组内选项可用。
private override void OnInspectorGUI(){
private bool _enableToggle;
private override void OnInspetcorGUI() {
_enableToggle = EditorLayout.BeginToggleGroup("EnableToggle", _enableToggle);
xxx(This is a script's name).yyy(This is a public bool value) = EditorGUILayout.Toggle("Toggle 01", xxx.yyy);
xxx.zzz = EditorGUILayout.Toggle("Toggle 02 ", xxx.zzz);
...
EditorLayout.EndToggleGroup();
}
}
3.3 BeginScrollView() / EndScrollView()
描述:限定内容执行SrollView呈现。
private Vector2 scrVec2;
static void OnInspectorGUI/OnGUI/OnHeaderGUI()
{
scrVec2 = EditorGUILayout.BeginScrollView(scrVec2, GUILayout.Width(100), GUILayout.Height(100));
{
GUILayout.Button("Make Sure") { Debug.Log("[Debug] I make sure !"); }
GUILayout.Label("Picture Path");
...
}
EditorLayout.EndScrollView();
}
3.4 Repaint 刷新
当编辑器出现新的信息更新无法及时更新时,使用this.Repaint()刷新编辑器窗口页面做到实时更新。
推荐写入关联方法OnSelectionChange()中
Event UGUI事件
3.1 Use
描述:自定义菜单等操作下告诉Unity该项操作已经调用结束。用于避免与原生Unity操作事件冲突的解决方法。
Event.current.Use();
Selection 选择类
描述:提供当前为焦点时的对象
Selection.activeTransform:返回选择对象的Transform组件信息【通用】
AssetDatabase 资源数据类
描述:存储资源数据信息的类。用于管理项目资源对象
AssetDatabse.GetAssetPath()获取资源路径。AssetDatabase.AssetPathToGUID()获取资源唯一GUID信息。LoadAssetAtPath<T>(string path):根据资源类型及资源路径索引资源对象。GenerateUniqueAssetPath(string path);:创建唯一路径。Refresh():强制 Project 视图更新内容。
PrefabUtility 预制件资源类
描述:针对于Unity内部资产管理类。
InstantiatePrefab(): 创建预制件资源
public class Script_11_02 : MonoBehaviour
{
[MenuItem("Assets/Book Tools/LoadPrefab", false, 2)]
static void LoadPrefab()
if(Selection.activeTransform)
{
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefab/Cube.prefab");
//GameObject go = GameObject.Instantiate<GameObject>(prefab);
GameObject go = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
//go.transform.SetParent(Selection.activeTransform);
go.transform.SetParent(Selection.activeTransform, false);
}
}
}
该方法下对Prefab引用可持续保持。SetParent(,false)坐标回归原点位置,即不受父坐标影响。
ReplacePrefab():替换Prefab,可用于更新Prefab。GetCorrespondingObjectFromSource():返回Sources的资源对象。
关于资源卸载:编辑模式下仅GameObject.DestroyImmediate(GameObject, bool)可用。TRUE下同时卸载游戏对象引用的资源。
EditorUtility 编辑资源类
EditorUtility.UnLoadUnusedAssetsImmediate():卸载未被使用、引用的资源
AssetModificationProcessor 序列化资源编辑动作
提供OnWillCreateAsset()/OnWillDeleteAsset()/OnWillMoveAsset()/OnWillSaveAssets()四类方法可覆写
Gizmos 可视化视图类
描述:可绘制辅助参考线、框等图形,限制于 OnGizmosGUI() 与 OnGizmosSelect() 中
案例记录:
private void OnGizmosGUI()
{
Gizmos.Color = Color.Green;
Gizmos.matrix = Matrix4x4.TRS(Camera.mian, transform.position, transform.rotation, Vector3.one);
Gizmos.DrawFrustum(Vector3.zero, Camera.main.fieldOfView, Camera.main.farClipPlane, Camera.main.nearClipPlane);
}
更多推荐
所有评论(0)