[Unity Editor] 编辑器脚本常用功能

记录环境:

  • Unity2020

1、Inspector显示


  1. 修改字段显示的名字
[Label("尺寸")]
public Vector2Int Size;
Label
  1. 添加字段名额外备注(同时显示标签跟字段名)
[Header("尺寸")]
public Vector2Int Size;
Header
  1. 自定义字段Filed的显示样式
[CustomPropertyDrawer(typeof(NoiseSettingsPropertyAttribute))]
internal sealed class NoiseSettingsPropertyDrawer : PropertyDrawer
{ ... }

/********* 使用 *********/
[NoiseSettingsProperty]
public NoiseSettings noiseProfile;
  1. 防止变量重命名后丢失引用
[FormerlySerializedAs("m_IgnoreParent"), SerializeField]
bool m_IgnoreCanvasScaler = true;

2、菜单、入口


  1. 右键创建资源的快捷菜单
[CreateAssetMenu(fileName = "MyAssetName", menuName = "Game/Create Asset")]
public class MyAssetScript : ScriptableObject
{ ... }
  1. 在Add Component菜单隐藏自定义组件
[AddComponentMenu("")]
public class CinemachineComposer : CinemachineComponentBase
{ ... }
  1. Editor脚本中添加组件的右键菜单
// 路径规则:"CONTEXT/组件脚本名"
[MenuItem("CONTEXT/CinemachineVirtualCamera/Adopt Current Camera Settings")]
static void AdoptCurrentCameraSettings(MenuCommand command)
{ ... }
Editor脚本中添加组件的右键菜单

3、杂项


  1. 剪切板
    复制资源名、资源路径等,减少配置出错率
GUIUtility.systemCopyBuffer = Selection.activeObject.name
  1. UnityEditor启动时执行一些处理
// 脚本编译、Game Play也会执行,依赖 Auto Refresh preferences、Play Mode configuration的配置
[InitializeOnLoad]
public class EditorLaunchRegister
{ ... }
  1. Project窗口选中资源
EditorGUIUtility.PingObject( asset );
  1. 选中对象(Inspector)
Selection.SetActiveObjectWithContext(obj, obj);
  1. 通过菜单名执行编辑器操作
EditorApplication.ExecuteMenuItem("File/Save")
  1. 添加?的文档跳转
[HelpURL("https://thoughts.teambition.com/workspaces/")]
Unity里问号`?`文档跳转
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容