Skip to content
Snippets Groups Projects
Commit 824cd40e authored by Marc Aurel Störmer's avatar Marc Aurel Störmer
Browse files

Add AudioLog script

- Play one or more AudioClips (in 3D space)
parent f4ebefa4
No related branches found
No related tags found
1 merge request!1Submission of the VR application
fileFormatVersion: 2
guid: 1d1346c64fb8a45469eeddfbd741b1cd
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections.Generic;
using UnityEngine;
namespace SwimmingInVR.Scripts
{
/// <summary>
/// The AudioLog class contains the functions to play one or more audio clips.
/// The audio clips together form an AudioLog,
/// which can be played back directly when the scene is started,
/// or via the function call StartAudioLog().
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class AudioLog : MonoBehaviour
{
/// <summary>
/// This is the AudioSource over which the audioClips are played back.
/// If none is set, the AudioLog class will create one.
/// </summary>
[SerializeField] private AudioSource audioSource;
/// <summary>
/// Contains all audio clips to be played in the correct order, which belong to this AudioLog.
/// </summary>
[SerializeField] private List<AudioClip> audioClips;
/// <summary>
/// If this variable is set, the method to play the audio log StartAudioLog() is called at the in the Start() method of
/// this AudioLog.
/// </summary>
[SerializeField] private bool bPlayOnStart;
/// <summary>
/// The AudioLog is played while this variable is true.
/// Set to true by the StartAudioLog() method and will be set back to false after playback.
/// </summary>
private bool _bIsPlaying;
/// <summary>
/// The index of the audio clip in the audioClips list that will be played next.
/// </summary>
private int _iNextAudioClipIndex;
private void Awake()
{
// We want to make sure that there is an AudioSource for this AudioLog.
if (!audioSource)
audioSource = gameObject.GetComponent<AudioSource>();
if (!audioSource)
audioSource = gameObject.AddComponent<AudioSource>();
}
// Start is called before the first frame update
private void Start()
{
if (audioClips.Count == 0)
{
Debug.LogWarning("<b>[AudioLog]</b> The AudioLog (" + gameObject.name +
") does not contain any audio clips. AudioLog will be disabled.");
enabled = false;
}
// AudioSource setup
audioSource.clip = audioClips[0];
audioSource.spatialBlend = 1; // 3D
// We use playOnAwake and enable the AudioSource as required because audioSource.Play(), which we previously used in Update(), did not play the AudioClips to the end.
// A bug? This is our workaround.
audioSource.enabled = false;
audioSource.playOnAwake = true;
if (bPlayOnStart)
StartAudioLog();
}
// Update is called once per frame
private void Update()
{
if (!_bIsPlaying) return;
audioSource.enabled = true;
if (audioSource.isPlaying) return;
audioSource.enabled = false;
_iNextAudioClipIndex++;
if (_iNextAudioClipIndex < audioClips.Count)
{
audioSource.clip = audioClips[_iNextAudioClipIndex];
}
else
{
audioSource.enabled = false;
_bIsPlaying = false;
_iNextAudioClipIndex =
0; // So that when we call StartAudioLog() again, we can start listening to the AudioLog from the beginning.
}
}
/// <summary>
/// This method is called to start the AudioLog. It has no effect while the AudioLog is already playing.
/// But it can be called again after playing to start the AudioLog from the beginning.
/// Current usage: Called by Start() if bPlayOnStart = true. Shall be called by the Scanner class when a player scans
/// an AudioLog.
/// </summary>
public void StartAudioLog()
{
_bIsPlaying = true;
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: d55c6fe2d4e1547dc88e25ac4162df38
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment