Search Unity

Create GameObjects and Animations from KeyValues at Runtime

Discussion in 'Animation' started by ST_ProductVIz, Jan 21, 2022.

  1. ST_ProductVIz

    ST_ProductVIz

    Joined:
    Nov 29, 2017
    Posts:
    27
    I am currently trying to create animation clips at runtime from previously parsed key values to change the material of a mesh renderer, animate the alpha value, and change the material again. I tried this with the legacy system because, according to the documentation, it is the only way to set animation curves at runtime.
    Unfortunately, the legacy system doesn't seem to be able to change a material in a clip (while the non-legacy system is able to, but this one just can't be accessed at runtime). Is there another way to create animation clips from key values at runtime that I may have overlooked?
    Any help would be appreciated!
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    With the legacy system you could make a script with an array of materials and have the animation set an int on the script indicating which material it wants then have the script apply it.

    Or if you use a system based on the Playables API you can use Animation Jobs to bind whatever you want at runtime. I've only used it for things like Procedural Leaning, but if non-legacy animations can set materials then jobs should be able to do it too.

    Then there's always the option of just having a script watch the current animation time and setting the material accordingly in LateUpdate.
     
  3. ST_ProductVIz

    ST_ProductVIz

    Joined:
    Nov 29, 2017
    Posts:
    27
    Thanks to your reply, I looked into the Playables API a bit more and decided to use Custom Playables to solve my problems. It took me a while to understand the overall concept (at least some parts of it). So, I have successfully created a series of playables and can play them, but now I get stuck when I try to play them in sequence (ex: the first playable starts at t = 0, the second at t = 3, etc). I've tried using TimelinePlayable, but I haven't found any examples or useful documentation, nor could I figure out how it works. Can anyone give me a minimal example of how to create a timeline-like behavior at runtime?
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Yeah, working with Playables and Timeline directly is a pain since the documentation is so bad and I can't suggest any better alternatives since advertising isn't allowed in this forum (except in signatures).

    Basic timeline-like behaviour can be achieved by simply checking the time of the current playable to determine when you need to start playing and fading in the next one. The SetLeadTime method might be useful for that (if it actually works), but I've never used it since I already had an event system to handle the sequencing.