using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace SmartRoom.EditorTools
{
///
/// 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.
///
[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().style = style;
Save(scene, path);
Debug.Log("[Smart Room] Szene erstellt: " + path + " – Play drücken, um den Raum zu simulieren.");
}
/// Szene anlegen, ohne die gerade geöffnete Szene zu wechseln.
static void CreateInBackground(string path, RoomStyle style)
{
var scene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Additive);
var go = new GameObject("SmartRoom");
go.AddComponent().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(EditorBuildSettings.scenes);
if (!scenes.Exists(s => s.path == path)) scenes.Add(new EditorBuildSettingsScene(path, true));
EditorBuildSettings.scenes = scenes.ToArray();
AssetDatabase.Refresh();
}
}
}