Skip to content

Commit

Permalink
Do not change open scene when previewing sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkehrwald committed Nov 25, 2024
1 parent 5416805 commit 93fc9f3
Showing 1 changed file with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

Expand Down Expand Up @@ -108,48 +109,51 @@ private bool PlayStopButton()
}

/// <summary>
/// Gets or creates the editor GameObject for playing sounds in the editor.
///
/// The hierarchy looks like that:
///
/// [scene root]
/// |
/// -- EditorScene
/// |
/// -- EditorAudio (with AudioSource component)
/// Gets or creates the AudioSource for playing sounds in the editor.
/// The object containing the AudioSource is created in a new, additively loaded scene
/// to avoid making changes to the user's currently open scene.
/// </summary>
/// <returns>AudioSource of the editor GameObject for test playing audio.</returns>
/// <returns>AudioSource for previewing audio assets in the editor</returns>
private static AudioSource GetOrCreateAudioSource()
{
// todo check whether we'll instantiate those live in the future or rely on a provided prefab
var editorSceneGo = SceneManager.GetActiveScene().GetRootGameObjects()
.FirstOrDefault(go => go.name == "EditorScene");

if (editorSceneGo == null) {
editorSceneGo = new GameObject("EditorScene");
Scene editorScene = GetOrCreateEditorScene();
GameObject editorAudio = GetOrCreateEditorAudioObject(editorScene);
if (!editorAudio.TryGetComponent<AudioSource>(out var audioSource)) {
audioSource = editorAudio.AddComponent<AudioSource>();
}
return audioSource;
}

GameObject editorAudioGo = null;
for (var i = 0; i < editorSceneGo.transform.childCount; i++) {
var go = editorSceneGo.transform.GetChild(i).gameObject;
if (go.name != "EditorAudio") {
continue;
}
editorAudioGo = go;
break;
}
private static Scene GetOrCreateEditorScene()
{
const string sceneName = "VpeEditorScene";

if (editorAudioGo == null) {
editorAudioGo = new GameObject("EditorAudio");
editorAudioGo.transform.SetParent(editorSceneGo.transform);
for (int i = 0; i < SceneManager.loadedSceneCount; i++) {
Scene scene = SceneManager.GetSceneAt(i);
if (scene.name == sceneName)
return scene;
}

var audioSource = editorAudioGo.GetComponent<AudioSource>();
if (!audioSource) {
audioSource = editorAudioGo.AddComponent<AudioSource>();
Scene editorScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);
editorScene.name = sceneName;
return editorScene;
}

private static GameObject GetOrCreateEditorAudioObject(Scene editorScene)
{
const string audioObjName = "AudioPreview";

var audioObj = editorScene.GetRootGameObjects()
.FirstOrDefault(go => go.name == audioObjName);

if (audioObj == null) {
var activeScene = SceneManager.GetActiveScene();
SceneManager.SetActiveScene(editorScene);
audioObj = new GameObject(audioObjName);
SceneManager.SetActiveScene(activeScene);
}

return audioSource;
return audioObj;
}
}
}
Expand Down

0 comments on commit 93fc9f3

Please sign in to comment.