Auto compile-time support for Unity Animation Parameters

Paul Marsh
3 min readMay 12, 2021

--

Previously I wrote about the problems in Unity of having to use string literals for Tags. In the article I provided a solution to automatically generate constants to be able to provide compile-time checking. I have now enhanced this to support Animation Parameters too.

The Problem — magic strings

When you write code to change an animation parameter you end up having to use a string. E.g.

Code to change an animation parameter

It is a very brittle technique. Have you spelt it correctly, has a animation designer changed the parameter, what are the available parameters when you add new code, etc.

Automatically generate a constants file

When I edit the parameters of a animation controller, I want the parameter to be more concretely available to my code.

Changing an Animation

In this example I have opened the SolvalouController animation controller and added an IsReady parameter. I then must save the project.

Now if I look in my Globals folder, not only do I have a list of my Unity Tags I now have a Param file per Controller.

File per Controller
Params as constants

I can change my code and be more comfortable that my params are now accurate and I will have compile time errors if a change occurs.

Compile time support

Plug-in design

When I wrote the Tag Processor it was a standalone script. However, now I have two processors I thought it was time to create a better design.

Tag Processor Container

Tag Processor Container

The Container lives in the Resources folder, it just makes it easy to load from code. Its role is to provide a list of Processors that are to be used. In the above example you can see that it contains 2 elements, Tag Processor and Animation Param Processor.

Processor Assets

The Processors

Post Processor

Each processor derives from PostProcessorBase. This contract means something can ask, “Can you handle this changing file”, and “Go ahead and process the changes”.

This means the previously written AssetModificationProcessor can iterate over the processors to see if anyone is interested in the changed files and call the appropriate one.

Using the plug-ins

Known Problems

The code currently does NOT cope with clashes of Controller Names that are in different namespaces. I am mulling over how best to handle that, suggestions welcome.

I am still not refreshing the Asset Database so for the first run the constants class file may not appear in the hierarchy until after a compilation has occurred.

The Code

If you would like to use the code, then please feel free and let me know how you get on. But it is just experimental so please ensure you have the correct back-ups of your code before using it.

cbcstudio/Editor/TagProcessors at main · paulio/cbcstudio (github.com)

--

--

Paul Marsh
Paul Marsh

Written by Paul Marsh

Unity, VR, Enterprise and .Net Developer

No responses yet