Unity Timeline Tutorial: Playable C# API
In this video tutorial about the Unity 2017 Timeline Editor I show how to record a playable timeline asset and how to play it dynamically using a C# script.
Follow along the video to see how to record a playable using the timeline editor.
After the asset is created I add a trigger to a different GameObject and on entering this trigger with my player I use the Playable API of the PlayableDirector-class to play the recorded timeline asset.
Here is the code you can use in your own project:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Playables; public class TimelineStarter : MonoBehaviour { public GameObject Snake; private void OnTriggerEnter(Collider other) { PlayableDirector pd = Snake.GetComponent<PlayableDirector>(); if(pd != null) { pd.Play(); } } }
Add this code to a script that is attached to the GameObject with the trigger.
The PlayableDirector has a bool property called Play on Awake. Be sure to set this to false (disable checkbox) so that it is not played when the game is started.