Fix scene transitions and add demoable transition between two scenes
This commit is contained in:
56
Assets/Scripts/CharacterManager.cs
Normal file
56
Assets/Scripts/CharacterManager.cs
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
Assets/Scripts/LoadScene.cs
Normal file
21
Assets/Scripts/LoadScene.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Assets/Scripts/UnloadScene.cs
Normal file
20
Assets/Scripts/UnloadScene.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user