Created
June 29, 2018 13:59
-
-
Save Demkeys/ef60a0b471fa2b7026fc0bbd3f445bc7 to your computer and use it in GitHub Desktop.
Example code showing how to mute Tracks in a Timeline asset.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Instructions | |
/// - Create a Timeline asset. Add 3 tracks to it. | |
/// - Create empty GameObject in scene. Attach this script to the GameObject. Then attach a PlayableDirector component to it. | |
/// Drag and drop the Timeline asset you created into the Playable field of the PlayableDirector. | |
/// - Hit Play. The 1, 2 and 3 keys on your keyboard mute the first, second and third tracks respectively. | |
/// </summary> | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Timeline; | |
using UnityEngine.Playables; | |
public class MuteTimelineTracksScript : MonoBehaviour { | |
public PlayableDirector playableDirector; // Add reference to attached PlayableDirector component from Inspector | |
private TimelineAsset someTimelineAsset; | |
// Use this for initialization | |
void Start () { | |
someTimelineAsset = (TimelineAsset)playableDirector.playableAsset; | |
} | |
// Update is called once per frame | |
void Update () { | |
if(Input.GetKeyDown(KeyCode.Alpha1)) MuteUnmuteTrack(0); | |
else if(Input.GetKeyDown(KeyCode.Alpha2)) MuteUnmuteTrack(1); | |
else if(Input.GetKeyDown(KeyCode.Alpha3)) MuteUnmuteTrack(2); | |
} | |
void MuteUnmuteTrack(int trackIndex) | |
{ | |
// Get track from TimelineAsset | |
TrackAsset someTimelineTrackAsset = someTimelineAsset.GetOutputTrack(trackIndex); | |
// Change TimelineAsset's muted property value | |
someTimelineTrackAsset.muted = !someTimelineTrackAsset.muted; | |
double t = playableDirector.time; // Store elapsed time | |
playableDirector.RebuildGraph(); // Rebuild graph | |
playableDirector.time = t; // Restore elapsed time | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment