Unity Tips – XML Comments

You can (and should) use XML comments to more effectively document your code, including Classes, Fields, Properties and Methods. XML comments will also populate the description for these elements when you hover over them in Visual Studio – this is particularly helpful for Classes that contain public members.

By using XML comments, you can then simply hover over the public member being used in another class and Visual Studio will give you the summary of what that member does.

An example of XML comments in Visual Studio

You can quickly add the template of an XML comment by typing ‘///’ above the member you want to comment. Visual Studio will automatically populate the <summary> structure for you, and give you the appropriate return and argument tags included in the new comment.

You can also use these XML comments to generate HTML documentation using tools such as Doxygen, and preview these types of documentation within Visual Studio with extensions like DocPreview.

DocPreview’s generated XML documentation for this method example.

Unity Tips – Rich Text in the Console

As Unity supports Rich Text tags, you use them in your Debug.Log methods in order to liven up your Console window and put emphasis on important Debug lines. Whilst some Rich Text tags will be more useful than others in this context, some helpful examples would be Color, Bold, Italics and maybe even Size, depending on how you have your Console setup.

An example of Rich Text being used in the Console.

Here are some examples of Rich Text tags in use in code. A good idea would be to write extension methods for commonly used debug lines so that you don’t have to keep writing out the same tags.

Debug.Log($"This object's name is <b><color=white>{gameObject.name}</color></b>!");

Debug.Log("<color=green><b>Everything has gone okay!</b></color> <i><size=8>Hooray</size></i>");

Debug.LogError("<color=red><b>[ERROR]</b></color> - Something has gone wrong.");

Unity Tips – Memory Profiler

Unity has a very helpful tool availability within it’s Package Manager for analysing memory usage known as the ‘Memory Profiler’. This tool is very helpful to optimise your game as it allows you to see what is currently loaded into memory at a given moment in time.

The Memory Profiler will show you in chunks what is being used at an overview, and you can click on these overviews to see exactly what assets are being loaded to contribute to that. See my very creatively named “RawImage_File” in the “Texture2D” section in the image below.

At the time of writing, the Memory Profiler is currently in preview, so in order to import the Memory Profiler into your project, you will need to bring it in from the Package Manager. You will need to ‘Show preview packages’ to be able to find it. Even though it’s in preview, it’s still very helpful, so don’t dismiss it even if you come across a few stability issues (which is likely).

Make preview packages available by clicking ‘Show preview packages’ in the ‘Advanced’ menu.

You can open the Memory Profiler from “Window > Analysis > Memory Profiler”. Once open you can take a snapshot of the current memory state by hitting “Capture Player”. You can then open this snapshot up to see it by pressing the open button in the newly created list entry.

You can also load two memory snapshots at once in order to compare them easier. Though this package is still in preview at the moment (and will be for a while i’m guessing), it’s a very helpful tool and is definitely worth checking out.

Unity Tips – Scene View Layers

You can hide layers in the Scene view by clicking on the ‘Layers’ dropdown in the top right of the Unity Editor. By clicking on the eyes in the dropdown, you are able to toggle elements on and off in the scene view. There is also a shortcut for ‘Nothing’ and ‘Everything’.

The ‘Layers’ dropdown with the UI layer hidden.

Here is an example of the ‘UI’ layer being hidden in the scene view…

And here is the exact same scene with the ‘UI’ layer showing…

This is particularly helpful for isolating UI canvases in some scenes or keeping them out of the way whilst you’re working on the environment. It is also helpful for isolating sections of the environment and keeping the scene view uncluttered.

Unity Tips – Colour Libraries

You can save libraries of colours as an Asset in Unity to be used in the Colour Picker. This is particularly helpful when using version control and syncing up with others working on the same project.

An example of an inspected Colour Library when saved as an ‘Project’ Asset

To save (or load) a colour library, you should expand the ‘Swatches’ section in the Colour Picker and click on the menu icon next to the header. From here you can change how you show your swatches (Grid/List), select libraries to load and create new ones.

When creating a new library, you can give it a name and specify where you want it to be created. If you use ‘Project Folder’ it will be local to the current project, helpful for version control! Preferences Folder’ will load for every project. A ‘Project Folder’ library will automatically be saved in the ‘Assets/Editor’ folder, and will create you a new one if one doesn’t already exist.

A loaded colour library will show all of your colours under ‘Swatches’ – you can only load one library at a time but can switch between them whenever you need.

Unity Tips – Banker’s Rounding

As Unity uses the .NET runtime, both the Mathf.RoundToInt() and Mathf.Round() methods use “Banker’s Rounding”. This is a rounding system that rounds numbers to the nearest even integer when rounding from the midpoint (.5). The “Banker’s Rounding” approach works best when dealing with large quantities of numbers and exists to avoid rounding error. However, this may be not what you’re expecting when you’re working in Unity!

You can use the .NET System.Math library in instead of UnityEngine.Mathf to implement a more conventional rounding system by passing through MidpointRounding.AwayFromZero as an argument to the Math.Round method. Be warned that the default for Round is also “Banker’s Rounding” so if you don’t use this, you will still get the same results as Unity’s version. See below for an example.

(I was stupid in this and forgot to re-comment the netRoundingA, it actually returns 3 which was the whole point of this)

You will also need to cast the return to an int, as the Math.Round method will return a double instead.

Unity Tips – Serializable Classes

Clean up your inspector and group variables together with Serializable Classes! If you tag a POCO class with the [Serializable] attribute, Unity will be able to serialize it into the inspector for you.

Bonus tip, if you add a string field to the top of the class, Unity will use this as the name of the element when serializing these classes into an array – much easier to read and keep on top in place of ‘Element 0’, ‘Element 1’, ‘Element 2’ etc.