Skip to content

Commit b1af6d8

Browse files
committed
DynamicAudio: Added accessible AudioSource property
1 parent 067fcf4 commit b1af6d8

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

Source/MonoBehaviours/DynamicAudio.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,22 @@ public sealed class DynamicAudio : MonoBehaviour
4545
private AudioClip[] _audioClips;
4646
#pragma warning restore IDE0044 // Add readonly modifier
4747

48+
/// <value>
49+
/// The audio source property. If the field it is referencing is <see langword="null"/> then it adds a component.
50+
/// </value>
51+
public AudioSource AudioSource => _audioSource ??= gameObject.AddComponent<AudioSource>();
52+
4853
/// <summary>
49-
/// The audio source.
54+
/// The audio source field.
5055
/// </summary>
5156
[SerializeField]
5257
private AudioSource _audioSource;
5358

5459
/// <summary>
5560
/// Returns the <see cref="AudioSource"/>.
5661
/// </summary>
57-
/// <param name="dynamicAudio">The instance of <see cref="DynamicAudio"/> to retrieve <see cref="_audioSource"/> from.</param>
58-
public static explicit operator AudioSource(DynamicAudio dynamicAudio) => dynamicAudio._audioSource;
62+
/// <param name="dynamicAudio">The instance of <see cref="DynamicAudio"/> to retrieve <see cref="AudioSource"/> from.</param>
63+
public static explicit operator AudioSource(DynamicAudio dynamicAudio) => dynamicAudio.AudioSource;
5964

6065
private Routine<float, float> _fade;
6166

@@ -65,9 +70,7 @@ private void Awake()
6570
{
6671
_fade = this.ToRoutine((float from, float to) => TweenFade(from, to));
6772

68-
_audioSource = gameObject.AddComponent<AudioSource>();
69-
70-
_audioSource.playOnAwake = false;
73+
AudioSource.playOnAwake = false;
7174

7275
StartCoroutine(UpdateVolume());
7376
}
@@ -82,7 +85,7 @@ private void Awake()
8285
/// <summary>
8386
/// Pauses playing the clip.
8487
/// </summary>
85-
public void Pause() => _audioSource.Pause();
88+
public void Pause() => AudioSource.Pause();
8689

8790
/// <summary>
8891
/// Plays a sound, with optional parameters.
@@ -100,15 +103,15 @@ public void Play(AudioClip clip, bool loop = false, int priority = 0, float dela
100103
{
101104
clip.NullCheck("You cannot play an audio clip which is null.");
102105

103-
_audioSource.clip = clip;
104-
_audioSource.loop = loop;
105-
_audioSource.priority = priority;
106-
_audioSource.pitch = pitch;
107-
_audioSource.time = time;
106+
AudioSource.clip = clip;
107+
AudioSource.loop = loop;
108+
AudioSource.priority = priority;
109+
AudioSource.pitch = pitch;
110+
AudioSource.time = time;
108111

109112
_volume = volume;
110113

111-
_audioSource.PlayDelayed(delay);
114+
AudioSource.PlayDelayed(delay);
112115
}
113116

114117
/// <summary>
@@ -129,16 +132,16 @@ public void Play(AudioClip clip, bool loop = false, int priority = 0, float dela
129132
/// <summary>
130133
/// Stops playing the clip.
131134
/// </summary>
132-
public void Stop() => _audioSource.Stop();
135+
public void Stop() => AudioSource.Stop();
133136

134137
/// <summary>
135138
/// Unpauses the paused playback of this <see cref="AudioSource"/>.
136139
/// </summary>
137-
public void Unpause() => _audioSource.UnPause();
140+
public void Unpause() => AudioSource.UnPause();
138141

139142
private IEnumerator TweenFade(float to, float time)
140143
{
141-
float current = 0, from = _audioSource.volume;
144+
float current = 0, from = AudioSource.volume;
142145

143146
while (current < time)
144147
{
@@ -151,14 +154,14 @@ private IEnumerator TweenFade(float to, float time)
151154
yield return null;
152155
}
153156

154-
_audioSource.volume = to;
157+
AudioSource.volume = to;
155158
}
156159

157160
private IEnumerator UpdateVolume()
158161
{
159162
while (true)
160163
{
161-
_audioSource.volume = _volume * GameVolume / 100f;
164+
AudioSource.volume = _volume * GameVolume / 100f;
162165
yield return null;
163166
}
164167
}

0 commit comments

Comments
 (0)