UPDATE: This script is now deprecated, since AC now supports the Addressables system natively.
Unity's addressable system provides a performant way of retrieving asset files at runtime, and this variant of the "Dialogue: Play speech" Action allows you to use them for speech audio.
To use it:
- In the Speech Manager, set the Reference speech files field to By Naming Convention.
- Create a folder and place in it a new C# script named ActionSpeechAddressable.cs.
- In the Speech Manager, define this folder as a custom Actions directory to install this as a new "Dialogue: Play speech (addressables)" Action, which you should use instead of the default.
- When lines are gathered, they will each have an expected filename. Add each associated audio file to your addressables package, and give it the same name.
ActionSpeechAddressable.cs:
using UnityEngine;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.AddressableAssets;
using System.Collections.Generic;
namespace AC
{
public class ActionSpeechAddressable : ActionSpeech
{
protected AudioClip addressableAudioClip;
protected bool isAwaitingAddressible = false;
public ActionSpeechAddressable ()
{
this.isDisplayed = true;
category = ActionCategory.Dialogue;
title = "Play speech (addressables)";
lineID = -1;
}
public override void AssignValues (List<ActionParameter> parameters)
{
addressableAudioClip = null;
isAwaitingAddressible = false;
base.AssignValues (parameters);
}
private void OnCompleteLoad (AsyncOperationHandle<AudioClip> obj)
{
isAwaitingAddressible = false;
StartSpeech (obj.Result);
}
public override float Run ()
{
if (!isRunning)
{
SpeechLine speechLine = KickStarter.speechManager.GetLine (lineID);
string filename = speechLine.GetFilename ();
Addressables.LoadAssetAsync<AudioClip> (filename).Completed += OnCompleteLoad;
isAwaitingAddressible = true;
isRunning = true;
return defaultPauseTime;
}
if (isAwaitingAddressible)
{
return defaultPauseTime;
}
if (isBackground)
{
isRunning = false;
return 0f;
}
return base.Run ();
}
}
}