LoginSignup
13
13

More than 3 years have passed since last update.

【Unity】インスペクタの表示を動的に変更する

Last updated at Posted at 2019-02-11

インスペクタのプロパティ変更に応じて、表示内容を動的に変更する。

チェックボックスの有無で、各設定の表示/非表示が切り替わるだけの、簡単なサンプル

  • 動作結果
    20190211.gif

  • GameObject側

HogeEditor.cs
using UnityEngine;
public class HogeObject : MonoBehaviour
{
    public bool EnableShadow = false;
    public ShadowSetteing Setteing = new ShadowSetteing();

    [System.Serializable]
    public class ShadowSetteing
    {
        public Color EffectColor;
        public Vector2 Distance;
        public bool UseAlpha;
    }
}
  • Editor側
HogeObjectEditor.cs
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(HogeObject))]
public class HogeObjectEditor : Editor
{
    private HogeObject _target;

    private void Awake()
    {
        _target = target as HogeObject;
    }

    public override void OnInspectorGUI()
    {
        EditorGUI.BeginChangeCheck();

        _target.EnableShadow = EditorGUILayout.ToggleLeft("EnableShadow", _target.EnableShadow);
        if (_target.EnableShadow)
        {
            EditorGUILayout.LabelField("影の設定");
            _target.Setteing.EffectColor = EditorGUILayout.ColorField("色", _target.Setteing.EffectColor);
            _target.Setteing.Distance = EditorGUILayout.Vector2Field("距離", _target.Setteing.Distance);
            _target.Setteing.UseAlpha = EditorGUILayout.Toggle("透過", _target.Setteing.UseAlpha);
        }

        // GUIの更新があったら実行
        if (EditorGUI.EndChangeCheck())
        {
            EditorUtility.SetDirty(_target);
        }
    }
}
#endif
13
13
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13