using System;
using System.Collections.Generic;
using UnityEngine.ResourceManagement;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceLocations;
using UnityEngine.ResourceManagement.Util;
using UnityEngine.SceneManagement;
namespace UnityEngine.ResourceManagement.ResourceProviders
{
///
/// Wrapper for scenes. This is used to allow access to the AsyncOperation and delayed activation.
///
public struct SceneInstance
{
Scene m_Scene;
internal AsyncOperation m_Operation;
///
/// The scene instance.
///
public Scene Scene { get { return m_Scene; } internal set { m_Scene = value; } }
///
/// Activate the scene via the AsyncOperation.
///
[Obsolete("Activate() has been deprecated. Please use ActivateAsync().")]
public void Activate()
{
m_Operation.allowSceneActivation = true;
}
///
/// Activate the scene via the AsyncOperation. This is the scene loading AsyncOperation provided by the engine.
/// The documentation for AsyncOperation can be found here: https://docs.unity3d.com/ScriptReference/AsyncOperation.html
///
/// The scene load operation.
public AsyncOperation ActivateAsync()
{
m_Operation.allowSceneActivation = true;
return m_Operation;
}
///
public override int GetHashCode()
{
return Scene.GetHashCode();
}
///
public override bool Equals(object obj)
{
if (!(obj is SceneInstance))
return false;
return Scene.Equals(((SceneInstance)obj).Scene);
}
}
///
/// Interface for scene providers.
///
public interface ISceneProvider
{
///
/// Load a scene at a specified resource location.
///
/// The resource manager to use for loading dependencies.
/// The location of the scene.
/// Load mode for the scene.
/// If true, the scene is activated as soon as it finished loading. Otherwise it needs to be activated via the returned SceneInstance object.
/// The loading priority for the load.
/// An operation handle for the loading of the scene. The scene is wrapped in a SceneInstance object to support delayed activation.
AsyncOperationHandle ProvideScene(ResourceManager resourceManager, IResourceLocation location, LoadSceneMode loadMode, bool activateOnLoad, int priority);
///
/// Release a scene.
///
/// The resource manager to use for loading dependencies.
/// The operation handle used to load the scene.
/// An operation handle for the unload.
AsyncOperationHandle ReleaseScene(ResourceManager resourceManager, AsyncOperationHandle sceneLoadHandle);
}
internal interface ISceneProvider2 : ISceneProvider
{
///
/// Release a scene.
///
/// The resource manager to use for loading dependencies.
/// The operation handle used to load the scene.
/// An operation handle for the unload.
AsyncOperationHandle ReleaseScene(ResourceManager resourceManager, AsyncOperationHandle sceneLoadHandle, UnloadSceneOptions unloadOptions);
}
static internal class SceneProviderExtensions
{
public static AsyncOperationHandle ReleaseScene(this ISceneProvider provider, ResourceManager resourceManager, AsyncOperationHandle sceneLoadHandle, UnloadSceneOptions unloadOptions)
{
if (provider is ISceneProvider2)
return ((ISceneProvider2)provider).ReleaseScene(resourceManager, sceneLoadHandle, unloadOptions);
return provider.ReleaseScene(resourceManager, sceneLoadHandle);
}
}
}