-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneLoader.cs
More file actions
102 lines (87 loc) · 2.73 KB
/
SceneLoader.cs
File metadata and controls
102 lines (87 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public static class SceneLoader
{
private class LoadingMonoBehaviour : MonoBehaviour { }
private static Action onLoaderCallback;
private static AsyncOperation loadingAsyncOperation;
public static void Load(string scene)
{
onLoaderCallback = () =>
{
var loadingGameObject = new GameObject("Loading Game Object");
loadingGameObject.AddComponent<LoadingMonoBehaviour>().StartCoroutine(LoadAsync(scene));
// SceneManager.LoadScene(scene);
if (scene == "MainMenu")
{
GlobalControl.ManualDestroy();
QuestController.ManualDestroy();
}
};
//On scene change have cursor be default
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
SceneManager.LoadScene("Loading");
}
public static void LoadPreviousFloor()
{
LoadByIndex(SceneManager.GetActiveScene().buildIndex - 1);
}
public static void LoadNextFloor()
{
LoadByIndex(SceneManager.GetActiveScene().buildIndex + 1);
}
private static void LoadByIndex(int sceneIndex)
{
onLoaderCallback = () =>
{
var loadingGameObject = new GameObject("Loading Game Object");
loadingGameObject.AddComponent<LoadingMonoBehaviour>().StartCoroutine(LoadByIndexAsync(sceneIndex));
// SceneManager.LoadScene(sceneIndex);
if (sceneIndex == 0)
{
GlobalControl.ManualDestroy();
}
};
SceneManager.LoadScene("Loading");
}
private static IEnumerator LoadAsync(string scene)
{
yield return null;
loadingAsyncOperation = SceneManager.LoadSceneAsync(scene);
while (!loadingAsyncOperation.isDone)
{
yield return null;
}
}
private static IEnumerator LoadByIndexAsync(int sceneIndex)
{
yield return null;
loadingAsyncOperation = SceneManager.LoadSceneAsync(sceneIndex);
while (!loadingAsyncOperation.isDone)
{
yield return null;
}
}
public static float GetLoadingProgress()
{
if (loadingAsyncOperation != null)
{
return Mathf.Clamp01(loadingAsyncOperation.progress / 0.9f);
}
else
{
return 1f;
}
}
public static void LoaderCallback()
{
if (onLoaderCallback != null)
{
onLoaderCallback();
onLoaderCallback = null;
}
}
}