-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCC_CreateAudioSource.cs
More file actions
98 lines (76 loc) · 2.69 KB
/
RCC_CreateAudioSource.cs
File metadata and controls
98 lines (76 loc) · 2.69 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//----------------------------------------------
// Realistic Car Controller
//
// Copyright © 2014 - 2017 BoneCracker Games
// http://www.bonecrackergames.com
// Buğra Özdoğanlar
//
//----------------------------------------------
using UnityEngine;
using System.Collections;
/// <summary>
/// Creates new audiosources at runtime.
/// </summary>
public class RCC_CreateAudioSource : MonoBehaviour {
/// <summary>
/// Creates new audiosource with specified settings.
/// </summary>
public static AudioSource NewAudioSource(GameObject go, string audioName, float minDistance, float maxDistance, float volume, AudioClip audioClip, bool loop, bool playNow, bool destroyAfterFinished){
GameObject audioSourceObject = new GameObject(audioName);
if (go.transform.Find ("All Audio Sources")) {
audioSourceObject.transform.SetParent (go.transform.Find ("All Audio Sources"));
} else {
GameObject allAudioSources = new GameObject ("All Audio Sources");
allAudioSources.transform.SetParent (go.transform, false);
audioSourceObject.transform.SetParent (allAudioSources.transform, false);
}
audioSourceObject.transform.position = go.transform.position;
audioSourceObject.transform.rotation = go.transform.rotation;
audioSourceObject.AddComponent<AudioSource>();
AudioSource source = audioSourceObject.GetComponent<AudioSource> ();
//audioSource.GetComponent<AudioSource>().priority =1;
source.minDistance = minDistance;
source.maxDistance = maxDistance;
source.volume = volume;
source.clip = audioClip;
source.loop = loop;
source.dopplerLevel = .5f;
if(minDistance == 0 && maxDistance == 0)
source.spatialBlend = 0f;
else
source.spatialBlend = 1f;
if (playNow) {
source.playOnAwake = true;
source.Play ();
} else {
source.playOnAwake = false;
}
if(destroyAfterFinished){
if(audioClip)
Destroy(audioSourceObject, audioClip.length);
else
Destroy(audioSourceObject);
}
return source;
}
/// <summary>
/// Adds High Pass Filter to audiosource. Used for turbo.
/// </summary>
public static void NewHighPassFilter(AudioSource source, float freq, int level){
if(source == null)
return;
AudioHighPassFilter highFilter = source.gameObject.AddComponent<AudioHighPassFilter>();
highFilter.cutoffFrequency = freq;
highFilter.highpassResonanceQ = level;
}
/// <summary>
/// Adds Low Pass Filter to audiosource. Used for engine off sounds.
/// </summary>
public static void NewLowPassFilter(AudioSource source, float freq){
if(source == null)
return;
AudioLowPassFilter lowFilter = source.gameObject.AddComponent<AudioLowPassFilter>();
lowFilter.cutoffFrequency = freq;
// lowFilter.highpassResonanceQ = level;
}
}