-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironmentalSoundController.cs
More file actions
68 lines (60 loc) · 2.36 KB
/
Copy pathEnvironmentalSoundController.cs
File metadata and controls
68 lines (60 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class EnvironmentSoundSettings
{
public List<AudioClip> clips;
public float minInterval = 5.0f; // Minimum interval between plays
public float maxInterval = 15.0f; // Maximum interval between plays
public float volume = 1.0f;
}
public class EnvironmentalSoundController : MonoBehaviour
{
[Header("Environmental Sound Settings")]
[SerializeField] private AudioSource atmosphereAudioSource;
[SerializeField] private AudioSource windAudioSource;
[SerializeField] private AudioSource animalAudioSource;
[SerializeField] private EnvironmentSoundSettings atmosphereSettings;
[SerializeField] private EnvironmentSoundSettings windSettings;
[SerializeField] private EnvironmentSoundSettings animalSettings;
void Start()
{
// Start the playback for looping and interval-based sounds
if (atmosphereSettings.clips.Count > 0)
{
StartCoroutine(PlayLoopingSound(atmosphereAudioSource, atmosphereSettings, true));
}
if (windSettings.clips.Count > 0)
{
StartCoroutine(PlayIntervalSound(windAudioSource, windSettings));
}
if (animalSettings.clips.Count > 0)
{
StartCoroutine(PlayIntervalSound(animalAudioSource, animalSettings));
}
}
private IEnumerator PlayLoopingSound(AudioSource source, EnvironmentSoundSettings settings, bool loop = false)
{
source.clip = settings.clips[Random.Range(0, settings.clips.Count)];
source.volume = settings.volume;
source.loop = loop;
source.Play();
// Return null if the sound should loop indefinitely
if (loop) yield return null;
else yield return new WaitForSeconds(source.clip.length);
}
private IEnumerator PlayIntervalSound(AudioSource source, EnvironmentSoundSettings settings)
{
while (true)
{
yield return new WaitForSeconds(Random.Range(settings.minInterval, settings.maxInterval));
if (!source.isPlaying)
{
source.clip = settings.clips[Random.Range(0, settings.clips.Count)];
source.volume = settings.volume;
source.Play();
}
}
}
}