Search Unity

Animating serialized class properties with animation clip?

Discussion in 'Animation' started by FeastSC2, Sep 12, 2018.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I would like to animate with an animation clip the values of a serialized class within a class (using the animation window). How can I achieve this?

    The code below is an example of a class where the test float cannot be animated with an Animation clip.

    Code (CSharp):
    1. public class ParentClass : Monobehaviour
    2. {
    3.     public ChildClass myClass;
    4.     [Serializable]
    5.     public class ChildClass
    6.     {
    7.          public float test;
    8.     }
    9. }
     
  2. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Is there no way to make this work?
    I'd have to make a major rewrite in my code for a worse structure if this really is impossible.
     
  3. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    it won't work with aggregated class.

    But if you change class ChildClass from class to struct it should work, we do support animation on aggregated struct but not on class. There is a technical detail behind all this but I do not know exactly the reason.
     
    fuser likes this.
  4. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Alright I'll find another way then.
    This is a bit of a related question but how can I detect through script when an animation clip is targetting an object with a one of my scripts?
    Basically I want to know if a shader script is being animated with an animation clip and if it is, I want update the material, I only want to do this if I know it's being animated because if not, it's wasted resources.
     
  5. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    FeastSC2 likes this.
  6. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I did what you suggested, it's good but it's still not as precise as I would want it.
    Maybe there's a way to detect if a script has had its exposed properties modified, in which case it would update? How would I know that, must I necessarily keep a copied version of the object to have something to compare it to?
     
  7. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    you could try to use MonoBehaviour.OnDidApplyAnimationProperties().

    This method is undocumented but should be called every time the animation system write into your monobehaviour.
    Try to keep the workload of this function to the lowest possible as it can become quickly the bottleneck.
     
    richardkettlewell and FeastSC2 like this.
  8. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    519
    Hi there, is there any way to do this now? More easily than having to create a controller script on top ?