Unity插件之Odin 2.inspector使用-Essentials
简介
Odin定义了很多的属性标签,通过这些属性标签很大程度上增强了编辑器功能,本文将详细介绍如下属性标签:
- EnableIf & DisableIf
- ShowIf & HideIf
- HideInPlayMode & HideInEditorMode & DisableInPlayMode & DisableInEditorMode
- Required
- ReadOnly
- AssetsOnly
- SceneObjectsOnly
- ValidateInput
- HideInPrefabAssets & HideInPrefabInstances & HideInPrefabs & HideInNonPrefabs & DisableInPrefabAssets & DisableInPrefabInstances & DisableInPrefabs & DisableInNonPrefabs
- TypeFilter
声明
本文中的内容属于个人总结整理而来,个人水平有限,对于部分细节难免有理解错误及遗漏之处,如果您在阅读过程中有所发现,希望您能指正,同时文章中的部分内容也参考了其它大神的文章,如果文章中的内容侵犯了您的权益,表示非常歉意,请您指出,我将尽快修改。
如果您进行转载,请标明出处。
Unity插件之Odin 2.inspector使用-Essentials(http://www.liyubin.com/articles/2019/03/26/1553609564799.html)
API详解
EnableIfAttribute & DisableIfAttribute
简介
可用于字段或者属性,用来判断是否在属性面板enable或者disable
API详解
-
Fields
类 型 名 称 说 明 string MemberName 指定一个字段、属性或者方法的名称,如果返回值为bool型,则直接使用返回值;如果有指定Value,则会根据Value进行判断 object Value 用于判断的值 Properties
Constructors
示例
[EnumToggleButtons]
public InfoMessageType SomeEnum;
public bool IsToggled;
[EnableIf("SomeEnum", InfoMessageType.Info)]
public Vector2 Info;
[EnableIf("IsToggled")]
public int EnableIfToggled;
[DisableIf("IsToggled")]
public int DisableIfToggled;
[DisableIf("SomeObject")]
public Vector3 EnableWhenNull;
ShowIfAttribute & HideIfAttribute
简介
使用ShowIf和HideIf可以在某些条件将属性在面板中进行显示或隐藏
API详解
-
Fields
类 型 名 称 说 明 string MemberName 指定一个字段、属性或者方法的名称,如果返回值为bool型,则直接使用返回值;如果有指定Value,则会根据Value进行判断 object Value 用于判断的值 bool Animate 指定当状态变化时是否需要使用动画 Properties
-
Constructors
参 数 描 述 string memberName, bool animate = true 各个参数的含义参见Fields中说明 string memberName, object optionalValue, bool animate = true 各个参数的含义参见Fields中说明
示例
public class ShowAndHideIfExamples : MonoBehaviour
{
public UnityEngine.Object SomeObject;
[EnumToggleButtons]
public InfoMessageType SomeEnum;
public bool IsToggled;
[ShowIf("SomeEnum", InfoMessageType.Info)]
public Vector3 Info;
[ShowIf("SomeEnum", InfoMessageType.Warning)]
public Vector2 Warning;
[ShowIf("SomeEnum", InfoMessageType.Error)]
public Vector3 Error;
[ShowIf("IsToggled")]
public Vector2 VisibleWhenToggled;
[HideIf("IsToggled")]
public Vector3 HiddenWhenToggled;
[HideIf("SomeObject")]
public Vector3 ShowWhenNull;
[ShowIf("SomeObject")]
public Vector3 HideWhenNull;
}
HideInEditorModeAttribute & HideInPlayModeAttribute
简介
HideInEditorMode可以在编译器模式下不再绘制标记的属性,HideInPlayMode可以在运行模式下不再绘制标记的属性
API详解
- Fields
- Properties
- Constructors
示例
见DisableInEditorModeAttribute & DisableInPlayModeAttribute
DisableInEditorModeAttribute & DisableInPlayModeAttribute
简介
使用DisableInEditorMode可以将属性在编译器模式下禁用编辑属性,使用DisableInPlayMode可以将属性在运行期禁止编辑
仅可适用于字段、方法及属性
API详解
- Fields
- Properties
- Constructors
示例
public class HideInEditorAndPlayModeExamples : MonoBehaviour
{
[Title("Hidden in play mode")]
[HideInPlayMode]
public int A;
[HideInPlayMode]
public int B;
[Title("Hidden in editor mode")]
[HideInEditorMode]
public int C;
[HideInEditorMode]
public int D;
[Title("Disable in play mode")]
[DisableInPlayMode]
public int E;
[DisableInPlayMode]
public int F;
[Title("Disable in editor mode")]
[DisableInEditorMode]
public int G;
[DisableInEditorMode]
public int H;
}
HideInPlayMode & HideInEditorMode & DisableInPlayMode & DisableInEditorMode
TODO:
RequiredAttribute
简介
Required属性标签标识可以用提示指定的属性必须有值,同时可以指明提示信息的类型
API详解
-
Fields
类 型 名 称 说 明 string ErrorMessage 错误提示信息 InfoMessageType MessageType 提示信息类型(None,Info,Warning,Error) Properties
-
Constructors
参 数 描 述 无 string errorMessage, InfoMessageType messageType 各个参数的含义参见Fields中说明 string errorMessage 各个参数的含义参见Fields中说明 InfoMessageType messageType 各个参数的含义参见Fields中说明
示例
public class RequiredExamples : MonoBehaviour
{
[Required]
public GameObject MyGameObject;
[Required("Custom error message.")]
public Rigidbody MyRigidbody;
[InfoBox("Use $ to indicate a member string as message.")]
[Required("$DynamicMessage")]
public GameObject GameObject;
public string DynamicMessage = "Dynamic error message";
}
ReadOnlyAttribute
简介
使用ReadOnly属性标签可以禁止对指定的属性进行修改
API详解
- Fields
- Properties
- Constructors
示例
public class ReadOnlyExamples : MonoBehaviour
{
[ReadOnly]
public string MyString = "This is displayed as text";
[ReadOnly]
public int MyInt = 9001;
[ReadOnly]
public int[] MyIntList = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
}
AssetsOnlyAttribute & SceneObjectOnlyAttribute
简介
AssetOnly属性标签可以标记需要Object的属性,强制限定些属性只能使用Project中的资源,而不能使用Scene中的资源(如果你希望给属性、字段赋值只能使用Project中的资源,而非Scene中的资源可以使用此标签)
SceneObjectOnly属性标签与AssetOnly标签恰好相反
API详解
- Fields
- Properties
- Constructors
示例
public MyComponent : MonoBehaviour
{
[AssetsOnly]
public GameObject MyPrefab;
}
ValidateInputAttribute
简介
API详解
-
Fields
类 型 名 称 说 明 string DefaultMessage 提示信息 string MemberName 指定进行验证的方法名称,此方法至少要有一个参数,此参数的类型与属性类型必须一致 InfoMessageType MessageType 消息类型 bool IncludeChildren 是否对子值进行验证,默认是true bool ContiniousValidationCheck 当值为true时,修改属性值并不会立即触发验证执行,在面板中每帧会执行一次 Properties
-
Constructors
参 数 描 述 string memberName, string defaultMessage = null, InfoMessageType messageType = InfoMessageType.Error 各个参数的含义参见Fields中说明
示例
HideInPrefabAssets & HideInPrefabInstances & HideInPrefabs & HideInNonPrefabs & DisableInPrefabAssets & DisableInPrefabInstances & DisableInPrefabs & DisableInNonPrefabs
TODO:
TypeFilterAttribute
简介
使用TypeFilter属性标签可以使用属性从绘制的下拉列表中选择指定的类型
API详解
-
Fields
类 型 名 称 说 明 string DropdownTitle 下拉列表标题,默认是Null string MemberName 指字一个字段、属性或者方法的名称,此类型或者返回值必须继承自IList,比如List,Array Properties
-
Constructors
参 数 描 述 string memberName 各个参数的含义参见Fields中说明