|
Post by chrisoshea on Nov 24, 2021 15:58:17 GMT
Hi
So my game previously had a bootloader scene that had the masteraudio prefab in, with dontdestoryonload turned on. The prefab also contained some background music, so that it would play across scenes.
However we changed it so that you could run the game from any scene and not have to go through the bootloader and main menu, to help dev/debug time.
I have a sound manager class, that then calls for playing sound from masteraudio. The trouble is the masteraudio prefab doesn't exist in the scene (because not going through the bootloader).
What would be the best way to handle this on my sound manager class? Thinking if a script calls sound manager play sound, if no masteraudio exists to add it to the scene?
Is masteraudio gets added to a scene whilst there are already dynamic sound groups in the scene (so the MA prefab appears after), do the DSGs still get addedd to master audio?
Thanks Chris
|
|
|
Post by DarkTonic Dev on Nov 24, 2021 16:20:11 GMT
Just put a copy of the MasterAudio Game Object from the bootstrapper Scene into every other scene. It will get automatically destroyed if you arrive to that scene from bootstrapper, but if not then it's there to use.
-Brian
|
|
|
Post by chrisoshea on Nov 25, 2021 16:51:19 GMT
Is the only way to make background music continue to play across scenes, to turn on persist across scenes?
If I have a masteraudio prefab in every single scene with dontdestroy ticked, is there much performance overhead having the original destroy the new instance in the scene?
I'm guessing masteraudio checks to see if another masteraudio exists, BEFORE the dynamic sound groups look for the masteraudio to add to?
Thanks
|
|
|
Post by DarkTonic Dev on Nov 25, 2021 18:11:36 GMT
Yes, that is the only way. How much performance it takes may depend on how many sound groups you have in your non-persistent master audio game object.
Yes that all happens before dsgc's notify Master Audio, because of script execution order.
|
|
|
Post by chrisoshea on Apr 7, 2022 12:55:37 GMT
Hi I am revisting this issue again. Ideally we want an empty MasterAudio prefab with dontdestory in our bootstrap scene, and not have one per scene getting destroyed as we have lots of scenes and quick changes.
If you start the game from bootstrap, all is fine. If I start the game from another scene in editor, I need there to be a masteraudio prefab present.
I made this script to add a MA prefab into the scene if the editor play mode changes to play.
[InitializeOnLoadAttribute] public static class AddMasterAudioOnPlay { static Scene scene; static AddMasterAudioOnPlay() { EditorApplication.playModeStateChanged += ChangePlayModeState; } private static void ChangePlayModeState(PlayModeStateChange state) { if (state == PlayModeStateChange.EnteredPlayMode) { // which scene scene = SceneManager.GetActiveScene(); int sceneIndex = scene.buildIndex; // only add masteraudio prefab to scene in editor if starting scene is higher than 3 if (sceneIndex > 3) { Debug.Log("Add MasterAudio via editor scene=" + sceneIndex); Object maPrefab = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/MasterAudio.prefab", typeof(GameObject)); if (maPrefab != null) { PrefabUtility.InstantiatePrefab(maPrefab); } } } } }
It works, however none of the dynamic sound groups in the scene are added to masteraudio. Any ideas? Thanks
|
|
|
Post by DarkTonic Dev on Apr 7, 2022 14:35:53 GMT
Maybe if you make a script that calls this static method, and make sure its ahead of everything in the Script Execution Order? Then remove InitializeOnLoadAttribute.
|
|