Search Unity

Feedback Dear Unity, can we have something better than strings?

Discussion in 'Editor & General Support' started by John_Leorid, Sep 28, 2022.

  1. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    650
    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).
    Code (CSharp):
    1. public class UIToolkitReferences
    2. {
    3.     public static Key key1 = new Key(id: 56471231, name: "isGrounded");
    4.     public static Key key2 = new Key(id: 57454486, name: "moveSpeed");
    5. // ...
    6. }
    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:
    Code (CSharp):
    1. public class MyUIRefs
    2. {
    3.     public static Key isGrounded => UIToolkitReferences.key1;
    4. }
    5.  
    6. // and then use it like so
    7. 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.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,870
    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.
     
    John_Leorid likes this.