Documentation for the Unity C# Library
Loading...
Searching...
No Matches
Audio.cs
Go to the documentation of this file.
1using System;
2using UnityEngine;
3
5{
9 public class Audio
10 {
11 public string Name { get; private set; }
12
16 public int AudioID { get; private set; }
17
21 public AudioType Type { get; private set; }
22
26 public bool IsPlaying { get; private set; }
27
31 public bool Paused { get; private set; }
32
36 public bool Stopping { get; private set; }
37
41 public bool Activated { get; private set; }
42
46 public bool Pooled { get; set; }
47
51 public float Volume { get; private set; }
52
56 public AudioSource AudioSource { get; private set; }
57
61 public Transform SourceTransform
62 {
63 get { return sourceTransform; }
64 set
65 {
66 if (value == null)
67 {
69 }
70 else
71 {
72 sourceTransform = value;
73 }
74 }
75 }
76
80 public AudioClip Clip
81 {
82 get { return clip; }
83 set
84 {
85 clip = value;
86 if (AudioSource != null)
87 {
88 AudioSource.clip = clip;
89 }
90 }
91 }
92
96 public bool Loop
97 {
98 get { return loop; }
99 set
100 {
101 loop = value;
102 if (AudioSource != null)
103 {
104 AudioSource.loop = loop;
105 }
106 }
107 }
108
112 public bool Mute
113 {
114 get { return mute; }
115 set
116 {
117 mute = value;
118 if (AudioSource != null)
119 {
120 AudioSource.mute = mute;
121 }
122 }
123 }
124
128 public int Priority
129 {
130 get { return priority; }
131 set
132 {
133 priority = Mathf.Clamp(value, 0, 256);
134 if (AudioSource != null)
135 {
136 AudioSource.priority = priority;
137 }
138 }
139 }
140
144 public float Pitch
145 {
146 get { return pitch; }
147 set
148 {
149 pitch = Mathf.Clamp(value, -3, 3);
150 if (AudioSource != null)
151 {
152 AudioSource.pitch = pitch;
153 }
154 }
155 }
156
160 public float StereoPan
161 {
162 get { return stereoPan; }
163 set
164 {
165 stereoPan = Mathf.Clamp(value, -1, 1);
166 if (AudioSource != null)
167 {
168 AudioSource.panStereo = stereoPan;
169 }
170 }
171 }
172
176 public float SpatialBlend
177 {
178 get { return spatialBlend; }
179 set
180 {
181 spatialBlend = Mathf.Clamp01(value);
182 if (AudioSource != null)
183 {
184 AudioSource.spatialBlend = spatialBlend;
185 }
186 }
187 }
188
192 public float ReverbZoneMix
193 {
194 get { return reverbZoneMix; }
195 set
196 {
197 reverbZoneMix = Mathf.Clamp(value, 0, 1.1f);
198 if (AudioSource != null)
199 {
200 AudioSource.reverbZoneMix = reverbZoneMix;
201 }
202 }
203 }
204
208 public float DopplerLevel
209 {
210 get { return dopplerLevel; }
211 set
212 {
213 dopplerLevel = Mathf.Clamp(value, 0, 5);
214 if (AudioSource != null)
215 {
216 AudioSource.dopplerLevel = dopplerLevel;
217 }
218 }
219 }
220
224 public float Spread
225 {
226 get { return spread; }
227 set
228 {
229 spread = Mathf.Clamp(value, 0, 360);
230 if (AudioSource != null)
231 {
232 AudioSource.spread = spread;
233 }
234 }
235 }
236
240 public AudioRolloffMode RolloffMode
241 {
242 get { return rolloffMode; }
243 set
244 {
245 rolloffMode = value;
246 if (AudioSource != null)
247 {
248 AudioSource.rolloffMode = rolloffMode;
249 }
250 }
251 }
252
256 public float Max3DDistance
257 {
258 get { return max3DDistance; }
259 set
260 {
261 max3DDistance = Mathf.Max(value, 0.01f);
262 if (AudioSource != null)
263 {
264 AudioSource.maxDistance = max3DDistance;
265 }
266 }
267 }
268
272 public float Min3DDistance
273 {
274 get { return min3DDistance; }
275 set
276 {
277 min3DDistance = Mathf.Max(value, 0);
278 if (AudioSource != null)
279 {
280 AudioSource.minDistance = min3DDistance;
281 }
282 }
283 }
284
288 public bool Persist { get; set; }
289
293 public float FadeInSeconds { get; set; }
294
298 public float FadeOutSeconds { get; set; }
299
300
304 public enum AudioType
305 {
306 Music,
307 Sound,
308 UISound
309 }
310
311 private static int audioCounter = 0;
312
313 private AudioClip clip;
314 private bool loop;
315 private bool mute;
316 private int priority;
317 private float pitch;
318 private float stereoPan;
319 private float spatialBlend;
320 private float reverbZoneMix;
321 private float dopplerLevel;
322 private float spread;
323 private AudioRolloffMode rolloffMode;
324 private float max3DDistance;
325 private float min3DDistance;
326
327 private float targetVolume;
328 private float initTargetVolume;
329 private float tempFadeSeconds;
330 private float fadeInterpolater;
331 private float onFadeStartVolume;
332 private Transform sourceTransform;
333
334 public Audio(string name, AudioType audioType, AudioClip clip, bool loop, bool persist, float volume,
335 float fadeInValue,
336 float fadeOutValue, Transform sourceTransform)
337 {
338 Name = name;
339
340 // Set unique audio ID
342 audioCounter++;
343
344 // Initialize values
345 this.Type = audioType;
346 this.Clip = clip;
347 this.SourceTransform = sourceTransform;
348 this.Loop = loop;
349 this.Persist = persist;
350 this.targetVolume = volume;
351 this.initTargetVolume = volume;
352 this.tempFadeSeconds = -1;
353 this.FadeInSeconds = fadeInValue;
354 this.FadeOutSeconds = fadeOutValue;
355
356 Volume = 0f;
357 Pooled = false;
358
359 // Set audiosource default values
360 Mute = false;
361 Priority = 128;
362 Pitch = 1;
363 StereoPan = 0;
365 {
366 SpatialBlend = 1;
367 }
368
369 ReverbZoneMix = 1;
370 DopplerLevel = 1;
371 Spread = 0;
372 RolloffMode = AudioRolloffMode.Logarithmic;
373 Min3DDistance = 1;
374 Max3DDistance = 500;
375
376 // Initliaze states
377 IsPlaying = false;
378 Paused = false;
379 Activated = false;
380 }
381
385 private void CreateAudiosource()
386 {
387 AudioSource = SourceTransform.gameObject.AddComponent<AudioSource>() as AudioSource;
388 AudioSource.clip = Clip;
389 AudioSource.loop = Loop;
390 AudioSource.mute = Mute;
391 AudioSource.volume = Volume;
392 AudioSource.priority = Priority;
393 AudioSource.pitch = Pitch;
394 AudioSource.panStereo = StereoPan;
395 AudioSource.spatialBlend = SpatialBlend;
396 AudioSource.reverbZoneMix = ReverbZoneMix;
397 AudioSource.dopplerLevel = DopplerLevel;
398 AudioSource.spread = Spread;
399 AudioSource.rolloffMode = RolloffMode;
400 AudioSource.maxDistance = Max3DDistance;
401 AudioSource.minDistance = Min3DDistance;
402 }
403
407 public void Play()
408 {
410 }
411
416 public void Play(float volume)
417 {
418 // Check if audio still exists in sound manager
419 if (Pooled)
420 {
421 // If not, restore it from the audioPool
422 bool restoredFromPool = EazySoundManager.RestoreAudioFromPool(Type, AudioID);
423 if (!restoredFromPool)
424 {
425 return;
426 }
427
428 Pooled = true;
429 }
430
431 // Recreate audiosource if it does not exist
432 if (AudioSource == null)
433 {
435 }
436
437 AudioSource.Play();
438 IsPlaying = true;
439
440 fadeInterpolater = 0f;
442 targetVolume = volume;
443 }
444
448 public void Stop()
449 {
450 fadeInterpolater = 0f;
452 targetVolume = 0f;
453
454 Stopping = true;
455 }
456
460 public void Pause()
461 {
462 AudioSource.Pause();
463 Paused = true;
464 }
465
469 public void UnPause()
470 {
471 AudioSource.UnPause();
472 Paused = false;
473 }
474
478 public void Resume()
479 {
480 AudioSource.UnPause();
481 Paused = false;
482 }
483
488 public void SetVolume(float volume)
489 {
490 if (volume > targetVolume)
491 {
492 SetVolume(volume, FadeOutSeconds);
493 }
494 else
495 {
496 SetVolume(volume, FadeInSeconds);
497 }
498 }
499
505 public void SetVolume(float volume, float fadeSeconds)
506 {
507 SetVolume(volume, fadeSeconds, this.Volume);
508 }
509
516 public void SetVolume(float volume, float fadeSeconds, float startVolume)
517 {
518 targetVolume = Mathf.Clamp01(volume);
520 onFadeStartVolume = startVolume;
521 tempFadeSeconds = fadeSeconds;
522 }
523
529 public void Set3DDistances(float min, float max)
530 {
531 Min3DDistance = min;
532 Max3DDistance = max;
533 }
534
538 public void Update()
539 {
540 if (AudioSource == null)
541 {
542 return;
543 }
544
545 Activated = true;
546
547 // Increase/decrease volume to reach the current target
548 if (Volume != targetVolume)
549 {
550 float fadeValue;
551 fadeInterpolater += Time.unscaledDeltaTime;
552 if (Volume > targetVolume)
553 {
554 fadeValue = tempFadeSeconds != -1 ? tempFadeSeconds : FadeOutSeconds;
555 }
556 else
557 {
558 fadeValue = tempFadeSeconds != -1 ? tempFadeSeconds : FadeInSeconds;
559 }
560
561 Volume = Mathf.Lerp(onFadeStartVolume, targetVolume, fadeInterpolater / fadeValue);
562 }
563 else if (tempFadeSeconds != -1)
564 {
565 tempFadeSeconds = -1;
566 }
567
568 // Set the volume, taking into account the global volumes as well.
569 switch (Type)
570 {
571 case AudioType.Music:
572 {
573 AudioSource.volume = Volume * EazySoundManager.GlobalMusicVolume * EazySoundManager.GlobalVolume;
574 break;
575 }
576 case AudioType.Sound:
577 {
578 AudioSource.volume = Volume * EazySoundManager.GlobalSoundsVolume * EazySoundManager.GlobalVolume;
579 break;
580 }
581 case AudioType.UISound:
582 {
583 AudioSource.volume = Volume * EazySoundManager.GlobalUISoundsVolume * EazySoundManager.GlobalVolume;
584 break;
585 }
586 }
587
588 // Completely stop audio if it finished the process of stopping
589 if (Volume == 0f && Stopping)
590 {
591 // AudioStopped?.Invoke(this);
592 AudioSource.Stop();
593 Stopping = false;
594 IsPlaying = false;
595 Paused = false;
596 }
597
598 // Update playing status
599 if (AudioSource.isPlaying != IsPlaying && (Application.isFocused || Application.isEditor))
600 {
601 IsPlaying = AudioSource.isPlaying;
602 }
603 }
604 }
605}
The audio object.
Definition Audio.cs:10
void SetVolume(float volume, float fadeSeconds, float startVolume)
Sets the audio volume.
Definition Audio.cs:516
AudioRolloffMode RolloffMode
How the audio attenuates over distance.
Definition Audio.cs:241
bool Mute
Whether the audio is muted.
Definition Audio.cs:113
float FadeInSeconds
How many seconds it needs for the audio to fade in/ reach target volume (if higher than current)
Definition Audio.cs:293
void Play()
Start playing audio clip from the beginning.
Definition Audio.cs:407
void Set3DDistances(float min, float max)
Sets the Audio 3D distances.
Definition Audio.cs:529
float Spread
The spread angle (in degrees) of a 3d stereo or multichannel sound in speaker space.
Definition Audio.cs:225
float StereoPan
Pans a playing sound in a stereo way (left or right). This only applies to sounds that are Mono or St...
Definition Audio.cs:161
Audio(string name, AudioType audioType, AudioClip clip, bool loop, bool persist, float volume, float fadeInValue, float fadeOutValue, Transform sourceTransform)
Definition Audio.cs:334
bool Stopping
Whether the audio is stopping.
Definition Audio.cs:36
void UnPause()
Resume playing audio clip.
Definition Audio.cs:469
AudioSource AudioSource
The audio source that is responsible for this audio. Do not modify the audiosource directly as it cou...
Definition Audio.cs:56
float ReverbZoneMix
The amount by which the signal from the AudioSource will be mixed into the global reverb associated w...
Definition Audio.cs:193
bool IsPlaying
Whether the audio is currently playing.
Definition Audio.cs:26
int Priority
Sets the priority of the audio.
Definition Audio.cs:129
float SpatialBlend
Sets how much this AudioSource is affected by 3D spatialisation calculations (attenuation,...
Definition Audio.cs:177
void Update()
Update loop of the Audio. This is automatically called from the sound manager itself....
Definition Audio.cs:538
static int audioCounter
Definition Audio.cs:311
bool Loop
Whether the audio will be lopped.
Definition Audio.cs:97
Transform SourceTransform
The source transform of the audio.
Definition Audio.cs:62
float initTargetVolume
Definition Audio.cs:328
bool Paused
Whether the audio is paused.
Definition Audio.cs:31
bool Pooled
Whether the audio is currently pooled. Do not modify this value, it is specifically used by EazySound...
Definition Audio.cs:46
void Stop()
Stop playing audio clip.
Definition Audio.cs:448
AudioClip Clip
Audio clip to play/is playing.
Definition Audio.cs:81
void Play(float volume)
Start playing audio clip from the beggining.
Definition Audio.cs:416
void Pause()
Pause playing audio clip.
Definition Audio.cs:460
AudioClip clip
Definition Audio.cs:313
float Max3DDistance
(Logarithmic rolloff) MaxDistance is the distance a sound stops attenuating at.
Definition Audio.cs:257
AudioRolloffMode rolloffMode
Definition Audio.cs:323
float Min3DDistance
Within the Min distance the audio will cease to grow louder in volume.
Definition Audio.cs:273
AudioType Type
The type of the Audio.
Definition Audio.cs:21
float Volume
The volume of the audio. Use SetVolume to change it.
Definition Audio.cs:51
float Pitch
The pitch of the audio.
Definition Audio.cs:145
float fadeInterpolater
Definition Audio.cs:330
float tempFadeSeconds
Definition Audio.cs:329
Transform sourceTransform
Definition Audio.cs:332
void SetVolume(float volume)
Sets the audio volume.
Definition Audio.cs:488
float FadeOutSeconds
How many seconds it needs for the audio to fade out/ reach target volume (if lower than current)
Definition Audio.cs:298
float DopplerLevel
The doppler scale of the audio.
Definition Audio.cs:209
AudioType
Enum representing the type of audio.
Definition Audio.cs:305
void CreateAudiosource()
Creates and initializes the audiosource component with the appropriate values.
Definition Audio.cs:385
float onFadeStartVolume
Definition Audio.cs:331
void Resume()
Resume playing audio clip.
Definition Audio.cs:478
bool Activated
Whether the audio is created and updated at least once.
Definition Audio.cs:41
int AudioID
The ID of the Audio.
Definition Audio.cs:16
void SetVolume(float volume, float fadeSeconds)
Sets the audio volume.
Definition Audio.cs:505
bool Persist
Whether the audio persists in between scene changes.
Definition Audio.cs:288
Static class responsible for playing and managing audio and sounds.
static bool RestoreAudioFromPool(Audio.AudioType audioType, int audioID)
Restores and re-adds a pooled audio to its corresponding audio dictionary.
static float GlobalVolume
Global volume.
static GameObject Gameobject
The gameobject that the sound manager is attached to.