Dear Unity, can we have something better than strings?

So as far as I understand this, different dev-jobs such as Animation, UI Design and Coding are split in the editor, so that two developers can work at the same time without running into merge conflicts. To provide that, those systems are split by strings.
To access anything in the animator, we need strings, set in the animator.
Same for the UI Toolkit. We can’t assign any references (by drag & drop).

Can we have something that’s better for this purpose? Something that’s typesave and that artists and programmers can use at the same time without running into issues?

How about simple code generation, just an enum that the artist can set in the editor and the programmer can set in code, it get’s synced both ways (maybe with File.GetLastWriteTime).
Or maybe just a generated class, containing Keys (so that a rename won’t break code, which is the case with enums and strings).

public class UIToolkitReferences
{
    public static Key key1 = new Key(id: 56471231, name: "isGrounded");
    public static Key key2 = new Key(id: 57454486, name: "moveSpeed");
// ...
}

So the name can change but they key will stay the same, as the ID stays the same. As Programmer we could write our own wrapper which then just returns the correct key, like so:

public class MyUIRefs
{
    public static Key isGrounded => UIToolkitReferences.key1;
}

// and then use it like so
animator.SetBool(MyUIRefs.isGrounded, true);

Both files could get auto-generated when new variables are added but when their names change, the MyUIRefs File stays the same, so there are no compile errors.

This would solve a major problem that’s currently happening with strings (renaming a thing in the editor breaking code) and would also speed up the whole workflow because of autocomplete in VisualStudio - therefor not misspelling things or having to remember those strings / copy-pasting them one by one from the editor to code.

These are just some suggestions, maybe you have an even better idea but please give us something better than strings. Thanks for reading.

At the end of the day, all serialised references are strings, fyi. A UnityEngine.Object reference is just a guid saved as a string.

Better referencing in the UI builder is on the way for UI Toolkit. Mind you, you can still work without strings pretty easily with UI toolkit with smart use of style selectors.

1 Like