@@ -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
@@ -118,12 +118,13 @@ private void RemoveConflictingJobs(AudioType _type) {
118
118
119
119
// cancel jobs that share the same audio track
120
120
AudioType _conflictAudio = AudioType . None ;
121
+ AudioTrack _audioTrackNeeded = GetAudioTrack ( _type , "Get Audio Track Needed" ) ;
121
122
foreach ( DictionaryEntry _entry in m_JobTable ) {
122
123
AudioType _audioType = ( AudioType ) _entry . Key ;
123
124
AudioTrack _audioTrackInUse = GetAudioTrack ( _audioType , "Get Audio Track In Use" ) ;
124
- AudioTrack _audioTrackNeeded = GetAudioTrack ( _type , "Get Audio Track Needed" ) ;
125
125
if ( _audioTrackInUse . source == _audioTrackNeeded . source ) {
126
126
_conflictAudio = _audioType ;
127
+ break ;
127
128
}
128
129
}
129
130
if ( _conflictAudio != AudioType . None ) {
@@ -132,19 +133,23 @@ private void RemoveConflictingJobs(AudioType _type) {
132
133
}
133
134
134
135
private IEnumerator RunAudioJob ( AudioJob _job ) {
135
- yield return new WaitForSeconds ( _job . delay ) ;
136
+ if ( _job . delay != null ) yield return _job . delay ;
136
137
137
138
AudioTrack _track = GetAudioTrack ( _job . type ) ; // track existence should be verified by now
138
139
_track . source . clip = GetAudioClipFromAudioTrack ( _job . type , _track ) ;
139
140
141
+ float _initial = 0f ;
142
+ float _target = 1f ;
140
143
switch ( _job . action ) {
141
144
case AudioAction . START :
142
145
_track . source . Play ( ) ;
143
146
break ;
147
+ case AudioAction . STOP when ! _job . fade :
148
+ _track . source . Stop ( ) ;
149
+ break ;
144
150
case AudioAction . STOP :
145
- if ( ! _job . fade ) {
146
- _track . source . Stop ( ) ;
147
- }
151
+ _initial = 1f ;
152
+ _target = 0f ;
148
153
break ;
149
154
case AudioAction . RESTART :
150
155
_track . source . Stop ( ) ;
@@ -154,17 +159,19 @@ private IEnumerator RunAudioJob(AudioJob _job) {
154
159
155
160
// fade volume
156
161
if ( _job . fade ) {
157
- float _initial = _job . action == AudioAction . START || _job . action == AudioAction . RESTART ? 0 : 1 ;
158
- float _target = _initial == 0 ? 1 : 0 ;
159
162
float _duration = 1.0f ;
160
163
float _timer = 0.0f ;
161
164
162
- while ( _timer < _duration ) {
165
+ while ( _timer <= _duration ) {
163
166
_track . source . volume = Mathf . Lerp ( _initial , _target , _timer / _duration ) ;
164
167
_timer += Time . deltaTime ;
165
168
yield return null ;
166
169
}
167
170
171
+ // if _timer was 0.9999 and Time.deltaTime was 0.01 we would not have reached the target
172
+ // make sure the volume is set to the value we want
173
+ _track . source . volume = _target ;
174
+
168
175
if ( _job . action == AudioAction . STOP ) {
169
176
_track . source . Stop ( ) ;
170
177
}
0 commit comments