Files
Scampz/Assets/Scripts/CharacterManager.cs

57 lines
1.2 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Scampz.GameJam
{
public class CharacterManager : MonoBehaviour
{
public static CharacterManager Instance;
private bool gameStart;
void Awake()
{
if (!gameStart)
{
Instance = this;
LoadScene(0, LoadSceneMode.Additive);
gameStart = true;
}
}
public void LoadScene(int levelIndex, LoadSceneMode loadSceneMode)
{
StartCoroutine(LoadSceneAsync(levelIndex, loadSceneMode));
}
public void UnloadScene(int levelIndex)
{
StartCoroutine(UnloadSceneAsync(levelIndex));
}
IEnumerator LoadSceneAsync(int levelIndex, LoadSceneMode loadSceneMode)
{
yield return null;
var loadSceneOperation = SceneManager.LoadSceneAsync(levelIndex, loadSceneMode);
loadSceneOperation.allowSceneActivation = false;
while (!loadSceneOperation.isDone)
{
if (loadSceneOperation.progress >= 0.9f)
break;
}
loadSceneOperation.allowSceneActivation = true;
yield return loadSceneOperation;
}
IEnumerator UnloadSceneAsync(int levelIndex)
{
yield return null;
SceneManager.UnloadSceneAsync(levelIndex);
}
}
}