@@ -53,13 +53,6 @@ public static UAudioManager Instance
5353 base . Awake ( ) ;
5454
5555 CreateEventsDictionary ( ) ;
56-
57- if ( events . Length > 0 )
58- {
59- string key = events [ 0 ] . name ;
60- PlayEvent ( key ) ;
61- StopEvent ( key ) ;
62- }
6356 }
6457
6558 /// <summary>
@@ -135,14 +128,15 @@ private void PlayEvent(string eventName,
135128 emitter = ApplyAudioEmitterTransform ( emitter ) ;
136129 if ( emitter == null )
137130 {
138- return ;
131+ //if emitter is null, use the uaudiomanager gameobject(2dsound)
132+ emitter = gameObject ;
139133 }
140134
141135 AudioEvent currentEvent ;
142136
143137 if ( ! eventsDictionary . TryGetValue ( eventName , out currentEvent ) )
144138 {
145- Debug . LogErrorFormat ( this , "Could not find event \" {0}\" " , eventName ) ;
139+ Debug . LogFormat ( "Could not find event \" {0}\" " , eventName ) ;
146140 return ;
147141 }
148142
@@ -196,80 +190,85 @@ private void PlayEvent(AudioEvent audioEvent,
196190 }
197191
198192 /// <summary>
199- /// Stops all events with the name matching eventName .
193+ /// Stop event by gameObject .
200194 /// </summary>
201- /// <param name="eventName">The name associated with the AudioEvents.</param>
202- public void StopAllEvents ( string eventName )
195+ /// <param name="eventName"></param>
196+ /// <param name="gameObjectToStop"></param>
197+ /// <param name="fadeOutTime"></param>
198+ public void StopEventsOnGameObject ( string eventName , GameObject gameObjectToStop , float fadeOutTime = 0f )
203199 {
204200 for ( int i = activeEvents . Count - 1 ; i >= 0 ; i -- )
205201 {
206- if ( activeEvents [ i ] . audioEvent . name == eventName )
202+ ActiveEvent activeEvent = activeEvents [ i ] ;
203+
204+ if ( activeEvent . AudioEmitter == gameObjectToStop )
207205 {
208- StopEvent ( activeEvents [ i ] ) ;
206+ StopEvent ( activeEvent . audioEvent . name , gameObjectToStop , fadeOutTime ) ;
209207 }
210208 }
211209 }
212210
211+
213212 /// <summary>
214- /// Stops an AudioEvent .
213+ /// Stops all events by name .
215214 /// </summary>
216215 /// <param name="eventName">The name associated with the AudioEvent.</param>
217- public void StopEvent ( string eventName )
216+ /// <param name="fadeOutTime">The amount of time in seconds to completely fade out the sound.</param>
217+ public void StopAllEvents ( string eventName , GameObject emitter = null , float fadeOutTime = 0f )
218218 {
219- //if there's a default fade out time specified in the event, use it
219+
220220 for ( int i = activeEvents . Count - 1 ; i >= 0 ; i -- )
221221 {
222- if ( activeEvents [ i ] . audioEvent . name == eventName )
222+ ActiveEvent activeEvent = activeEvents [ i ] ;
223+
224+ if ( activeEvent . audioEvent . name == eventName )
223225 {
224- StopEvent ( eventName , activeEvents [ i ] . audioEvent . fadeOutTime ) ;
226+ if ( fadeOutTime > 0 )
227+ {
228+ StartCoroutine ( StopEventWithFadeCoroutine ( activeEvent , fadeOutTime ) ) ;
229+ }
230+ else
231+ {
232+ StartCoroutine ( StopEventWithFadeCoroutine ( activeEvent , activeEvent . audioEvent . fadeOutTime ) ) ;
233+ }
225234 }
226235 }
227236 }
228237
229238 /// <summary>
230- /// Stops an AudioEvent .
239+ /// Stops all .
231240 /// </summary>
232- /// <param name="eventName">The name associated with the AudioEvent.</param>
233- /// <param name="emitter">The GameObject on which the AudioEvent will stopped.</param>
234- public void StopEvent ( string eventName , GameObject emitter )
241+ /// <param name="fadeOutTime">The amount of time in seconds to completely fade out the sound.</param>
242+ public void StopAll ( GameObject emitter = null , float fadeOutTime = 0f )
235243 {
236- emitter = ApplyAudioEmitterTransform ( emitter ) ;
237- if ( emitter == null )
244+ foreach ( ActiveEvent activeEvent in activeEvents )
238245 {
239- return ;
240- }
241-
242- for ( int i = activeEvents . Count - 1 ; i >= 0 ; i -- )
243- {
244- if ( activeEvents [ i ] . audioEvent . name == eventName && activeEvents [ i ] . AudioEmitter == emitter )
246+ if ( fadeOutTime > 0 )
245247 {
246- StopEvent ( activeEvents [ i ] . audioEvent . name , emitter , activeEvents [ i ] . audioEvent . fadeOutTime ) ;
248+ StartCoroutine ( StopEventWithFadeCoroutine ( activeEvent , fadeOutTime ) ) ;
249+ }
250+ else
251+ {
252+ StartCoroutine ( StopEventWithFadeCoroutine ( activeEvent , activeEvent . audioEvent . fadeOutTime ) ) ;
247253 }
248254 }
249255 }
250256
251- /// <summary>
252- /// Stops an AudioEvent.
253- /// </summary>
254- /// <param name="eventName">The name associated with the AudioEvent.</param>
255- /// <param name="fadeTime">The amount of time in seconds to completely fade out the sound.</param>
256- public void StopEvent ( string eventName , float fadeTime )
257- {
258- StopEvent ( eventName , gameObject , fadeTime ) ;
259- }
257+
260258
261259 /// <summary>
262260 /// Stops an AudioEvent.
263261 /// </summary>
264262 /// <param name="eventName">The name associated with the AudioEvent.</param>
265263 /// <param name="emitter">The GameObject on which the AudioEvent will stopped.</param>
266264 /// <param name="fadeTime">The amount of time in seconds to completely fade out the sound.</param>
267- public void StopEvent ( string eventName , GameObject emitter , float fadeTime )
265+ public void StopEvent ( string eventName , GameObject emitter = null , float fadeOutTime = 0f )
268266 {
269267 emitter = ApplyAudioEmitterTransform ( emitter ) ;
270268 if ( emitter == null )
271269 {
272- return ;
270+ //if emitter is null, use the uaudiomanager gameobject(2dsound)
271+ emitter = gameObject ;
273272 }
274273
275274 for ( int i = activeEvents . Count - 1 ; i >= 0 ; i -- )
@@ -278,7 +277,16 @@ public void StopEvent(string eventName, GameObject emitter, float fadeTime)
278277
279278 if ( activeEvent . audioEvent . name == eventName && activeEvent . AudioEmitter == emitter )
280279 {
281- StartCoroutine ( StopEventWithFadeCoroutine ( activeEvent , fadeTime ) ) ;
280+ //if there's no fade specified, use the fade stored in the event
281+ if ( fadeOutTime > 0f )
282+ {
283+ StartCoroutine ( StopEventWithFadeCoroutine ( activeEvent , fadeOutTime ) ) ;
284+ }
285+ else
286+ {
287+ StartCoroutine ( StopEventWithFadeCoroutine ( activeEvent , activeEvents [ i ] . audioEvent . fadeOutTime ) ) ;
288+
289+ }
282290 }
283291 }
284292 }
0 commit comments