-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneLoader.cs
More file actions
37 lines (33 loc) · 1.3 KB
/
SceneLoader.cs
File metadata and controls
37 lines (33 loc) · 1.3 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
using UnityEngine;
using UnityEngine.SceneManagement;
public static class SceneLoader {
public static float SceneLoadProgress => loadStatus == null ? 0f : loadStatus.progress;
static AsyncOperation loadStatus = null;
const int mainMenuIndex = 0;
const int gameIndex = 2;
const int loadingIndex = 1;
public static void LoadMainMenu() => StartLoadScene(mainMenuIndex);
public static void LoadGame() => StartLoadScene(gameIndex);
public static void ReloadScene() => StartLoadScene(SceneManager.GetActiveScene().buildIndex);
async static void StartLoadScene(int buildIndex) {
// Load the loading scene
SceneManager.LoadScene(loadingIndex);
// Force a wait so that the loading scene shows
await Awaitable.WaitForSecondsAsync(0.1f);
// Load target scene and save progress for loading scene
loadStatus = SceneManager.LoadSceneAsync(buildIndex);
loadStatus.completed += OnCompleteLoad;
}
static void OnCompleteLoad(AsyncOperation _) {
loadStatus.completed -= OnCompleteLoad;
loadStatus = null;
}
}
public class LoadSceneStatus {
public float Progress {get; private set;}
public void SetProgress(float value, object setter) {
if (setter is SceneLoader) {
Progress = value;
}
}
}