LoginSignup
19
17

More than 5 years have passed since last update.

UnityでAssert

Last updated at Posted at 2014-11-14

[2016/04/06追記]
気づいたらDebugクラスにもAssert関数がありました\(^o^)/
http://docs.unity3d.com/ja/current/ScriptReference/Debug.Assert.html
これで Debug.Log とかと同等に扱えるね!やったね!
[追記ここまで]


[2015/06/27追記]
Unity 5.1 から、Unity公式でAssertが用意されたようです٩( 'ω' )و
http://docs.unity3d.com/ScriptReference/Assertions.Assert.html
もう自前で書かなくてもいいね!やったね!
[追記ここまで]


とりあえずC#でのAssertってどうするの?って思ってMSDNを調べてみた。

Debug.Assert メソッド (Boolean) (System.Diagnostics)

なるほど。Unityでどう動くのかやってみよう!

// Use this for initialization
void Start () {
    System.Diagnostics.Debug.Assert( false, "assert test message." );
}

しかし何も起きなかった。無念。
というわけで自分で実装することに。

[System.Diagnostics.Conditional( "DEBUG" )]
void Assert( bool condition, string message ) {
    if( !condition ) {
        UnityEngine.Debug.LogError( message );
    }
}

System.Diagnostics.Conditional 属性を付与してるので、使う場合は [Player Settings] > [Other Settings] > [Configuration] > [Scripting Define Symbols] で DEBUG を定義しておく。
そうすることで、リリース時には呼び出しもされず、関数も事実上存在しないものに出来る。らしい。
まだ運用で試してないけど上手く扱えそう。

Conditional (C# プログラミング ガイド)

19
17
2

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
19
17