うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】Unity 用のデバッグツールを公開

あらまし

実機では画面に表示、UnityEditor では EditorWindow で
同じデバッグメニューができれば便利だと思い作成してみました。

※ 実機用 UI はUIElement が runtime UI として使えるようになってから作成する予定なので
現状は EditorWindow で利用するのみです

f:id:hacchi_man:20200202235746p:plain

リポジトリ

github.com

機能

Field, Property, Method をデバッグメニューに登録することができ
Field, Property であればパラメータの変更
Method であればそのメソッドを実行できます

この機能を利用するには DebugObserver に登録する必要があるので
Attribute を記述したクラスに下記の処理を書く必要があります

public class SampleDebugBehaviour : MonoBehaviour
{
    private void Awake()
    {
        // デバッグメニューに登録
        DebugObserver.Register(this);
    }
    
    private void OnDestroy()
    {
        // デバッグメニューの登録解除
        DebugObserver.Release(this);
    }

対応型

クラスや構造体の対応を行うのは面倒だったので、Primitive型と一部Unityで利用される構造体型に対応しています

  • int
  • float
  • string
  • bool
  • enum
  • Color
  • Vector2
  • Vector3

Field

Field に 対して DebugFieldAttribute を記述することで
Field をデバッグメニューに登録できます

DebugFieldAttribute

public DebugFieldAttribute(string path = "", string name = "", bool isReadOnly = false);

変数 説明
path 表示パス
name 表示名 (空文字だった場合 Field 名)
isReadOnly 編集不可能かどうか

サンプル

f:id:hacchi_man:20200202230810p:plain:w400

       [DebugField]
        private float _debugFloatValue;

        [DebugField("", "enum")]
        private TextAnchor _textAnchor;
        
        [DebugField("Field")]
        private int _debugIntValue;

        [DebugField("Field", "string")]
        private string _debugStringValue;

        [DebugField("Field", "Color", true)]
        private Color _debugColorValue;

Property

Property は Field とほとんど変わりなく setter が無い場合
自動的に readonly になるくらいです

DebugPropertyAttribute

public DebugPropertyAttribute(string path = "", string name = "", bool isReadOnly = false);

変数 説明
path 表示パス
name 表示名 (空文字だった場合 Field 名)
isReadOnly 編集不可能かどうか

サンプル

f:id:hacchi_man:20200202231640p:plain:w400

       private float _debugFloatValue;
        private Vector3 _debugVector3Value;
        
        [DebugProperty("Property")]
        private float _debugFloatProperty
        {
            get { return _debugFloatValue; }
            set { _debugFloatValue = value; }
        }
        
        [DebugProperty("Property/Disable")]
        private Vector3 _debugVector3Property
        {
            get { return _debugVector3Value; }
        }
        
                
        [DebugProperty("Property", "IntProp")]
        private int _debugIntProperty
        {
            get; 
            set;
        }

Method

Method では デバッグメニューメソッドの実行をすることができます 引数があった場合でも引数を指定したり 引数の値を固定で実行できたりします

DebugMethodAttribute

public DebugMethodAttribute(string path = "", string name = "");
public DebugMethodAttribute(string path = "", string name = "", params string[] args);
public DebugMethodAttribute(string path = "", string name = "", params object[] parameters);

変数 説明
path 表示パス
name 表示名 (空文字だった場合 Field 名)
args 引数の表示名
parameters 引数値を固定する際のパラメータ

サンプル

f:id:hacchi_man:20200202234817p:plain

       [DebugMethod("Method", "Test")]
        private void DebugMethod()
        {
            Debug.LogError("Click");
        }
        
        [DebugMethod("Method/Args", "IntValue", "int")]
        private void DebugMethodArgsInt(int intValue)
        {
            Debug.Log(intValue);
        }
        
        [DebugMethod("Method/Args", "IntValue2", "int1", "int2")]
        private void DebugMethodArgsInt(int intValue1, int intValue2)
        {
            Debug.Log(intValue1);
            Debug.Log(intValue2);
        }

        [DebugMethod("TimeScale", "TimeScale1", 1f)]
        [DebugMethod("TimeScale", "TimeScale2", 2f)]
        private void TimeScale(float scale)
        {
            Time.timeScale = scale;
            Debug.Log(scale);
        }