// Copyright (c) Pixel Crushers. All rights reserved. using UnityEngine; using System; namespace PixelCrushers.QuestMachine { public enum QuestAudioSourceIdentifierType { /// /// Use the audio source on the current main camera. /// MainCamera, /// /// Use the audio source on the current Quest Machine Configuration instance. /// QuestMachine, /// /// Use the audio source on the GameObject with the specified tag. /// GameObjectWithTag, /// /// Use the audio source on the GameObject with the specified name. /// GameObjectWithName } /// /// Specifies which audio source to use to play audio. /// [Serializable] public class QuestAudioSourceIdentifier { [Tooltip("How to identify the audio source.")] [SerializeField] private QuestAudioSourceIdentifierType m_type = QuestAudioSourceIdentifierType.MainCamera; [Tooltip("Tag or GameObject name.")] [SerializeField] private string m_id = string.Empty; /// /// How to identify the audio source. /// public QuestAudioSourceIdentifierType type { get { return m_type; } set { m_type = value; } } /// /// Tag or GameObject name. /// public string id { get { return m_id; } set { m_id = value; } } /// /// Play a one shot audio clip through the specified audio source. /// /// public void Play(AudioClip audioClip) { var audioSource = FindAudioSource(); if (audioSource != null) audioSource.PlayOneShot(audioClip); } private AudioSource FindAudioSource() { var go = FindAudioSourceGameObject(); if (go == null) return null; var audioSource = go.GetComponent(); if (audioSource == null) { audioSource = go.AddComponent(); audioSource.playOnAwake = false; audioSource.loop = false; } return audioSource; } private GameObject FindAudioSourceGameObject() { switch (type) { default: case QuestAudioSourceIdentifierType.MainCamera: return (Camera.main != null) ? Camera.main.gameObject : null; case QuestAudioSourceIdentifierType.QuestMachine: return (QuestMachineConfiguration.instance != null) ? QuestMachineConfiguration.instance.gameObject : null; case QuestAudioSourceIdentifierType.GameObjectWithTag: return GameObject.FindGameObjectWithTag(id); case QuestAudioSourceIdentifierType.GameObjectWithName: return GameObject.Find(id); } } } }