@@ -38,13 +38,13 @@ private class AudioJob {
38
38
public AudioAction action ;
39
39
public AudioType type ;
40
40
public bool fade ;
41
- public float delay ;
41
+ public WaitForSeconds delay ;
42
42
43
43
public AudioJob ( AudioAction _action , AudioType _type , bool _fade , float _delay ) {
44
44
action = _action ;
45
45
type = _type ;
46
46
fade = _fade ;
47
- delay = _delay ;
47
+ delay = _delay > 0f ? new WaitForSeconds ( _delay ) : null ;
48
48
}
49
49
}
50
50
@@ -85,7 +85,7 @@ private void Configure() {
85
85
private void Dispose ( ) {
86
86
// cancel all jobs in progress
87
87
foreach ( DictionaryEntry _kvp in m_JobTable ) {
88
- IEnumerator _job = ( IEnumerator ) _kvp . Value ;
88
+ Coroutine _job = ( Coroutine ) _kvp . Value ;
89
89
StopCoroutine ( _job ) ;
90
90
}
91
91
}
@@ -94,9 +94,8 @@ private void AddJob(AudioJob _job) {
94
94
// cancel any job that might be using this job's audio source
95
95
RemoveConflictingJobs ( _job . type ) ;
96
96
97
- IEnumerator _jobRunner = RunAudioJob ( _job ) ;
97
+ Coroutine _jobRunner = StartCoroutine ( RunAudioJob ( _job ) ) ;
98
98
m_JobTable . Add ( _job . type , _jobRunner ) ;
99
- StartCoroutine ( _jobRunner ) ;
100
99
Log ( "Starting job on [" + _job . type + "] with operation: " + _job . action ) ;
101
100
}
102
101
@@ -105,7 +104,7 @@ private void RemoveJob(AudioType _type) {
105
104
Log ( "Trying to stop a job [" + _type + "] that is not running." ) ;
106
105
return ;
107
106
}
108
- IEnumerator _runningJob = ( IEnumerator ) m_JobTable [ _type ] ;
107
+ Coroutine _runningJob = ( Coroutine ) m_JobTable [ _type ] ;
109
108
StopCoroutine ( _runningJob ) ;
110
109
m_JobTable . Remove ( _type ) ;
111
110
}
@@ -118,12 +117,13 @@ private void RemoveConflictingJobs(AudioType _type) {
118
117
119
118
// cancel jobs that share the same audio track
120
119
AudioType _conflictAudio = AudioType . None ;
120
+ AudioTrack _audioTrackNeeded = GetAudioTrack ( _type , "Get Audio Track Needed" ) ;
121
121
foreach ( DictionaryEntry _entry in m_JobTable ) {
122
122
AudioType _audioType = ( AudioType ) _entry . Key ;
123
123
AudioTrack _audioTrackInUse = GetAudioTrack ( _audioType , "Get Audio Track In Use" ) ;
124
- AudioTrack _audioTrackNeeded = GetAudioTrack ( _type , "Get Audio Track Needed" ) ;
125
124
if ( _audioTrackInUse . source == _audioTrackNeeded . source ) {
126
125
_conflictAudio = _audioType ;
126
+ break ;
127
127
}
128
128
}
129
129
if ( _conflictAudio != AudioType . None ) {
@@ -132,19 +132,23 @@ private void RemoveConflictingJobs(AudioType _type) {
132
132
}
133
133
134
134
private IEnumerator RunAudioJob ( AudioJob _job ) {
135
- yield return new WaitForSeconds ( _job . delay ) ;
135
+ if ( _job . delay != null ) yield return _job . delay ;
136
136
137
137
AudioTrack _track = GetAudioTrack ( _job . type ) ; // track existence should be verified by now
138
138
_track . source . clip = GetAudioClipFromAudioTrack ( _job . type , _track ) ;
139
139
140
+ float _initial = 0f ;
141
+ float _target = 1f ;
140
142
switch ( _job . action ) {
141
143
case AudioAction . START :
142
144
_track . source . Play ( ) ;
143
145
break ;
146
+ case AudioAction . STOP when ! _job . fade :
147
+ _track . source . Stop ( ) ;
148
+ break ;
144
149
case AudioAction . STOP :
145
- if ( ! _job . fade ) {
146
- _track . source . Stop ( ) ;
147
- }
150
+ _initial = 1f ;
151
+ _target = 0f ;
148
152
break ;
149
153
case AudioAction . RESTART :
150
154
_track . source . Stop ( ) ;
@@ -154,17 +158,19 @@ private IEnumerator RunAudioJob(AudioJob _job) {
154
158
155
159
// fade volume
156
160
if ( _job . fade ) {
157
- float _initial = _job . action == AudioAction . START || _job . action == AudioAction . RESTART ? 0 : 1 ;
158
- float _target = _initial == 0 ? 1 : 0 ;
159
161
float _duration = 1.0f ;
160
162
float _timer = 0.0f ;
161
163
162
- while ( _timer < _duration ) {
164
+ while ( _timer <= _duration ) {
163
165
_track . source . volume = Mathf . Lerp ( _initial , _target , _timer / _duration ) ;
164
166
_timer += Time . deltaTime ;
165
167
yield return null ;
166
168
}
167
169
170
+ // if _timer was 0.9999 and Time.deltaTime was 0.01 we would not have reached the target
171
+ // make sure the volume is set to the value we want
172
+ _track . source . volume = _target ;
173
+
168
174
if ( _job . action == AudioAction . STOP ) {
169
175
_track . source . Stop ( ) ;
170
176
}
0 commit comments