记录环境:
- Unity2020
1、Inspector显示
- 修改字段显示的名字
[Label("尺寸")]
public Vector2Int Size;
- 添加字段名额外备注(同时显示标签跟字段名)
[Header("尺寸")]
public Vector2Int Size;
- 自定义字段Filed的显示样式
[CustomPropertyDrawer(typeof(NoiseSettingsPropertyAttribute))]
internal sealed class NoiseSettingsPropertyDrawer : PropertyDrawer
{ ... }
/********* 使用 *********/
[NoiseSettingsProperty]
public NoiseSettings noiseProfile;
- 防止变量重命名后丢失引用
[FormerlySerializedAs("m_IgnoreParent"), SerializeField]
bool m_IgnoreCanvasScaler = true;
2、菜单、入口
- 右键创建资源的快捷菜单
[CreateAssetMenu(fileName = "MyAssetName", menuName = "Game/Create Asset")]
public class MyAssetScript : ScriptableObject
{ ... }
- 在Add Component菜单隐藏自定义组件
[AddComponentMenu("")]
public class CinemachineComposer : CinemachineComponentBase
{ ... }
- Editor脚本中添加组件的右键菜单
// 路径规则:"CONTEXT/组件脚本名"
[MenuItem("CONTEXT/CinemachineVirtualCamera/Adopt Current Camera Settings")]
static void AdoptCurrentCameraSettings(MenuCommand command)
{ ... }
3、杂项
- 剪切板
复制资源名、资源路径等,减少配置出错率
GUIUtility.systemCopyBuffer = Selection.activeObject.name
- UnityEditor启动时执行一些处理
// 脚本编译、Game Play也会执行,依赖 Auto Refresh preferences、Play Mode configuration的配置
[InitializeOnLoad]
public class EditorLaunchRegister
{ ... }
- Project窗口选中资源
EditorGUIUtility.PingObject( asset );
- 选中对象(Inspector)
Selection.SetActiveObjectWithContext(obj, obj);
- 通过菜单名执行编辑器操作
EditorApplication.ExecuteMenuItem("File/Save")
- 添加?的文档跳转
[HelpURL("https://thoughts.teambition.com/workspaces/")]