SoundEmitter AllocEmitter() { // Look for unused emitter foreach (var e in m_Emitters) { if (!e.playing) { e.seqId = m_SequenceId++; return(e); } } // Hunt down one emitter to kill SoundEmitter emitter = null; float distance = float.MinValue; var listenerPos = m_CurrentListener != null ? m_CurrentListener.transform.position : Vector3.zero; foreach (var e in m_Emitters) { var s = e.source; // Skip looping if (s.loop) { continue; } // Pick closest; assuming 2d sounds very close! var dist = 0.0f; if (s.spatialBlend > 0.0f) { dist = (s.transform.position - listenerPos).magnitude; // if tracking another object assume closer var t = s.transform; if (t.parent != m_SourceHolder.transform) { dist *= 0.5f; } } if (dist > distance) { distance = dist; emitter = e; } } if (emitter != null) { emitter.Kill(); emitter.seqId = m_SequenceId++; return(emitter); } GameDebug.Log("Unable to allocate sound emitter!"); return(null); }
SoundEmitter AllocEmitter() { // Look for unused emitter foreach (var e in m_Emitters) { if (!e.playing) { e.seqId = m_SequenceId++; return(e); } } // Hunt down one emitter to kill SoundEmitter emitter = null; float distance = float.MinValue; var listenerPos = m_CurrentListener != null ? m_CurrentListener.transform.position : Vector3.zero; foreach (var e in m_Emitters) { var s = e.source; if (s == null) { // Could happen if parent was killed. Not good, but fixable: GameDebug.LogWarning("Soundemitter had its audiosource destroyed. Making a new."); e.source = MakeAudioSource(); e.repeatCount = 0; s = e.source; } // Skip destroyed sources and looping sources if (s.loop) { continue; } // Pick closest; assuming 2d sounds very close! var dist = 0.0f; if (s.spatialBlend > 0.0f) { dist = (s.transform.position - listenerPos).magnitude; // if tracking another object assume closer var t = s.transform; if (t.parent != m_SourceHolder.transform) { dist *= 0.5f; } } if (dist > distance) { distance = dist; emitter = e; } } if (emitter != null) { emitter.Kill(); emitter.seqId = m_SequenceId++; return(emitter); } GameDebug.Log("Unable to allocate sound emitter!"); return(null); }