Files
Scampz/Assets/Scripts/GameManager/GameManager.cs
2022-08-16 01:49:55 -07:00

68 lines
1.9 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Scampz.GameJam
{
public class GameManager : MonoBehaviour
{
private static Animator _animator;
private static GameManager _instance;
public static GameManager Instance
{
get
{
if (!_instance)
{
_instance = new GameObject().AddComponent<GameManager>();
_animator = new GameObject().AddComponent<Animator>();
_instance.name = _instance.GetType().ToString();
DontDestroyOnLoad(_instance.gameObject);
DontDestroyOnLoad(_animator.gameObject);
}
return _instance;
}
}
public void LoadScene(string sceneName, Transform spawnPoint, LoadSceneMode loadSceneMode)
{
StartCoroutine(LoadSceneAsync(sceneName, loadSceneMode));
var player = GameObject.FindGameObjectWithTag("Player");
var cc = player.GetComponent<CharacterController>();
cc.enabled = false;
player.transform.position = spawnPoint.transform.position;
player.transform.rotation = spawnPoint.transform.rotation;
cc.enabled = true;
}
public void UnloadScene(string sceneName)
{
StartCoroutine(UnloadSceneAsync(sceneName));
}
private IEnumerator LoadSceneAsync(string sceneName, LoadSceneMode loadSceneMode)
{
var loadSceneOperation = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
loadSceneOperation.allowSceneActivation = false;
while (!loadSceneOperation.isDone)
{
yield return null;
if (loadSceneOperation.progress >= 0.9f)
break;
}
loadSceneOperation.allowSceneActivation = true;
yield return loadSceneOperation;
}
private IEnumerator UnloadSceneAsync(string sceneName)
{
yield return null;
if (SceneManager.GetSceneByName(sceneName).IsValid())
SceneManager.UnloadSceneAsync(sceneName);
}
}
}