Search Unity

How to create particle clip in Timeline at Runtime

Discussion in 'Timeline' started by Lafsody_UrniqueStudio, Aug 1, 2019.

  1. Lafsody_UrniqueStudio

    Lafsody_UrniqueStudio

    Joined:
    Mar 2, 2013
    Posts:
    11
    How to create particle clip in Timeline at Runtime

    We currently develop a game that can control time, and after develop many version. We see Timeline fits perfectly with our game.

    So, we use a method that we build Timeline and modify it at runtime.
    The position, rotation and animation clip tracks are works well as we expect.

    TimelineTrack.png

    But when come to the particle system we can't find the way how to create it.

    After some research. We found that when we drag the particle system to control track, it works as we want. So we need to imitate it in runtime. But still can't figure it out. How?

    This is our test code so far.
    Code (CSharp):
    1. void CreateTrack()
    2.     {
    3.         var track = timeline.CreateTrack<ControlTrack>(null, "RuntimeParticleTrack");
    4.  
    5.         // var particlePlayable = ParticleControlPlayable.Create(playableGraph, particle, 10001);
    6.  
    7.         var timelineClip = track.CreateClip<ControlPlayableAsset>();
    8.         Playable playable = (timelineClip.asset as ControlPlayableAsset).CreatePlayable(playableGraph, particle.gameObject);
    9.  
    10.         playableDirector.RebuildGraph();
    11.     }
    As we compare the particle that adds by drag and drops with particle we add by script. (Using PlayableGraph Visualizer)
    EditorVsRuntime.png We guess something don't bind correctly (Don't know what is Activation Control and Particle Control is), Source Game Object is still 'None', but we can't find doc or class to do this. Can't find a class like AnimationPlayableOutput either.

    Any help greatly appreciated!
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    The binding between the clip and the GameObject is done using a name lookup. The clip uses an ExposedReference, which is really just a string used to do a lookup in the PlayableDirector.

    Code (CSharp):
    1. var controlAsset = (ControlPlayableAsset) (controlTrack.CreateClip<ControlPlayableAsset>().asset);
    2. controlAsset.sourceGameObject.exposedName = "ParticleSystem";
    3. controlAsset.updateParticle = true;
    4.  
    5. playableDirector.SetReferenceValue("ParticleSystem", particleSystem.gameObject);
    Alternately you can directly set the prefabGameObject field of ControlPlayableAsset to reference a prefab, but that will cause the particle system to be instantiated when the Timeline starts to play.
     
  3. Lafsody_UrniqueStudio

    Lafsody_UrniqueStudio

    Joined:
    Mar 2, 2013
    Posts:
    11
    I already test on my test code and it works like a charm! I really appreciate it!
    I can now implement that to our game.

    I have another unrelated question. After I open Timeline editor of my playable director, my gameObject that I modify their position on timeline snap to position (0,0,0). I notice that in PlayableGraphVisualizer there is some node added to my graph e.g. AnimationOffset node.

    This is my graph.
    playableGraph.png
    And this is my graph after open Timeline editor.
    playableGraphWithMixer.png

    Is there any way to prevent the gameObject always snap to a position (0,0,0) after open Timeline editor?

    P.S. I don't know why I have graph duplicated equal to the amount of track. Did I do something wrong? Or is it normal? If I have 5 tracks I totally got 5 graphs that identical to each other.
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    If you set the AnimationTracks to use Scene Offsets, it will use the current scene position as a starting point. Or you can use Transform Offsets to set the exact location/rotation that you want it to snap to. Use the former if your tracks should play from their current spot during the game, and the latter if you want them to play at that exact position.

    As for the visualizer, that's a bug in the visualizer. Timeline has a unique structure to it's graph, all it's outputs point at the root, so the visualizer redraws the graph for each track, instead of each sub-graph. We'll try to get a fix for that.
     
  5. Lafsody_UrniqueStudio

    Lafsody_UrniqueStudio

    Joined:
    Mar 2, 2013
    Posts:
    11
    Thank you for the great explanation!

    After some test about offset more, I think my bug that snaps gameObject to the position (0, 0, 0) after open timeline editor is not caused directly by Offsets.
    In my TImeline I have
    Position Track that conducts transform.position in an infinite clip. (Top Track)
    Rotation Track that conducts transform.rotation in an infinite clip. (Second Track)
    I think my RotationTrack somehow override my transform.position (and set to (0, 0, 0) due to my RotationTrack doesn't set transform.position) because two tracks control the same transform.
    It's weird that it only happens after open Timeline Editor at runtime.

    After I rearrange track and made RotationTrack beyond PositionTrack and open Timeline Editor at runtime, the position of character doesn't snap to the position (0, 0, 0) anymore. And when I drag Timeline the position seems right but the rotation doesn't work anymore.
    In this case, my PositionTrack seems to override my RotationTrack.

    So, this behavior only prevents me to open Timeline Editor at runtime, that is not a big deal.
    But it is intrigued me that what is actually happening and is this can cause any more bug in my game in the future.
    Are two tracks control the same transform a bad practice?
    Should I merge PositionTrack and RotationTrack to one track?
     
  6. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Yes, unfortunately rotation and position on the root object are always intertwined. Editing one on a track, always causes the other one to be written.
     
  7. Lafsody_UrniqueStudio

    Lafsody_UrniqueStudio

    Joined:
    Mar 2, 2013
    Posts:
    11
    Ok, I will merge them to one track.
    Thanks for the fast response as always!