Fix scene transitions and add demoable transition between two scenes

This commit is contained in:
2022-08-13 15:28:24 -07:00
parent 4f93893e42
commit e0a2b1a8be
11 changed files with 2185 additions and 349 deletions

View File

@@ -0,0 +1,56 @@
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);
}
}
}

View File

@@ -1,4 +1,3 @@
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
@@ -14,12 +13,12 @@ namespace Scampz.GameJam
public void OnTriggerEnter(Collider collider)
{
animator.SetTrigger("FadeOut");
StartCoroutine(LoadScene(sceneIndex));
CharacterManager.Instance.LoadScene(sceneIndex, LoadSceneMode.Additive);
}
public void OnFadeComplete()
{
StartCoroutine(LoadScene(sceneIndex));
CharacterManager.Instance.LoadScene(sceneIndex, LoadSceneMode.Additive);
}
private void Awake()
@@ -29,27 +28,5 @@ namespace Scampz.GameJam
else
Singleton = this;
}
private void FadeToLevel()
{
animator.SetTrigger("FadeOut");
}
private IEnumerator LoadScene(int levelIndex)
{
yield return null;
var loadSceneOperation = SceneManager.LoadSceneAsync(levelIndex);
loadSceneOperation.allowSceneActivation = false;
while (!loadSceneOperation.isDone)
{
if (loadSceneOperation.progress >= 0.9f)
break;
}
loadSceneOperation.allowSceneActivation = true;
yield return loadSceneOperation;
}
}
}

View File

@@ -0,0 +1,21 @@
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Scampz.GameJam
{
public class LoadScene : MonoBehaviour
{
private bool loaded = false;
public int sceneIndex;
void OnTriggerEnter(Collider collider)
{
if (!loaded)
{
loaded = true;
CharacterManager.Instance.LoadScene(sceneIndex, LoadSceneMode.Additive);
}
}
}
}

View File

@@ -0,0 +1,20 @@
using UnityEngine;
namespace Scampz.GameJam
{
public class UnloadScene : MonoBehaviour
{
public int scene;
private bool unloaded = false;
void OnTriggerEnter(Collider collider)
{
if (!unloaded)
{
unloaded = true;
CharacterManager.Instance.UnloadScene(scene);
}
}
}
}