This repository has been archived on 2024-07-09. You can view files and clone it, but cannot push or open issues or pull requests.
everellia/SheKnowsWhatYouAreToHerGame/Assets/Scripts/AudioManager.cs

139 lines
4 KiB
C#
Raw Normal View History

2024-07-03 18:36:39 +00:00
/*
* author: mark joshwel
* date: 29/5/2024
* description: audio manager for handling audio in the game
*/
using System;
using UnityEngine;
/// <summary>
2024-07-03 20:37:15 +00:00
/// singleton class for handling audio in the game
2024-07-03 18:36:39 +00:00
/// </summary>
public class AudioManager : MonoBehaviour
{
/// <summary>
2024-07-03 20:37:15 +00:00
/// singleton pattern: define instance field for accessing the singleton elsewhere
2024-07-03 18:36:39 +00:00
/// </summary>
public static AudioManager Instance;
2024-07-03 20:50:59 +00:00
/// <summary>
/// music audio source
/// </summary>
[SerializeField] private AudioSource musicSource;
2024-07-03 18:36:39 +00:00
2024-07-03 20:50:59 +00:00
/// <summary>
/// music source default volume
/// </summary>
2024-07-03 18:36:39 +00:00
[SerializeField] private float musicSourceDefaultVolume = 0.6f;
2024-07-03 20:50:59 +00:00
/// <summary>
/// sound effects (sfx) audio source
/// </summary>
2024-07-03 18:36:39 +00:00
[SerializeField] private AudioSource sfxSource;
2024-07-03 20:50:59 +00:00
/// <summary>
/// sound effects (sfx) source default volume
/// </summary>
2024-07-03 18:36:39 +00:00
[SerializeField] private float sfxSourceDefaultVolume = 0.6f;
2024-07-03 20:50:59 +00:00
/// <summary>
/// audio clip for menu button click
/// </summary>
[SerializeField] public AudioClip menuButtonClick;
2024-07-03 18:36:39 +00:00
2024-07-03 20:50:59 +00:00
/// <summary>
/// audio clip for menu button hover
/// </summary>
[SerializeField] public AudioClip menuButtonHover;
2024-07-03 18:36:39 +00:00
/// <summary>
/// function to set doesn't destroy on load and checks for multiple instances
2024-07-03 20:37:15 +00:00
/// </summary>
private void Awake()
{
// check if instance hasn't been set yet
if (Instance == null)
{
// set this instance as the singleton instance
Instance = this;
// don't destroy this instance on a scene load
2024-07-03 20:37:15 +00:00
DontDestroyOnLoad(gameObject);
Debug.Log("AudioManager: Awake as singleton instance");
}
// check if instance is already set and it's not this instance
else if (Instance != null && Instance != this)
{
Debug.Log("AudioManager: Awake as non-singleton instance, destroying self");
// destroy the new instance if it's not the singleton instance
Destroy(gameObject);
}
}
/// <summary>
/// function to set default volumes for the audio sources
2024-07-03 18:36:39 +00:00
/// </summary>
public void Start()
{
// set the default volume for the music source
musicSource.volume = musicSourceDefaultVolume;
// set the default volume for the sfx source
sfxSource.volume = sfxSourceDefaultVolume;
}
/// <summary>
2024-07-03 20:37:15 +00:00
/// plays the audio clip once on the music source/channel
2024-07-03 18:36:39 +00:00
/// </summary>
/// <param name="clip">audio clip to play</param>
public void PlayOnMusicChannel(AudioClip clip)
{
musicSource.PlayOneShot(clip);
}
/// <summary>
2024-07-03 20:37:15 +00:00
/// plays the audio clip once on the sound effects (sfx) source/channel
2024-07-03 18:36:39 +00:00
/// </summary>
/// <param name="clip">audio clip to play</param>
public void PlayOnSFXChannel(AudioClip clip)
{
sfxSource.PlayOneShot(clip);
}
/// <summary>
2024-07-03 20:37:15 +00:00
/// function to get the current volume of the music source/channel
2024-07-03 18:36:39 +00:00
/// </summary>
/// <returns>volume as float from 0.0 to 1.0</returns>
public float GetMusicVolume()
{
return musicSource.volume;
}
/// <summary>
2024-07-03 20:37:15 +00:00
/// sets the volume of the music source/channel
2024-07-03 18:36:39 +00:00
/// </summary>
/// <param name="volume">float (0.0-1.0) to set the channel volume to</param>
public void SetMusicVolume(float volume)
{
musicSource.volume = Math.Min(volume, 1.0f);
}
/// <summary>
2024-07-03 20:37:15 +00:00
/// function to get the current volume of the sound effects (sfx) source/channel
2024-07-03 18:36:39 +00:00
/// </summary>
/// <returns>volume as float from 0.0 to 1.0</returns>
public float GetSfxVolume()
{
return sfxSource.volume;
}
/// <summary>
2024-07-03 20:37:15 +00:00
/// sets the volume of the sound effects (sfx) source/channel
2024-07-03 18:36:39 +00:00
/// </summary>
/// <param name="volume">float (0.0-1.0) to set the channel volume to</param>
public void SetSfxVolume(float volume)
{
sfxSource.volume = Math.Min(volume, 1.0f);
}
}