コガネブログ

平日更新を目標に Unity や C#、Visual Studio などのゲーム開発アレコレを書いていきます

【Unity】26個の PropertyAttribute が使用できる「NaughtyAttributes」紹介

はじめに

「NaughtyAttributes」を Unity プロジェクトに導入することで
26個の PropertyAttribute が使用できるようになります

目次

Slider

public class NaughtyComponent : MonoBehaviour
{
    [Slider( 0, 10 )] public int intSlider;
    [Slider( 0f, 10f )] public float floatSlider;
}

f:id:baba_s:20171224145150p:plain

MinMaxSlider

public class NaughtyComponent : MonoBehaviour
{
    [MinMaxSlider( 0f, 100f )] public Vector2 minMaxSlider;
}

f:id:baba_s:20171224145158p:plain

ReorderableList

public class NaughtyComponent : MonoBehaviour
{
    [ReorderableList] public int[] intArray;
    [ReorderableList] public List<float> floatArray;
}

f:id:baba_s:20171224145211g:plain

Button

public class NaughtyComponent : MonoBehaviour
{
    [Button]
    private void MethodOne() {}
    
    [Button( "Button Text" )]
    private void MethodOne() {}
}

f:id:baba_s:20171224145227p:plain

Dropdown

public class NaughtyComponent : MonoBehaviour
{
    [Dropdown( "intValues" )] public int intValue;
    [Dropdown( "stringValues" )] public string stringValue;
    [Dropdown( "vectorValues" )] public Vector3 vectorValue;
    
    private int[] intValues = new [] { 1, 2, 3, 4, 5 };
    
    private string[] stringValues = new [] { "A", "B", "C", "D", "E" };
    
    private DropdownList<Vector3> vectorValues = new DropdownList<Vector3>
    {
        { "Right"  , Vector3.right   }, 
        { "Left"   , Vector3.left    }, 
        { "Up"     , Vector3.up      }, 
        { "Down"   , Vector3.down    }, 
        { "Forward", Vector3.forward }, 
        { "Back"   , Vector3.back    }, 
    };
}

f:id:baba_s:20171224145235g:plain

ResizableTextArea

public class NaughtyComponent : MonoBehaviour
{
    [ResizableTextArea] public string resizableTextArea;
}

f:id:baba_s:20171224145256g:plain

ShowNonSerializedField

public class NaughtyComponent : MonoBehaviour
{
    [ShowNonSerializedField] private int myInt = 10;
    [ShowNonSerializedField] private const float PI = 3.14159f;
    [ShowNonSerializedField] private static readonly Vector3 CONST_VECTOR = new Vector3( 1, 1, 1 );
}

f:id:baba_s:20171224145311p:plain

ShowNativeProperty

public class NaughtyComponent : MonoBehaviour
{
    public List<Transform> transforms;
    
    [ShowNativeProperty]
    public int TransformCount { get { return transforms.Count; } }
}

f:id:baba_s:20171224145320p:plain

ReadOnly

public class NaughtyComponent : MonoBehaviour
{
    [ReadOnly] public Vector3 forwardVector = Vector3.forward;
}

f:id:baba_s:20171224145334p:plain

ShowAssetPreview

public class NaughtyComponent : MonoBehaviour
{
    [ShowAssetPreview] public Sprite sprite;
    [ShowAssetPreview( 128, 128 )] public GameObject prefab;
}

f:id:baba_s:20171224145341p:plain

ShowIf / HideIf

public class NaughtyComponent : MonoBehaviour
{
    public bool showInt;
    
    [ShowIf( "showInt" ) public int myInt;
    [ShowIf( "AlwaysShow" )] public float myFloat;
    [ShowIf( "NeverShow" )] public Vector3 myVector;
    
    private bool AlwaysShow() { return true; }
    private bool NeverShow() { return false; }
}

f:id:baba_s:20171224145348g:plain

BoxGroup

public class NaughtyComponent : MonoBehaviour
{
    [BoxGroup( "Integers" )] public int firstInt;
    [BoxGroup( "Integers" )] public int secondInt;
    [BoxGroup( "Floats" )] public float firstFloat;
    [BoxGroup( "Floats" )] public float secondFloat;
}

f:id:baba_s:20171224145355p:plain

MinValue / MaxValue

public class NaughtyComponent : MonoBehaviour
{
    [MinValue( 0 ), MaxValue( 10 )] public int myInt;
    [MinValue( 0f )] public float myFloat;
}

f:id:baba_s:20171224145401g:plain

Required

public class NaughtyComponent : MonoBehaviour
{
    [Required] public Transform myTransform;
    [Required( "Custom required text" )] public GameObject myGameObject;
}

f:id:baba_s:20171224145423p:plain

ValidateInput

public class NaughtyComponent : MonoBehaviour
{
    [ValidateInput( "IsNotNull" )]
    public Transform myTransform;
    
    [ValidateInput( "IsGreaterThanZero", "myInteger must be greater than zero" )]
    public int myInt;
    
    private bool IsNotNull( Transform transform )
    {
        return transform != null;
    }
    
    private bool IsGreaterThanZero( int integer )
    {
        return integer > 0;
    }
}

f:id:baba_s:20171224145438p:plain

Section

public class NaughtyComponent : MonoBehaviour
{
    [Section( "Integers" )]
    public int firstInt;
    public int secondInt;
    
    [Section( "Floats" )]
    public float firstFloat;
    public float secondFloat;
}

f:id:baba_s:20171224145444p:plain

BlankSpace

public class NaughtyComponent : MonoBehaviour
{
    public int someInt;
    
    [BlankSpace]
    public float someFloat;
}

f:id:baba_s:20171224145456p:plain

InfoBox

public class NaughtyComponent : MonoBehaviour
{
    [InfoBox( "myInt is not even", InfoBoxType.Error, "IsMyIntNotEven" )]
    public int myInt;
    
    [InfoBox( "This is myFloat" )]
    public float myFloat;
    
    [InfoBox( "This is myVector", InfoBoxType.Warning )]
    public Vector3 myVector;
    
    private bool isMyIntNotEven()
    {
        return myInt % 2 != 0;
    }
}

f:id:baba_s:20171224145508p:plain

OnValueChanged

public class NaughtyComponent : MonoBehaviour
{
    [OnValueChanged( "OnValueChanged" )]
    public int myInt;
    
    private void OnValueChanged()
    {
        Debug.Log( myInt );
    }
}

関連記事