KybernetikGames / animancer

Documentation for the Animancer Unity Plugin.
66 stars 8 forks source link

ITransitions in a CustomClass array are not unique #291

Closed m4a4 closed 1 year ago

m4a4 commented 1 year ago

Environment

Description

Trying to use ITransition in a custom class that is stored in an array does not create unique references, and thus changing 1 variable on them changes them all.

Reproduction

Steps to reproduce the bug:

  1. Go to "04 Transitions" example scene.
  2. Add to the PlayTransitionOnClick class:
    [System.Serializable]
    public class Test1
    {
    [SerializeReference]
    [SerializeField] private ITransition _Test = new ClipTransition();
    }
    [SerializeField] private Test1[] _Test;
  3. Go to the editor on DefaultHumanoid and add an element to the Test array. Select ClipTransition, set an animation.
  4. Add a new element (notice that the element has already "copied" the previous).
  5. Change a value on either element and see both change.
KybernetikGames commented 1 year ago

That's not really a bug, it's just how Unity handles [SerializeReference] fields.

When you increase the size of an array in the Inspector, Unity clones the last item into all the new slots. For regular serialized fields that means they're all separate copies, but [SerializeReference] fields can actually have multiple fields referencing the same object, so you're getting multiple array items all pointing to the same underlying data.

You can avoid that by using the dropdown menu to pick the type of each new array item because that creates a new object so they won't be referencing the same thing anymore.

In my To Do list for the next major version I want to see if I can figure out a way to detect when multiple fields are referencing the same thing so I can indicate that in the Inspector somehow (and to give you a way to split them or merge them intentionally since it could potentially be useful).

Also, there's no point in having both [SerializeField] and [SerializeReference] on the same field.

m4a4 commented 1 year ago

Yeah, I noticed why it was happening (after posting the bug, I used the dropdown to make unique transitions), I just didn't see anything online about it (so I figured I'd add it as a bug somewhere).

And I was hoping that there was an easy fix so I wouldn't need to keep track of it. I noticed that an array of ITransition worked, and maybe that fix could've been ported over. But I guess not.

Ah, that's nice to know. I only did that for the Test1 to quickly get something reproducible for here.