-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAudioZone.cs
More file actions
155 lines (134 loc) · 5.13 KB
/
AudioZone.cs
File metadata and controls
155 lines (134 loc) · 5.13 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using UnityEngine;
public class AudioZone : MonoBehaviour
{
private enum Options { SetVolume, VolumeOnDistance };
[Header("Type")]
[SerializeField] private Options _Option = Options.SetVolume;
[Header("Target")]
[SerializeField] private Transform _ZoneEffector = null;
[Header("Settings - Zone")]
[SerializeField] private string _AudioTrackName = "";
[SerializeField] private int _Categoryid = 0;
[SerializeField] private float _Volume = 1;
public float Range = 10;
[Tooltip("1 = volume from 0 to max based on how close the effector is to the center.")]
[SerializeField] private float _IncreaseMultiplier = 1;
[Header("3D Audio")]
[SerializeField] private bool _Use3DAudio = true;
[SerializeField] private bool _UseThisPos = true;
[SerializeField] private bool _SetParentToThis = true;
[SerializeField] private bool _UpdateToThisPos = false;
[SerializeField] private bool _CreateNewAudioSource = true;
[SerializeField] private AudioSource _AudioSource;
[Header("CustomCurve")]
[SerializeField] private bool _SetCustomCurve;
[SerializeField] private AnimationCurve _CustomCurve;
// Check effector leaving bounds
private bool _EffectorInBounds;
// Optimization (This way the AudioHandler doesn't have to loop trough the available audiotracks)
private int _AudioTrackID;
// Max distance
private float _MaxDistance;
void Start()
{
if (AudioHandler.AUDIO != null)
{
//3D Audio
if (_Use3DAudio)
{
if (_CreateNewAudioSource)
_AudioTrackName = AudioHandler.AUDIO.DuplicateAudioTrack(_AudioTrackName, _Categoryid);
if (_UseThisPos)
AudioHandler.AUDIO.ChangeAudioPosition(_AudioTrackName, transform.position, _Categoryid);
if (_SetParentToThis)
AudioHandler.AUDIO.ChangeAudioParent(_AudioTrackName, this.transform, _Categoryid);
}
if (_ZoneEffector == null)
{
try
{
_ZoneEffector = GameObject.FindObjectsOfType<AudioListener>()[0].gameObject.transform;
}
catch { Debug.Log("No AudioListener Found In The Scene"); }
}
// Get TrackID
_AudioTrackID = AudioHandler.AUDIO.Get_Track_ID(_AudioTrackName, _Categoryid);
if (_AudioTrackID == -1)
Debug.Log("AudioZone: Track(" + _AudioTrackName + ") Does not Exist");
// Set max distance
_MaxDistance = Range;
_AudioSource = AudioHandler.AUDIO.Get_AudioSource(_AudioTrackID, _Categoryid);
_AudioSource.maxDistance = Range;
_AudioSource.rolloffMode = AudioRolloffMode.Custom;
if (_SetCustomCurve)
_AudioSource.SetCustomCurve(AudioSourceCurveType.CustomRolloff, _CustomCurve);
else
_AudioSource.rolloffMode = AudioRolloffMode.Linear;
_AudioSource.volume = _Volume;
}
else
{
_AudioTrackID = -1;
Debug.Log("AudioZone: AudioHandler does not exist in this scene");
}
}
void Update()
{
if (_AudioTrackID == -1)
return;
if (Vector3.Distance(transform.position, _ZoneEffector.position) <= _MaxDistance)
{
switch (_Option)
{
case Options.SetVolume:
AudioHandler.AUDIO.SetTrackVolume(_AudioTrackID, _Volume, true, _Categoryid);
break;
case Options.VolumeOnDistance:
float distance = Vector3.Distance(transform.position, _ZoneEffector.position);
float newvolume = (1 - (distance / _MaxDistance)) * _Volume * _IncreaseMultiplier;
AudioHandler.AUDIO.SetTrackVolume(_AudioTrackID, newvolume, true, _Categoryid);
break;
}
// Check Effector OnExit
if (!_EffectorInBounds)
_EffectorInBounds = true;
}
else
{
// Effector OnExit
if (_EffectorInBounds)
{
AudioHandler.AUDIO.SetTrackVolume(_AudioTrackID, 0, true, _Categoryid);
_EffectorInBounds = false;
}
}
if (_UpdateToThisPos)
AudioHandler.AUDIO.ChangeAudioPosition(_AudioTrackID, transform.position, _Categoryid);
}
public void PlayTrack()
{
AudioHandler.AUDIO.PlayTrack(_AudioTrackName, _Categoryid);
}
public void StartTrack()
{
AudioHandler.AUDIO.StartTrack(_AudioTrackName, _Categoryid);
}
public void StopTrack()
{
AudioHandler.AUDIO.StopTrack(_AudioTrackName, _Categoryid);
}
public string GetTrackName()
{
return _AudioTrackName;
}
public void Set_Volume(float newvolume)
{
_Volume = newvolume;
_AudioSource.volume = newvolume;
}
private void OnDrawGizmosSelected()
{
Gizmos.color = new Vector4(0, 1f, 0, 0.1f);
Gizmos.DrawSphere(transform.position, Range);
}
}