Blipples

SpawnManager

Feb 15th, 2025 (edited)
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. /// <summary>
  2. /// Manages the spawning and despawning of networked objects.
  3. /// </summary>
  4. public class SpawnManager : NetworkBehaviour
  5. {
  6. /// <summary>
  7. /// Singleton Instance of the SpawnManager.
  8. /// </summary>
  9. public static SpawnManager Instance;
  10.  
  11. /// <summary>
  12. /// Reference to the NetworkManager.
  13. /// </summary>
  14. [SerializeField]
  15. private NetworkManager networkManager;
  16.  
  17. /// <summary>
  18. /// Initializes the singleton Instance and sets up event handlers.
  19. /// </summary>
  20. void Awake()
  21. {
  22. if (Instance == null)
  23. {
  24. Instance = this;
  25. }
  26.  
  27. networkManager = InstanceFinder.NetworkManager;
  28. }
  29.  
  30. [ServerRpc(RequireOwnership = false)]
  31. public void RPC_SpawnObjectAtPosition(GameObject prefab, Scene scene, Vector2 position)
  32. {
  33. SpawnObjectAtPosition(prefab, scene, position);
  34. }
  35.  
  36. private void SpawnObjectAtPosition(GameObject prefab, Scene scene, Vector2 position)
  37. {
  38. NetworkObject networkObject = networkManager.GetPooledInstantiated(prefab, position, Quaternion.identity, true);
  39. networkManager.ServerManager.Spawn(networkObject, null, scene);
  40. }
  41.  
  42. /// <summary>
  43. /// Despawns a networked object.
  44. /// </summary>
  45. /// <param name="nob">The networked object to despawn.</param>
  46. [ServerRpc(RequireOwnership = false)]
  47. public void RPC_DespawnObject(NetworkObject nob)
  48. {
  49. DespawnObject(nob);
  50. }
  51.  
  52. private void DespawnObject(NetworkObject nob)
  53. {
  54. networkManager.ServerManager.Despawn(nob);
  55. }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment