Tuesday 21 January 2014

My C# (Unity3D) Assert()/Log() signature combines Conditional and pre-processor

Someone queried my logging method signature in a patch I offered recently so I thought I'd share the technique and explanation.  Forewarnings:

  • it's C# (WHAT?! When did I switch? Explanation to come, I guess!)
  • it's for performance (so sacrifices some immediate clarity to that mistress).


#if !DEBUG
    [System.Diagnostics.Conditional("UNITY_EDITOR")]
#endif
    public static void Assert(bool comparison, string messageFmt, params System.Object[] args) { ... }
    // also used on logging


An explanation in two parts, (1) the Conditional and (2) how the #if interacts with it.