Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Can I animate a ScriptableObject field?

Discussion in 'Animation' started by Sinterklaas, May 25, 2022.

  1. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    I'm making a 2D game where my sprites are drawn with a custom shader. This shader uses several textures, such as a base color texture, an outline texture and a normal map. Because animating all of these textures as separate animation properties is messy, I wrote an AnimationFrame ScriptableObject to help keep things organized:

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3.  
    4. [Serializable]
    5. [CreateAssetMenu(fileName = "Animation Frame", menuName = "Animation Frame")]
    6. public class AnimationFrame : ScriptableObject
    7. {
    8.     public Sprite baseColor;
    9.     public Sprite outline;
    10.     public Texture2D normalMap;
    11.     // etc.
    12. }
    Each game object contains a SpriteRenderer, an Animator and a custom SpriteController component:
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3.  
    4. [RequireComponent(typeof(SpriteRenderer))]
    5. public class SpriteController : MonoBehaviour
    6. {
    7.     public AnimationFrame animationFrame;
    8.  
    9.     private SpriteRenderer spriteRenderer;
    10.  
    11.     private void Awake()
    12.     {
    13.         spriteRenderer = GetComponent<SpriteRenderer>();
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         // Update texture values of spriteRenderer's material...
    19.     }
    20. }
    Problem is, the "animationFrame" field is not showing up in the animation window. I read somewhere that ScriptableObjects cannot be serialized at all and that the Animator can only access serialized fields. Is this true and does this mean that what I'm trying to do is impossible in Unity?

    If so, what would be a good workaround? Because I really don't want to have to animate all of my texture maps as separete animation properties unless I absolutely have to.
     
  2. Alejandro-Martinez-Chacin

    Alejandro-Martinez-Chacin

    Joined:
    Oct 15, 2013
    Posts:
    146
    Late but curious how did you go about it?
    Supposing animations are to be shared between many controllers:
    Sounds like a list of the available AnimationFrame(s) somewhere (could be another ScriptableObject or directly on the SpriteController) and just animate another property that's just the index of the animation.

    Really just for organization purposes:
    In this case, just a serializable class or struct as the single animationFrame field.
    If you still want a ScriptableObject somewhere, the scriptable object could use the same struct/class.

    (I'm assuming [Serializable] class or struct does show properly on the animator, I would say yes 95% sure)