-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathSceneLoader.cs
More file actions
60 lines (47 loc) · 1.62 KB
/
SceneLoader.cs
File metadata and controls
60 lines (47 loc) · 1.62 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
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Meta.Multiplayer.Core
{
/// <summary>
/// Handle scenes loading and keeps tracks of the current loaded scene and loading scenes through the NetCode
/// NetworkManager.
/// </summary>
public class SceneLoader
{
private static string s_currentScene = null;
#if UNITY_EDITOR
[UnityEditor.InitializeOnEnterPlayMode]
private static void Reset()
{
s_currentScene = null;
}
#endif
public bool SceneLoaded { get; private set; } = false;
public bool AllowSceneReload { get; set; } = false;
public string CurrentScene => s_currentScene;
public SceneLoader() => SceneManager.sceneLoaded += OnSceneLoaded;
~SceneLoader()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
SceneLoaded = true;
s_currentScene = scene.name;
_ = SceneManager.SetActiveScene(scene);
}
public void LoadScene(string scene, bool useNetManager = true)
{
if (!AllowSceneReload && !useNetManager && scene == s_currentScene) return;
SceneLoaded = false;
if (useNetManager && NetworkManager.Singleton.IsClient)
{
_ = NetworkManager.Singleton.SceneManager.LoadScene(scene, LoadSceneMode.Single);
return;
}
_ = SceneManager.LoadSceneAsync(scene);
}
}
}