fgm24/Assets/Scripts/Multiplayer/NetworkedGameSetup.cs

32 lines
1005 B
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
public class NetworkedGameSetup : NetworkBehaviour
{
[SerializeField] private GameObject PlayerPrefab;
private void Start()
{
DontDestroyOnLoad(this);
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += SceneLoaded;
}
private void SceneLoaded(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut)
{
if (IsHost && sceneName == "Multiplayer")
{
for (int i = 0; i < clientsCompleted.Count; i++)
{
GameObject player = Instantiate(PlayerPrefab, Vector2.up * 3, Quaternion.identity);
player.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientsCompleted[i], true);
}
}
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= SceneLoaded;
}
}