Unity Odin (实现如图所示效果)(四)
代码
using UnityEngine ;
public class Test4 : MonoBehaviour
{
public string characterName;
public string characterInfo;
private int _characterAge;
public Texture2D icon;
public GameObject prefab;
public Stats stats;
}
[ System. Serializable ]
public class Stats
{
public int s1;
public int s2;
public int s3;
public int s4;
}
using System ;
using System. Collections. Generic ;
using System. Reflection ;
using Sirenix. OdinInspector ;
using Sirenix. OdinInspector. Editor ;
using Sirenix. Utilities ;
using UnityEngine ;
public class CharacterAttributeProcessor< T> : OdinAttributeProcessor< T> where T : Test4
{
public override void ProcessChildMemberAttributes ( InspectorProperty parentProperty, MemberInfo member, List< Attribute> attributes)
{
attributes. Add ( new LabelWidthAttribute ( 100 ) ) ;
if ( member. Name == "icon" )
{
attributes. Add ( new PropertyOrderAttribute ( - 1 ) ) ;
attributes. Add ( new HideLabelAttribute ( ) ) ;
attributes. Add ( new HorizontalGroupAttribute ( "base" , 100 ) ) ;
attributes. Add ( new PreviewFieldAttribute ( 100 , ObjectFieldAlignment. Center) ) ;
}
if ( member. GetReturnType ( ) == typeof ( string ) )
{
attributes. Add ( new VerticalGroupAttribute ( "base/right" ) ) ;
}
switch ( member. Name)
{
case "prefab" :
attributes. Add ( new VerticalGroupAttribute ( "base/right" ) ) ;
attributes. Add ( new InlineEditorAttribute ( InlineEditorModes. LargePreview) ) ;
attributes. Add ( new RequiredAttribute ( ) ) ;
break ;
case "_characterAge" :
attributes. Add ( new ShowInInspectorAttribute ( ) ) ;
attributes. Add ( new VerticalGroupAttribute ( "base/right" ) ) ;
break ;
}
}
}
public class StatsAttributeProcessor : OdinAttributeProcessor< Stats>
{
public override void ProcessChildMemberAttributes ( InspectorProperty parentProperty, MemberInfo member, List< Attribute> attributes)
{
attributes. Add ( new BoxGroupAttribute ( "Box" ) ) ;
attributes. Add ( new RangeAttribute ( 10 , 20 ) ) ;
}
public override void ProcessSelfAttributes ( InspectorProperty property, List< Attribute> attributes)
{
attributes. Add ( new PropertySpaceAttribute ( 50 ) ) ;
attributes. Add ( new HideLabelAttribute ( ) ) ;
}
}