You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB
C#

3 weeks ago
using System;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Unity.Multiplayer.Samples.Utilities
{
public class NetworkObjectSpawner : MonoBehaviour
{
public NetworkObject prefabReference;
public void Awake()
{
if (NetworkManager.Singleton && NetworkManager.Singleton.IsServer &&
NetworkManager.Singleton.SceneManager != null)
{
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += SceneManagerOnOnLoadEventCompleted;
}
else
{
Destroy(gameObject);
}
}
void OnDestroy()
{
if (NetworkManager.Singleton && NetworkManager.Singleton.IsServer &&
NetworkManager.Singleton.SceneManager != null)
{
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= SceneManagerOnOnLoadEventCompleted;
}
}
void SceneManagerOnOnLoadEventCompleted(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut)
{
SpawnNetworkObject();
Destroy(gameObject);
}
void SpawnNetworkObject()
{
var instantiatedNetworkObject = Instantiate(prefabReference, transform.position, transform.rotation, null);
SceneManager.MoveGameObjectToScene(instantiatedNetworkObject.gameObject,
SceneManager.GetSceneByName(gameObject.scene.name));
instantiatedNetworkObject.transform.localScale = transform.lossyScale;
instantiatedNetworkObject.Spawn(destroyWithScene: true);
}
}
}