Game Development Reference
In-Depth Information
String.Format
Line 30 of the code sample 2-3 uses the String.Format function
to build a complete string. This function is useful when you need
to make one long string that includes both literal statements and
variable values, which could be different types. By inserting the
tokens {0}, {1}, {2}… inside the string argument, the Format
function will substitute them for the subsequent function arguments
in the order in which they are provided; that is, String.Format
will concatenate your string argument at the location of the tokens,
with string versions of your function arguments. Thus, the string
{0} will be replaced with OgreName.ToString() . For more
information on String.Format , see the online documentation
at http://msdn.microsoft.com/en-us/library/system.
string.format%28v=vs.110%29.aspx .
You can section off and isolate blocks of code between the release and debug versions
that allow you to run debug-specific code when specific flags are enabled. When
debugging games, for example, you'll frequently develop two sets or variations of
code: the release code and the debug code. Imagine a common scenario where to find
and resolve a bug in the code, you resort to inserting the Debug.Log statements that
print out the values of variables and states of classes. You might even insert additional
lines, such as if statements and loops, to test out alternative scenarios and explore how
objects react. After amending the code for a while, the problem seems repaired, so you
remove the additional debug code and continue testing as you did earlier. However,
later, you discover that the problem has returned, or a similar one has arisen. So now
you wish you'd kept the debug code after all, because it'd be useful again. You might
promise yourself that next time, you'll simply comment out the debug code as opposed
to deleting it entirely. This will let you simply remove the commenting should the
code be needed again. However, of course, having to comment and uncomment code
is also tedious, especially if there're many lines, and they are scattered across multiple
files and parts of files. You can, however, resolve this problem and similar ones using
custom global defines. In essence, a global define is a special preprocessor flag that
you can enable or disable to conditionally compile or exclude blocks of your code. By
setting the flag to true , Unity will automatically compile one version of your code,
and by setting it to false , Unity will compile the other version. This will allow you to
maintain two versions or variations of your code in only one set of source files: one for
debug and one for release. Let's see this in practice in Unity. Consider the following
code sample 2-5:
01 using UnityEngine;
 
Search WWH ::




Custom Search