Strings

strings are an extremely versatile data type which can contain basically any amount and any type of data with a bit of conversion, but they're also:

  • Inefficient - Checking if two strings are equal requires comparing every individual character in both, which is slow and gets even slower as the length increases.
  • Unsafe - Raw strings aren't directly linked to anything, so if you make a typo or rename something you might not realise anything is wrong without spending time on extensive testing.

When Animancer needs to use strings, it uses StringReferences to improve the efficiency and StringAssets to improve the safety.

String References

A StringReference is a class containing a string. You can get one using the implicit conversion from string or by calling StringReference.Get.

public static readonly StringReference MyString = "MyString";
public static readonly StringReference MyString = StringReference.Get("MyString");

Internally, it keeps all the StringReferences in a dictionary keyed by their string. If no reference is registered for a particular string, a new one is created. Or if one already existed, then that same reference is returned. This means everything getting a reference for a particular string is given the exact same reference so checking if two are equal only requires a single reference equality check without going through the actual contents of the strings every time.

The Golf Events sample uses a StringReference in its script for the name of the Animancer Event it uses to hit the golf ball. That same name is used for a StringAsset assigned to the event in the Inspector.

String Assets

StringAsset is a ScriptableObject asset containing a StringReference which it initializes from the asset's name. You can create one using the Assets/Create/Animancer/String Asset menu function then reference it using a Serialized Field.

[SerializeField] private StringAsset _MyString;

That means the asset can be renamed to change the name without breaking either system and you can clearly see what each thing is using in the Inspector, which makes it more flexible and safer to use than hard coding a StringReference in the script.

StringAsset also has an implicit conversion to its StringReference so you can use an asset anywhere that accepts a reference.

Several samples use StringAssets for different things:

Sample Uses a StringAsset for
Linear Mixer The name of an Animancer Parameter which controls the blending of a Mixer State.
Footstep Events The name of Animancer Events.
Named Animations Aliases for animations in a Transition Library.