Download examples/open_world_city/Assets/Scripts/AudioManager.cs from ryzerrr/unity-game-agent: direct link, hf CLI and curl.
- Browser
- Download file 2.1 kB
-
https://huggingface.co/ryzerrr/unity-game-agent/resolve/main/examples/open_world_city/Assets/Scripts/AudioManager.cs
- Command line
-
hf download hf://ryzerrr/unity-game-agent/examples/open_world_city/Assets/Scripts/AudioManager.cs
-
curl -L -o AudioManager.cs https://huggingface.co/ryzerrr/unity-game-agent/resolve/main/examples/open_world_city/Assets/Scripts/AudioManager.cs
2.1 kB
| using UnityEngine; | |
| namespace UnityAgent | |
| { | |
| /// <summary> | |
| /// Singleton audio manager that exposes named one-shot SFX and music | |
| /// channels. Used by other systems to play clips without having to manage | |
| /// their own AudioSource components. | |
| /// </summary> | |
| public class AudioManager : MonoBehaviour | |
| { | |
| public static AudioManager Instance { get; private set; } | |
| [] | |
| public AudioSource sfxSource; | |
| public AudioSource musicSource; | |
| [] public float masterVolume = 1f; | |
| [] public float sfxVolume = 0.8f; | |
| [] public float musicVolume = 0.5f; | |
| private void Awake() | |
| { | |
| if (Instance != null && Instance != this) { Destroy(gameObject); return; } | |
| Instance = this; | |
| DontDestroyOnLoad(gameObject); | |
| if (sfxSource == null) | |
| { | |
| sfxSource = gameObject.AddComponent<AudioSource>(); | |
| sfxSource.playOnAwake = false; | |
| } | |
| if (musicSource == null) | |
| { | |
| musicSource = gameObject.AddComponent<AudioSource>(); | |
| musicSource.loop = true; | |
| musicSource.playOnAwake = false; | |
| } | |
| ApplyVolumes(); | |
| } | |
| public void PlaySfx(AudioClip clip, float volumeScale = 1f) | |
| { | |
| if (clip != null && sfxSource != null) | |
| sfxSource.PlayOneShot(clip, volumeScale * sfxVolume * masterVolume); | |
| } | |
| public void PlayMusic(AudioClip clip) | |
| { | |
| if (clip == null || musicSource == null) return; | |
| musicSource.clip = clip; | |
| musicSource.volume = musicVolume * masterVolume; | |
| musicSource.Play(); | |
| } | |
| public void StopMusic() | |
| { | |
| if (musicSource != null) musicSource.Stop(); | |
| } | |
| public void ApplyVolumes() | |
| { | |
| if (musicSource != null) musicSource.volume = musicVolume * masterVolume; | |
| AudioListener.volume = masterVolume; | |
| } | |
| } | |
| } | |