LoginSignup
85
82

More than 5 years have passed since last update.

UnityのInspectorで変数を表示する方法まとめ

Last updated at Posted at 2014-05-19

UnityのInspectorでの変数の表示についてまとめてみた

表示する方法

  • 変数をpublicフィールドにする

public string hoge

  • [SerializeField]属性を変数の前に書く
[SerializeField]
private string huga;
  • [System.Serializable]属性をクラス定義の前に書く
[System.Serializable]
public class HogeClass {
    public string hoge;
}

public class NewBehabiourScript : MonoBehaviour {

    public HogeClass hogeData;
}
  • InspectorをDebugモードにする

表示しない方法
* [HideInInspector]属性を変数の前に書く

[HideInInspector]
public string hoge;

隠しているだけで、プレハブに値が保持されているので、後でスクリプトの変数の初期値を変えても、プレハブに値がのこっていて、動作が変わらないというトラブルに注意
[System.NonSerialized]のほうがトラブルが少ない

  • [System.NonSerialized]属性を変数の前に書く
[System.NonSerialized]
public string fuga;

パラメータをそもそもプレハブに保存しないので、当然Inspectorに表示されない
publicだけど、Inspectorで触ってほしくないときに使う

  • Editorスクリプトで1から作る

詳細は省略 レイアウトを自分で決めるので表示非表示も自由

どうしても表示できないもの

  • structのパラメーター

structは属性をつけても表示できないので、classにするしかない模様
(unityのVector3構造体とかがなぜ表示できるかは謎ですが)

勘違いorバージョンアップでstructも表示できるようです
classと同様に[System.NonSerialized]を付加すればOKです

  • ジェネリッククラス

ジェネリックスは[System.Serializable]属性を与えてもインスペクターに表示することも、Editorスクリプトでアクセスすることもできない。
対策として、ジェネリッククラスを継承した別のクラスを作れば表示できる

(ちなみにDictionaryは structのKeyValuePair + ジェネリックスの二重苦なので普通は表示できない
無理矢理な対策はこちら http://qiita.com/k_yanase/items/fb64ccfe1c14567a907d )

85
82
0

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
85
82