using System;
using System.Collections.Generic;
using Unity.XR.CoreUtils;
using UnityEngine.SceneManagement;
namespace UnityEngine.XR.Simulation
{
///
/// Manages the runtime simulation scene and environment instance.
///
class SimulationSceneManager : BaseSimulationSceneManager
{
static readonly CreateSceneParameters k_EnvironmentSceneParameters = new (LocalPhysicsMode.Physics3D);
readonly HashSet lightInstances = new();
internal IReadOnlyCollection simulationEnvironmentLights => lightInstances;
readonly HashSet anchorInstances = new();
internal IReadOnlyCollection simulationEnvironmentAnchors => anchorInstances;
readonly HashSet boundingBoxInstances = new();
internal IReadOnlyCollection simulationEnvironmentBoundingBoxes => boundingBoxInstances;
private readonly HashSet imageInstances = new();
internal IReadOnlyCollection simulationEnvironmentImages => imageInstances;
internal void TrackLight(SimulatedLight light)
{
lightInstances.Add(light);
}
internal void UntrackLight(SimulatedLight light)
{
lightInstances.Remove(light);
}
internal void TrackAnchor(SimulatedAnchor anchor)
{
anchorInstances.Add(anchor);
}
internal void UntrackAnchor(SimulatedAnchor anchor)
{
anchorInstances.Remove(anchor);
}
internal void TrackBoundingBox(SimulatedBoundingBox box)
{
boundingBoxInstances.Add(box);
}
internal void UntrackBoundingBox(SimulatedBoundingBox box)
{
boundingBoxInstances.Remove(box);
}
internal void TrackImage(SimulatedTrackedImage image)
{
imageInstances.Add(image);
}
internal void UntrackImage(SimulatedTrackedImage image)
{
imageInstances.Remove(image);
}
protected override Scene CreateEnvironmentScene()
{
ClearTrackedObjects();
var scene = SceneManager.CreateScene(GenerateUniqueSceneName(), k_EnvironmentSceneParameters);
if (!scene.IsValid())
throw new InvalidOperationException("Environment scene could not be created.");
return scene;
}
protected override GameObject GetOrCreateEnvironmentPrefab()
{
return GetPreferencesEnvironmentPrefab();
}
protected override void DestroyEnvironmentScene()
{
ClearTrackedObjects();
if (environmentScene.IsValid() && environmentScene != default)
SceneManager.UnloadSceneAsync(environmentScene);
}
protected override GameObject InstantiateEnvironment(GameObject environmentPrefab)
{
return GameObjectUtils.Instantiate(environmentPrefab);
}
void ClearTrackedObjects()
{
lightInstances.Clear();
anchorInstances.Clear();
boundingBoxInstances.Clear();
}
}
}