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’.

AudioManagerSettings
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.

ImportSettings
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.

AudioMixer

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.

AudioSource

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.