Unity 3D – Debugging and Testing !

Debugging can be a really annoying task.
You can avoid a lot of debugging when testing your modules separately and thinking your design correctly.

How do I test my Unity application ?

Unit Testing

You should test your application both “locally” (at the Class scope) and “globally” (after the integration process – test integration with your other Classes, and with you Scene itself).

For local testing, you can use standard C# Unit Testing but it would give you a bigger amount or work. Even if going for this solution, use Unity Testing Tools instead which provides more flexibility.
A simpler way to test simple modules is to use this simple pattern that allow you more simplicity and less code to write :

#ifdef UNITY_EDITOR
void Update()
{
    if (Input.GetKeyUp("k")
    {
        MyFunction();
    }
}
#endif

Just ensure that you meet your Specifications at the time the module is implemented and commit your changes only when it is done.

Visual Studio Breakpoints & Tracepoints

When you start programming, you are trying to follow the track of which lines of code are executing sequentially by adding Debug.Logs.
The approach is great but as you are working with Multithreaded applications (each Monobehaviour Class runs in concurrency !), you will not easily focus on the feature you have to debug. Moreover, you won’t have a full visibility on your variables values.

Use breakpoints !
– After implementing a feature, just take some time thinking where you should put breakpoints in your code.
– After debugging, just remove them so that it won’t impact future debugging sessions.

Also, when working in Realtime/Multiplayer applications, you could set Tracepoints instead to keep the realtime sequencing unchanged (see Visual Studio Output Console).

The Profiler

In Unity, you can display the Profiler window by selecting Window > Profiler.
It is useful in some cases where your application crashes, you can analyse how much processing is required for the GPU, CPU in realtime.
You can deduce if you introduced too much processing at a given time, or if you are using too much drawcalls.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *