Master Audio 2022 integration

Master Audio 2022 is an audio-playback asset available on the Asset Store here.

The following scripts can be downloaded as a .unitypackage file here.

This page provides the following scripts:

* ActionSoundMA

A custom "Sound: Play (MasterAudio)" Action that can be used to play sounds. To install, see this tutorial.

* ActionPlaylist

A custom "Sound: Playlist" Action that can control the state of a playlist. To install, see this tutorial.

* BusVolumeSyncer

A script that syncs the volumes of buses with volumes set in AC's Options menu. To use, attach to a GameObject in the scene and fill in its Inspector.

* LanguageSyncer

A script that syncs Master Audio's language with the language set in AC's Options menu. To use, attach to a GameObject in the scene and fill in its Inspector.

ActionSoundMA.cs:

```
using UnityEngine;
using System.Collections.Generic;
#if UNITY\_EDITOR
using UnityEditor;
#endif
using DarkTonic.MasterAudio;

namespace AC
{

 [System.Serializable]
 public class ActionSoundMA : Action
 {

 [SoundGroupAttribute] public string soundGroup;
 public int soundGroupParameterID = -1;

 public Transform sourceTransform;
 public int sourceTransformParameterID = -1;
 public int sourceTransformConstantID = 0;
 public bool followTransform;
 private Transform runtimeSourceTransform;

 public float volumePercentage = 1f;
 public float pitch = 1f;
 public float delaySoundTime = 0f;

 public override ActionCategory Category { get { return ActionCategory.Sound; } }
 public override string Title { get { return "Play (MasterAudio)"; } }

 public override void AssignValues (List<ActionParameter> parameters)
 {
 soundGroup = AssignString (parameters, soundGroupParameterID, soundGroup);
 runtimeSourceTransform = AssignFile (parameters, sourceTransformParameterID, sourceTransformConstantID, runtimeSourceTransform);
 }

 public override float Run ()
 {
 if (!isRunning)
 {
 PlaySoundResult result = null;

 if (runtimeSourceTransform)
 {
 if (followTransform)
 {
 if (willWait)
 {
 result = MasterAudio.PlaySound3DFollowTransform (soundGroup, runtimeSourceTransform, volumePercentage, pitch, delaySoundTime);
 }
 else
 {
 MasterAudio.PlaySound3DFollowTransformAndForget (soundGroup, runtimeSourceTransform, volumePercentage, pitch, delaySoundTime);
 }
 }
 else
 {
 if (willWait)
 {
 result = MasterAudio.PlaySound3DAtTransform (soundGroup, runtimeSourceTransform, volumePercentage, pitch, delaySoundTime);
 }
 else
 {
 MasterAudio.PlaySound3DAtTransformAndForget (soundGroup, runtimeSourceTransform, volumePercentage, pitch, delaySoundTime);
 }
 }
 }
 else
 {
 if (willWait)
 {
 result = MasterAudio.PlaySound (soundGroup, volumePercentage, pitch, delaySoundTime);
 }
 else
 {
 MasterAudio.PlaySoundAndForget (soundGroup, volumePercentage, pitch, delaySoundTime);
 }
 }

 if (result != null && willWait)
 {
 if ((delaySoundTime > 0f && result.SoundScheduled) || (delaySoundTime <= 0f && result.SoundPlayed))
 {
 isRunning = true;
 result.ActingVariation.SoundFinished += OnSoundFinished;
 }
 }
 }

 return isRunning ? defaultPauseTime : 0f;

 }

 private void OnSoundFinished ()
 {
 isRunning = false;
 }

 public override void Skip () { }

#if UNITY\_EDITOR

 public override void ShowGUI (List<ActionParameter> parameters)
 {
 // Sound group
 soundGroupParameterID = Action.ChooseParameterGUI ("New label:", parameters, soundGroupParameterID, new ParameterType[2] { ParameterType.String, ParameterType.PopUp });
 if (soundGroupParameterID < 0)
 {
 var serializedObject = new SerializedObject (this);
 serializedObject.Update ();
 SerializedProperty eventProperty = serializedObject.FindProperty (nameof (soundGroup));
 EditorGUILayout.PropertyField (eventProperty, true);
 serializedObject.ApplyModifiedProperties ();
 }

 volumePercentage = EditorGUILayout.Slider ("Volume percentage:", volumePercentage, 0f, 1f);
 pitch = EditorGUILayout.Slider ("Pitch", pitch, 0f, 2f);
 delaySoundTime = EditorGUILayout.FloatField ("Delay sound time:", delaySoundTime);

 sourceTransformParameterID = Action.ChooseParameterGUI ("Source:", parameters, sourceTransformParameterID, ParameterType.GameObject);
 if (sourceTransformParameterID >= 0)
 {
 sourceTransformConstantID = 0;
 sourceTransform = null;
 }
 else
 {
 sourceTransform = (Transform) EditorGUILayout.ObjectField ("Source:", sourceTransform, typeof (Transform), true);

 sourceTransformConstantID = FieldToID (sourceTransform, sourceTransformConstantID);
 sourceTransform = IDToField (sourceTransform, sourceTransformConstantID, false);
 }

 {
 followTransform = EditorGUILayout.Toggle ("Follow source?", followTransform);
 }

 willWait = EditorGUILayout.Toggle ("Wait until finish?", willWait);
 }

#endif

 }

}

```

ActionPlaylist.cs:

