Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d1ad4ea

Browse files
Merge pull request #1 from bixarrio/master
A few changes that may be beneficial
2 parents 600ab80 + 67f06a4 commit d1ad4ea

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

‎Audio/AudioController.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ private class AudioJob {
3838
public AudioAction action;
3939
public AudioType type;
4040
public bool fade;
41-
public float delay;
41+
public WaitForSeconds delay;
4242

4343
public AudioJob(AudioAction _action, AudioType _type, bool _fade, float _delay) {
4444
action = _action;
4545
type = _type;
4646
fade = _fade;
47-
delay = _delay;
47+
delay = _delay>0f?newWaitForSeconds(_delay):null;
4848
}
4949
}
5050

@@ -85,7 +85,7 @@ private void Configure() {
8585
private void Dispose() {
8686
// cancel all jobs in progress
8787
foreach(DictionaryEntry _kvp in m_JobTable) {
88-
IEnumerator _job = (IEnumerator)_kvp.Value;
88+
Coroutine _job = (Coroutine)_kvp.Value;
8989
StopCoroutine(_job);
9090
}
9191
}
@@ -94,9 +94,8 @@ private void AddJob(AudioJob _job) {
9494
// cancel any job that might be using this job's audio source
9595
RemoveConflictingJobs(_job.type);
9696

97-
IEnumerator _jobRunner = RunAudioJob(_job);
97+
Coroutine _jobRunner = StartCoroutine(RunAudioJob(_job));
9898
m_JobTable.Add(_job.type, _jobRunner);
99-
StartCoroutine(_jobRunner);
10099
Log("Starting job on ["+_job.type+"] with operation: "+_job.action);
101100
}
102101

@@ -105,7 +104,7 @@ private void RemoveJob(AudioType _type) {
105104
Log("Trying to stop a job ["+_type+"] that is not running.");
106105
return;
107106
}
108-
IEnumerator _runningJob = (IEnumerator)m_JobTable[_type];
107+
Coroutine _runningJob = (Coroutine)m_JobTable[_type];
109108
StopCoroutine(_runningJob);
110109
m_JobTable.Remove(_type);
111110
}
@@ -118,12 +117,13 @@ private void RemoveConflictingJobs(AudioType _type) {
118117

119118
// cancel jobs that share the same audio track
120119
AudioType _conflictAudio = AudioType.None;
120+
AudioTrack _audioTrackNeeded = GetAudioTrack(_type, "Get Audio Track Needed");
121121
foreach (DictionaryEntry _entry in m_JobTable) {
122122
AudioType _audioType = (AudioType)_entry.Key;
123123
AudioTrack _audioTrackInUse = GetAudioTrack(_audioType, "Get Audio Track In Use");
124-
AudioTrack _audioTrackNeeded = GetAudioTrack(_type, "Get Audio Track Needed");
125124
if (_audioTrackInUse.source == _audioTrackNeeded.source) {
126125
_conflictAudio = _audioType;
126+
break;
127127
}
128128
}
129129
if (_conflictAudio != AudioType.None) {
@@ -132,19 +132,23 @@ private void RemoveConflictingJobs(AudioType _type) {
132132
}
133133

134134
private IEnumerator RunAudioJob(AudioJob _job) {
135-
yield return newWaitForSeconds(_job.delay);
135+
if(_job.delay!=null)yield return _job.delay;
136136

137137
AudioTrack _track = GetAudioTrack(_job.type); // track existence should be verified by now
138138
_track.source.clip = GetAudioClipFromAudioTrack(_job.type, _track);
139139

140+
float _initial = 0f;
141+
float _target = 1f;
140142
switch (_job.action) {
141143
case AudioAction.START:
142144
_track.source.Play();
143145
break;
146+
case AudioAction.STOP when !_job.fade:
147+
_track.source.Stop();
148+
break;
144149
case AudioAction.STOP:
145-
if (!_job.fade) {
146-
_track.source.Stop();
147-
}
150+
_initial = 1f;
151+
_target = 0f;
148152
break;
149153
case AudioAction.RESTART:
150154
_track.source.Stop();
@@ -154,17 +158,19 @@ private IEnumerator RunAudioJob(AudioJob _job) {
154158

155159
// fade volume
156160
if (_job.fade) {
157-
float _initial = _job.action == AudioAction.START || _job.action == AudioAction.RESTART ? 0 : 1;
158-
float _target = _initial == 0 ? 1 : 0;
159161
float _duration = 1.0f;
160162
float _timer = 0.0f;
161163

162-
while (_timer < _duration) {
164+
while (_timer <= _duration) {
163165
_track.source.volume = Mathf.Lerp(_initial, _target, _timer / _duration);
164166
_timer += Time.deltaTime;
165167
yield return null;
166168
}
167169

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+
168174
if (_job.action == AudioAction.STOP) {
169175
_track.source.Stop();
170176
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /