Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2024
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. using UnityEngine;
  2. using Unity.Netcode;
  3. using UnityEngine.InputSystem;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine.SceneManagement;
  7. public class CustomSceneManager : NetworkBehaviour
  8. {
  9. public static CustomSceneManager Instance;
  10.  
  11. // Public list to assign objects that should not be destroyed on load
  12. public List<GameObject> dontDestroyOnLoadObjects;
  13. private List<string> sceneNames = new List<string>();
  14.  
  15. private InputAction changeSceneAction;
  16. private NetworkVariable<int> currentSceneIndex = new NetworkVariable<int>();
  17.  
  18. // Add this line to declare the event
  19. public event System.Action OnSceneLoadedEvent;
  20.  
  21. private void Awake()
  22. {
  23. if (Instance != null && Instance != this)
  24. {
  25. Destroy(gameObject);
  26. return;
  27. }
  28. Instance = this;
  29. DontDestroyOnLoad(gameObject);
  30.  
  31. // Set all objects in the list to not be destroyed on load
  32. foreach (var obj in dontDestroyOnLoadObjects)
  33. {
  34. DontDestroyOnLoad(obj);
  35. }
  36.  
  37. // Populate sceneNames list with scenes from build settings
  38. for (int i = 0; i < UnityEngine.SceneManagement.SceneManager.sceneCountInBuildSettings; i++)
  39. {
  40. string scenePath = UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex(i);
  41. string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath);
  42. sceneNames.Add(sceneName);
  43. }
  44.  
  45. // Setup input action
  46. var inputActionAsset = new InputActionAsset();
  47. var gameplayActionMap = inputActionAsset.AddActionMap("Gameplay");
  48. changeSceneAction = gameplayActionMap.AddAction("ChangeScene", binding: "<Keyboard>/1");
  49.  
  50. changeSceneAction.performed += OnChangeScene;
  51. changeSceneAction.Enable();
  52.  
  53. // Initialize current scene index
  54. currentSceneIndex.Value = sceneNames.IndexOf(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
  55. }
  56.  
  57. private void OnEnable()
  58. {
  59. NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += OnSceneLoaded;
  60. }
  61.  
  62. private void OnDisable()
  63. {
  64. if (NetworkManager.Singleton != null)
  65. {
  66. NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= OnSceneLoaded;
  67. }
  68. changeSceneAction.performed -= OnChangeScene;
  69. changeSceneAction.Disable();
  70. }
  71.  
  72. private void OnChangeScene(InputAction.CallbackContext context)
  73. {
  74. if (IsServer && sceneNames.Count > 0)
  75. {
  76. ChangeToNextScene();
  77. }
  78. }
  79.  
  80. [ServerRpc(RequireOwnership = false)]
  81. private void LoadSceneServerRpc(int sceneIndex)
  82. {
  83. if (IsServer)
  84. {
  85. LoadSceneAsync(sceneIndex);
  86. }
  87. }
  88.  
  89. private void LoadSceneAsync(int sceneIndex)
  90. {
  91. NetworkManager.Singleton.SceneManager.LoadScene(sceneNames[sceneIndex], LoadSceneMode.Single);
  92. Debug.Log($"Started loading scene {sceneNames[sceneIndex]} asynchronously");
  93. }
  94.  
  95. private void OnSceneLoaded(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut)
  96. {
  97. Debug.Log($"Scene {sceneName} loaded. Clients completed: {clientsCompleted.Count}, Clients timed out: {clientsTimedOut.Count}");
  98. if (IsServer)
  99. {
  100. currentSceneIndex.Value = sceneNames.IndexOf(sceneName);
  101. NetworkObjChecker.Instance.SpawnInSceneNetworkObjects();
  102.  
  103. // Invoke the event
  104. OnSceneLoadedEvent?.Invoke();
  105.  
  106. // Trigger object spawn in SceneobjSpawner
  107. if (SceneobjSpawner.Instance != null)
  108. {
  109. Debug.Log($"Triggering SpawnNetworkedObject for scene {sceneName}");
  110. SceneobjSpawner.Instance.SpawnNetworkedObject();
  111. }
  112. else
  113. {
  114. Debug.LogWarning("SceneobjSpawner.Instance is null. Make sure it's set up correctly.");
  115. }
  116. }
  117. }
  118.  
  119. private void SpawnNetworkObjects()
  120. {
  121. NetworkObject[] sceneObjects = FindObjectsOfType<NetworkObject>();
  122. foreach (NetworkObject netObj in sceneObjects)
  123. {
  124. if (!netObj.IsSpawned)
  125. {
  126. netObj.Spawn();
  127. }
  128. }
  129. }
  130.  
  131. public override void OnNetworkSpawn()
  132. {
  133. if (IsServer)
  134. {
  135. foreach (var playerPrefab in dontDestroyOnLoadObjects)
  136. {
  137. NetworkManager.Singleton.AddNetworkPrefab(playerPrefab);
  138. }
  139. currentSceneIndex.Value = sceneNames.IndexOf(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
  140. }
  141. }
  142.  
  143. public void ChangeScene(string sceneName)
  144. {
  145. if (IsServer)
  146. {
  147. int sceneIndex = sceneNames.IndexOf(sceneName);
  148. if (sceneIndex != -1)
  149. {
  150. LoadSceneServerRpc(sceneIndex);
  151. }
  152. else
  153. {
  154. Debug.LogWarning($"Scene {sceneName} not found in build settings.");
  155. }
  156. }
  157. else
  158. {
  159. Debug.LogWarning("Scene change requested from a client. Only the server can initiate scene changes.");
  160. }
  161. }
  162.  
  163. public void ChangeToNextScene()
  164. {
  165. if (IsServer && sceneNames.Count > 0)
  166. {
  167. int nextSceneIndex = (currentSceneIndex.Value + 1) % sceneNames.Count;
  168. LoadSceneServerRpc(nextSceneIndex);
  169. }
  170. else
  171. {
  172. Debug.LogWarning("Scene change requested from a client or no scenes in build. Only the server can initiate scene changes.");
  173. }
  174. }
  175.  
  176. [ClientRpc]
  177. public void LoadSceneOnClientsClientRpc(int sceneIndex)
  178. {
  179. Debug.Log($"Client received request to load scene index: {sceneIndex}");
  180. // The NetworkManager will handle the scene loading for clients
  181. }
  182.  
  183. public int CurrentSceneIndex => currentSceneIndex.Value;
  184. public string GetCurrentSceneName()
  185. {
  186. return sceneNames[currentSceneIndex.Value];
  187. }
  188. }
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement