I understand that Unity doesn’t support animating variables in custom serialized classes, but basic fields in structs can be animated. However, I encountered an issue where classes like Sprite, Material, etc., inside a struct cannot be animated. For example:
[Serializable]
public struct TestStruct
{
[SerializeField]
private Sprite sprite; // Cannot be animated
[SerializeField]
private float number; // Can be animated
}
public class TestMonoBehaviour : MonoBehaviour
{
[SerializeField]
public TestStruct test;
[SerializeField]
private Sprite sprite; // Can be animated
[SerializeField]
private float number; // Can be animated
}
While simple fields like float can be animated, references to objects like Sprite in the struct seem not to be. Is there a way to expose these object references to the animator so they can be animated as well?
Resources like sprites are treated a bit different unfortunately. Don’t know the background exactly but e.g. Unity needs to be able to tell what sprite is used in the project and which isn’t so that it only packs those into the build.
One possible solution:
Map the sprites you wanna animate to an enum.
A serializable dictionary like this ( Serialized Dictionary | Utilities Tools | Unity Asset Store ) makes that easy in the editor.
Then animate a field of that enum type.
Finally have an update method somewhere that syncs the actual sprite field of whatever you want, with the corresponding sprite from the dictionary whenever the field changes.
Edit: I forgot right now whether you can animate properties. If yes, you can write the code that makes use of the dictionary to update the sprite, right into the properties set(), not requiring an update().