63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using UnityEngine.ResourceManagement.ResourceProviders;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public static class AddressableSceneLoader
|
|
{
|
|
public const string IntroScene = "Intro";
|
|
public const string MainMenuScene = "MainMenu";
|
|
public const string GameScene = "Game";
|
|
|
|
public static void Load(string sceneName)
|
|
{
|
|
AddressableSceneLoadRunner.Instance.Begin(sceneName);
|
|
}
|
|
|
|
public static IEnumerator LoadRoutine(string sceneName)
|
|
{
|
|
AsyncOperationHandle<SceneInstance> handle = Addressables.LoadSceneAsync(sceneName, LoadSceneMode.Single);
|
|
yield return handle;
|
|
if (handle.Status != AsyncOperationStatus.Succeeded)
|
|
Debug.LogError("Failed to load addressable scene '" + sceneName + "': " + handle.OperationException);
|
|
}
|
|
}
|
|
|
|
public class AddressableSceneLoadRunner : MonoBehaviour
|
|
{
|
|
static AddressableSceneLoadRunner instance;
|
|
|
|
public static AddressableSceneLoadRunner Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance != null)
|
|
return instance;
|
|
|
|
var go = new GameObject(nameof(AddressableSceneLoadRunner));
|
|
DontDestroyOnLoad(go);
|
|
instance = go.AddComponent<AddressableSceneLoadRunner>();
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
void Awake()
|
|
{
|
|
if (instance != null && instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
public void Begin(string sceneName)
|
|
{
|
|
StartCoroutine(AddressableSceneLoader.LoadRoutine(sceneName));
|
|
}
|
|
}
|