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.

Tutorial – Ambisonics in Unity

A Quick Overview of Ambisonics

With the rise of VR and 360 degree videos, Ambisonic audio has gained popularity in recent years and have far more applications now than they did when they were created in the 1970s. Simply put, Ambisonics are a method for recording and playing back 360 degree audio – this is commonly utilised for fixed point viewing and is particularly useful for 360 video and fixed point VR.

The primary difference between multi-channel Ambisonic recordings and multi-channel surround sound files is that whilst surround sound audio files have tracks that are routed to specific speaker setups such as 5.1 or 7.1, Ambisonics are speaker agnostic and can be decoded into any speaker array. The multiple channels exist to represent the full 3D representation of sound from the recording point.

The most common format for Ambisonics at the moment ‘B-format’ WAV files. This is the format that Unity uses, specifying that it should also use ACN component ordering and SN3D normalization.

To actually record these things in a way that’s compatible with Unity you’ll need a 4-channel recorder that will record in the settings specified above and something like this microphone here.

Ambisonics in Unity

Okay, now we’ve gotten past the “what the hell actually are these things?” section – we can carry on to how we actually use them in Unity. If you don’t have any B-format ambisonic recordings on hand but want to test this out, then you can grab some example files that will work from the lovely folks for work on Ambisonic Toolkit.

Creating an Ambisonic Audio Source is actually a really quick and easy process in Unity, so it shouldn’t take long until you’re up and running.

Project Settings

The first step to using Ambisonics in Unity is to specify the decoding plugin to use in the Audio Project Settings (Edit > Project Settings > Audio). The setting we’re interested in here is the Ambisonic Decoder Plugin, and we want to make sure it’s set to ‘Resonance Audio’ (Resonance Audio is the Google Audio plugin which is now natively tied into Unity 2018. If you are using a version of Unity prior to 2018.1, you can download the Resonance Audio packages here – it’s actually not a bad idea to use this package anyway as it seems like Unity’s native integration is still not ‘feature full’.

Import Settings

Next up, just drag into Unity the Ambisonics you want to use (if you’re using the examples from Ambisonic Toolkit then any of the B-Format or 4-Channel files seem to work okay). Once imported, you need to select all of the Ambisonics you want to try out and ensure they’re marked as ‘Ambisonic’ in the files Import Settings.

Creating an Audio Source and Mixer

Before we go ahead and start creating the Audio Source, we need to create an Audio Mixer that utilises the Resonance Audio Renderer. To do this, open up the Audio Mixer window (Window > Audio > Audio Mixer in 2018.2 or CTRL+8) and create a new Audio Mixer. For simplicity, we’re just going to add the Resonance Audio Renderer onto the Master channel using the ‘add’ button at the bottom of the channel. This is where we will route our audio source once it’s created.


Finally, we simply create a new Audio Source (Create > Audio > Audio Source) and edit the Inspector settings of this new Game Object so that the settings look something like the below, setting our Audio Clip to the Ambisonic we want to test and the Mixer Group to the one we just created. We also need to ensure that the ‘Spatialize’ option is turned off and that the Spatial Blend is set over to 2D.


Hey presto! That’s all you need to do in order to the Ambisonics working in Unity. This will give you a global soundscape that will automatically update the audio relative the the current positioning of the Audio Listener. I’ve made a quick example of this integration which you can see below.