Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    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:
    81
    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.