```
using UnityEngine;
using System.Collections.Generic;
#if UNITY\_EDITOR
using UnityEditor;
#endif
using DarkTonic.MasterAudio;

namespace AC
{

 [System.Serializable]
 public class ActionPlaylist : Action
 {

 public string clipName;
 public int clipNameParameterID = -1;

 [PlaylistAttribute] public string playlistName;
 public int playlistNameParameterID = -1;

 public bool playFirstClip = true;

 private enum Method { TriggerPlaylistClip, ChangePlaylist, StartPlaylist, StopPlaylist, QueuePlaylist, GrabPlaylist };
 [SerializeField] private Method method = Method.TriggerPlaylistClip;

 public override ActionCategory Category { get { return ActionCategory.Sound; } }
 public override string Title { get { return "Playlist"; } }

 public override void AssignValues (List<ActionParameter> parameters)
 {
 clipName = AssignString (parameters, clipNameParameterID, clipName);
 playlistName = AssignString (parameters, playlistNameParameterID, playlistName);
 }

 public override float Run ()
 {
 switch (method)
 {
 case Method.TriggerPlaylistClip:
 MasterAudio.TriggerPlaylistClip (clipName);
 break;

 case Method.QueuePlaylist:
 MasterAudio.QueuePlaylistClip (clipName);
 break;

 case Method.ChangePlaylist:
 MasterAudio.ChangePlaylistByName (playlistName, playFirstClip);
 break;

 case Method.StartPlaylist:
 MasterAudio.StartPlaylist (playlistName);
 break;

 case Method.GrabPlaylist:
 MasterAudio.GrabPlaylist (playlistName);
 break;

 default:
 break;
 }

 return 0f;
 }

#if UNITY\_EDITOR

 public override void ShowGUI (List<ActionParameter> parameters)
 {
 method = (Method) EditorGUILayout.EnumPopup ("Method:", method);
 switch (method)
 {
 ShowClipGUI (parameters);
 break;

 case Method.ChangePlaylist:
 ShowPlaylistGUI (parameters);
 playFirstClip = EditorGUILayout.Toggle ("Play first clip?", playFirstClip);
 break;

 ShowPlaylistGUI (parameters);
 break;

 default:
 break;
 }
 }

 private void ShowClipGUI (List<ActionParameter> parameters)
 {
 clipNameParameterID = Action.ChooseParameterGUI ("Clip name:", parameters, clipNameParameterID, ParameterType.String);
 if (clipNameParameterID < 0)
 {
 EditorGUILayout.TextField ("Clip name:", clipName);
 }
 }

 private void ShowPlaylistGUI (List<ActionParameter> parameters)
 {
 playlistNameParameterID = Action.ChooseParameterGUI ("Playlist:", parameters, playlistNameParameterID, ParameterType.String);
 if (playlistNameParameterID < 0)
 {
 var serializedObject = new SerializedObject (this);
 serializedObject.Update ();
 SerializedProperty eventProperty = serializedObject.FindProperty (nameof (playlistName));
 EditorGUILayout.PropertyField (eventProperty, true);
 serializedObject.ApplyModifiedProperties ();
 }
 }

#endif

 }

}

```

BusVolumeSyncer.cs:

```
using UnityEngine;
using DarkTonic.MasterAudio;

namespace AC
{

 public class BusVolumeSyncer : MonoBehaviour
 {

 [SerializeField] private BusVolume[] buses = new BusVolume[0];

 private void OnEnable ()
 {
 EventManager.OnInitialiseScene += OnInitialiseScene;
 EventManager.OnChangeVolume += OnChangeVolume;
 }

 private void OnDisable ()
 {
 EventManager.OnInitialiseScene -= OnInitialiseScene;
 EventManager.OnChangeVolume -= OnChangeVolume;
 }

 private void OnChangeVolume (SoundType soundType, float volume)
 {
 SyncVolumes ();
 }

 private void OnInitialiseScene ()
 {
 SyncVolumes ();
 }

 private void SyncVolumes ()
 {
 float sfxVolume = Options.GetSFXVolume ();
 float musicVolume = Options.GetMusicVolume ();
 float speechVolume = Options.GetMusicVolume ();

 foreach (BusVolume busVolume in buses)
 {
 if (string.IsNullOrEmpty (busVolume.busName)) continue;

 switch (busVolume.soundType)
 {
 case SoundType.Music:
 busVolume.SetVolume (musicVolume);
 break;

 case SoundType.SFX:
 busVolume.SetVolume (sfxVolume);
 break;

 case SoundType.Speech:
 busVolume.SetVolume (speechVolume);
 break;

 default:
 break;
 }
 }
 }

 [System.Serializable]
 private class BusVolume
 {

 public string busName;
 public SoundType soundType;
 public float relativeVolume;

 public void SetVolume (float volume)
 {
 MasterAudio.SetBusVolumeByName (busName, volume \* relativeVolume);
 }

 }

 }

}

```

LanguageSyncer:

```
using UnityEngine;
using DarkTonic.MasterAudio;

namespace AC
{

 public class LanguageSyncer : MonoBehaviour
 {

 [SerializeField] private SystemLanguage[] languages = new SystemLanguage[0];

 private void OnEnable () { EventManager.OnChangeVoiceLanguage += OnChangeVoiceLanguage; }
 private void OnDisable () { EventManager.OnChangeVoiceLanguage -= OnChangeVoiceLanguage; }

 private void OnChangeVoiceLanguage (int index)
 {
 if (index < languages.Length)
 {
 MasterAudio.DynamicLanguage = languages[index];
 }
 }

 }

}

```