Add scene transitions
This commit is contained in:
@@ -6,44 +6,31 @@ namespace Scampz.GameJam
|
||||
{
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
private static Animator _animator;
|
||||
private static GameManager _instance;
|
||||
public static GameManager Instance;
|
||||
private LevelChanger _levelChanger;
|
||||
|
||||
public static GameManager Instance
|
||||
private void Awake()
|
||||
{
|
||||
get
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
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;
|
||||
Destroy(this);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
_levelChanger = GetComponentInChildren<LevelChanger>();
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
StartCoroutine(LoadSceneAsync(sceneName, spawnPoint, loadSceneMode));
|
||||
}
|
||||
|
||||
public void UnloadScene(string sceneName)
|
||||
{
|
||||
StartCoroutine(UnloadSceneAsync(sceneName));
|
||||
}
|
||||
|
||||
private IEnumerator LoadSceneAsync(string sceneName, LoadSceneMode loadSceneMode)
|
||||
private IEnumerator LoadSceneAsync(string sceneName, Transform spawnPoint, LoadSceneMode loadSceneMode)
|
||||
{
|
||||
Debug.Log("Start Level Load");
|
||||
yield return null;
|
||||
var loadSceneOperation = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
|
||||
loadSceneOperation.allowSceneActivation = false;
|
||||
while (!loadSceneOperation.isDone)
|
||||
@@ -55,13 +42,16 @@ namespace Scampz.GameJam
|
||||
|
||||
loadSceneOperation.allowSceneActivation = true;
|
||||
yield return loadSceneOperation;
|
||||
}
|
||||
|
||||
private IEnumerator UnloadSceneAsync(string sceneName)
|
||||
{
|
||||
yield return null;
|
||||
if (SceneManager.GetSceneByName(sceneName).IsValid())
|
||||
SceneManager.UnloadSceneAsync(sceneName);
|
||||
Debug.Log("Stop Level Load");
|
||||
|
||||
if (SceneManager.GetActiveScene().name == sceneName)
|
||||
{
|
||||
Debug.Log("Move Player");
|
||||
var player = GameObject.FindGameObjectWithTag("Player");
|
||||
player.transform.position = spawnPoint.transform.position;
|
||||
player.transform.rotation = spawnPoint.transform.rotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user