Using Signal Emitters with Timeline in Unity 2020
What is a Signal?
Per the first Dictionary result on Google for the definition of signal:
a gesture, action, or sound that is used to convey information or instructions, typically by prearrangement between the parties concerned.
“the firing of the gun was the signal for a chain of beacons to be lit”
The reason I’m going with writing essays 101, “start with a definition and be completely unoriginal”, is simple.
It really drives home the point here.
The word is a very apt description of what this feature does!
Signal Emitters in Timeline
We can use Signal Emitters to do a broad array of things in Unity Timeline.
We’re going to focus on changing variables, but that is pretty much just scratching the surface.
First, we need to create a Director in our Timeline window.
I’ve created a new empty game object and named it “SignalEmitter-Director”, clicked “Create” in the Timeline window, and now we have a Director in our project folder and a Playable Director component automatically created on the SignalEmitter-Director game object.
Next, for demonstration purposes, we add a new script which I’ve named “SignalTest1”.
In the new script, I’ve added a very simple Public method that will print out to the console letting us know when it is triggered.
Sending up signals won’t do any good if there is nobody to hear them!
We’ll add a Signal Receiver component to our SignalEmitter-Director game object.
We create a signal track as shown above in the Timeline window.
Now we can assign our game object with the Signal Receiver component, SignalEmitter-Director game object, to the Signal Emitter Track in the Timeline window.
We’ll tell the game to throw out a signal at about 3 seconds after play mode begins.
Note that there is a warning triangle on our Signal Emitter.
If we look at our Inspector window with the newly created Signal Emitter selected, we can see that it clearly states “Your project contains no Signal assets”.
So we click the “Create Signal” button and save our new Signal.
While we are here, let’s add a reaction to the Signal being called.
By selecting the SignalTest1 (script) component and the PublicDebugMethod() we created on the script, we are now able to hit play and validate that our signal did in fact work as expected.
Clicking Play does indeed show our signal is working.
This has been an extremely basic example, but now that you understand how to use the tool, you can find much cooler use cases for this especially if you want to tie into your own custom code.
Pause / Play Animations via Signal Emitters with Timeline
Let’s do something a little more interesting.
We’ll control an animation by pausing it and then resuming it using signal Emitters.
Let’s create a script, which I’ve named “PauseTimeline” and assign it to our CubeSphere-Director game object.
We’ll add a using statement, a director variable and a couple of simple pause / play methods to the script.
Add the PauseTimeline script to the CubeSphere-Director game object.
We also need to a Signal Receiver component to our CubeSphere-Director game object.
Next, we’ll add a signal track to our CubeSphere Timeline and position the Signal Emitter on the track.
The Emit Signal for our Signal Emitter is empty, so we’ll need to create two.
One for play and one for pause.
I’ve created both of the signals and assigned the PlaySignal to the second Signal Emitter on the Timeline track.
Last, on the CubeSphere-Director game object, Signal Receiver component, we need to assign our script’s methods as reactions to the pause and play signals being called as shown above.
Now, we should see the animation pause midway and resume right before it ends naturally when we hit play.
Because our Signal Track is on the same Timeline as the animations, we end up getting stuck at Pause.
We can go ahead and delete the Signal Emitter for play on our Timeline, leaving just the Pause Signal Emitter.
We’ll revise our script to search for the spacebar keydown event in the Update() method and use this to resume the animation.
Alright, it is working!
One side note, this was purely for demonstrations purposes of using custom code.
We could accomplish the exact same effect without our custom script as shown above.
Since our signal receiver is on the same game object as the PlayableDirector, you have access to use it as a reaction without writing any code.
This would work at least for pausing, I’m not sure how you’d go about checking for a keydown to play here though without some custom code.
Anyways, I just wanted to point out that if your Signal Receiver resides on the game object of what you want to control with Signal Emitters, you might just browse your options before writing custom code that could be unnecessary and a waste of time.