Search Unity

Handling playables generically

Discussion in 'Animation' started by tree_fiddler_2000, Feb 6, 2019.

  1. tree_fiddler_2000


    Dec 5, 2018

    I have some code which deals with Playables, without really needing to care about what concrete type the playables they are (eg. I don't care if its a AnimationClipPlayable or AnimatorControllerPlayable), but I'm having a hard time making my code type agnostic.
    For example, if I want to connect a playable to a mixer, I need to have a value type representing the playable, which means I can't just keep an IPlayable reference around, since the Playable API requires structs:

    // won't compile because Connect() requires a struct
    IPlayable playable = animationClipPlayable;
    graph.Connect(playable , 0, mixerPlayable, 0);

    Is there a recommend way of working with playables so that I can pass them around generically, without caring what concrete type they are?
  2. Kybernetik


    Jan 3, 2013
    Have you tried using the "base" Playable struct instead of IPlayable? All playable structs can be cast to and from that struct.

    I ran into similar issues while making Animancer that I was able to solve by keeping a duplicate Playable in my base state class on top of the specific clip/controller/etc playable in their derived classes.