GruppeC/frontend/SmartLectureRoom/Assets/SmartRoom/Editor/SmartRoomSceneSetup.cs

83 lines
3.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace SmartRoom.EditorTools
{
/// <summary>
/// Legt beim ersten Öffnen des Projekts die Szenen "Assets/Scenes/SmartRoom.unity" (Classic) und
/// "Assets/Scenes/SmartRoomAmbient.unity" (Optik des Web-Frontends) an.
/// Menü: Smart Room → Szene öffnen / Ambient-Szene öffnen / … neu erstellen.
/// </summary>
[InitializeOnLoad]
static class SmartRoomSceneSetup
{
const string ScenePath = "Assets/Scenes/SmartRoom.unity";
const string AmbientScenePath = "Assets/Scenes/SmartRoomAmbient.unity";
static SmartRoomSceneSetup()
{
if (SessionState.GetBool("SmartRoom.SetupChecked", false)) return;
SessionState.SetBool("SmartRoom.SetupChecked", true);
EditorApplication.delayCall += () =>
{
if (EditorApplication.isPlayingOrWillChangePlaymode) return;
if (!File.Exists(ScenePath)) CreateScene();
if (!File.Exists(AmbientScenePath)) CreateInBackground(AmbientScenePath, RoomStyle.Ambient);
};
}
[MenuItem("Smart Room/Szene öffnen")]
static void OpenScene() => Open(ScenePath, RoomStyle.Classic);
[MenuItem("Smart Room/Ambient-Szene öffnen")]
static void OpenAmbientScene() => Open(AmbientScenePath, RoomStyle.Ambient);
[MenuItem("Smart Room/Szene neu erstellen")]
static void CreateScene() => Create(ScenePath, RoomStyle.Classic);
[MenuItem("Smart Room/Ambient-Szene neu erstellen")]
static void CreateAmbientScene() => Create(AmbientScenePath, RoomStyle.Ambient);
static void Open(string path, RoomStyle style)
{
if (!File.Exists(path)) { Create(path, style); return; }
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
EditorSceneManager.OpenScene(path);
}
static void Create(string path, RoomStyle style)
{
if (!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) return;
var scene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single);
new GameObject("SmartRoom").AddComponent<SmartRoomApp>().style = style;
Save(scene, path);
Debug.Log("[Smart Room] Szene erstellt: " + path + " – Play drücken, um den Raum zu simulieren.");
}
/// <summary>Szene anlegen, ohne die gerade geöffnete Szene zu wechseln.</summary>
static void CreateInBackground(string path, RoomStyle style)
{
var scene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Additive);
var go = new GameObject("SmartRoom");
go.AddComponent<SmartRoomApp>().style = style;
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(go, scene);
Save(scene, path);
EditorSceneManager.CloseScene(scene, true);
Debug.Log("[Smart Room] Szene erstellt: " + path + " – öffnen über Smart Room → Ambient-Szene öffnen.");
}
static void Save(UnityEngine.SceneManagement.Scene scene, string path)
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
EditorSceneManager.SaveScene(scene, path);
var scenes = new List<EditorBuildSettingsScene>(EditorBuildSettings.scenes);
if (!scenes.Exists(s => s.path == path)) scenes.Add(new EditorBuildSettingsScene(path, true));
EditorBuildSettings.scenes = scenes.ToArray();
AssetDatabase.Refresh();
}
}
}