コガネブログ

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

【Unity】プロパティを Inspector で編集できる「Property Backing Field Drawer」紹介(無料)

f:id:baba_s:20171101191206p:plain

概要

2014/6/7 に「Property Backing Field Drawer」がリリースされました

「Property Backing Field Drawer」を導入すると、
プロパティを Inspector で編集できるようになります

検証環境

  • Unity 2017.1.1f1
  • Property Backing Field Drawer 3.3.14

使い方

using Candlelight;
using UnityEngine;

public class Example : MonoBehaviour
{
    [SerializeField, PropertyBackingField]
    private int m_Int = 0;

    public int Int
    {
        get { return m_Int; }
        set { m_Int = value; Debug.Log( value ); }
    }
}

f:id:baba_s:20171101191622p:plain

上記のように、PropertyBackingField 属性を変数に適用して、プロパティを用意することで
Inspector でプロパティを通じて変数を編集できるようになります
例えば上記のコードでは Inspector で値を編集するたびにログが出力されます

using Candlelight;
using UnityEngine;

public class Example : MonoBehaviour
{
    [SerializeField, PropertyBackingField( "IntProperty" )]
    private int m_intField = 0;

    public int IntProperty
    {
        get { return m_intField; }
        set { m_intField = value; Debug.Log( value ); }
    }
}

PropertyBackingField 属性にプロパティ名を指定することで
変数を好きなプロパティと紐付けることができます

関連記事