FANDOM


Audio played from Unity Timelines does not get paused when an AC game is paused. This component can be attached to an AudioSource to pause it manually.

To use it:

  1. Paste the script below into a new C# file named TimelineAudioPauser.cs
  2. Attach the new Timeline Audio Pauser component to an AudioSource that is controlled with Timeline
  3. In the component's Inspector, assign the Director field to the Playable Director that controls it

(Credit to cemleme on the AC forum)

TimelineAudioPauser.cs:

using UnityEngine;
using AC;
using UnityEngine.Playables;

[RequireComponent (AudioSource)]
public class TimelineAudioPauser : MonoBehaviour
{

public PlayableDirector director;

private AudioSource audioSource;
private AudioClip clip;
private float pausedTime;

private void OnEnable ()
{
audioSource = GetComponent<AudioSource>();
EventManager.OnEnterGameState += OnEnterGameState;
EventManager.OnExitGameState += OnExitGameState;
}

private void OnDisable ()
{
EventManager.OnEnterGameState -= OnEnterGameState;
EventManager.OnExitGameState -= OnExitGameState;
}

protected void OnEnterGameState (GameState gameState)
{
if (director != null && gameState == GameState.Paused && director.state == PlayState.Playing)
{
clip = audioSource.clip;
pausedTime = audioSource.time;
audioSource.volume = 0;
}
}

protected void OnExitGameState (GameState gameState)
{
if (director != null && gameState == GameState.Paused && director.state == PlayState.Paused)
{
audioSource.clip = clip;
audioSource.time = pausedTime;
audioSource.volume = 1;
audioSource.Play();
}
}

}
Community content is available under CC-BY-SA unless otherwise noted.