Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Unity.Netcode;
- public class ClientConnectionHandler : NetworkBehaviour
- {
- public NetworkPrefab[] alternatePlayerPrefabs;
- public void SetClientPlayerPrefab(int index)
- {
- if (index >= alternatePlayerPrefabs.Length)
- {
- Debug.LogError($"Trying to assign player prefab index of {index} when there are only {alternatePlayerPrefabs.Length} entries!");
- return;
- }
- if (NetworkManager.Singleton.IsListening || IsSpawned)
- {
- Debug.LogError("This needs to be set before connecting!");
- return;
- }
- NetworkManager.Singleton.NetworkConfig.ConnectionData = System.BitConverter.GetBytes(index);
- }
- public override void OnNetworkSpawn()
- {
- if (IsServer)
- {
- NetworkManager.Singleton.ConnectionApprovalCallback += ConnectionApprovalCallback;
- }
- }
- private void ConnectionApprovalCallback(byte[] connectionData, ulong clientId, NetworkManager.ConnectionApprovedDelegate callback)
- {
- int playerPrefabIndex = System.BitConverter.ToInt32(connectionData, 0);
- if (playerPrefabIndex >= alternatePlayerPrefabs.Length)
- {
- Debug.LogError($"Client provided player prefab index of {playerPrefabIndex} when there are only {alternatePlayerPrefabs.Length} entries!");
- callback(false, 0, null, null, null);
- return;
- }
- NetworkPrefab playerPrefab = alternatePlayerPrefabs[playerPrefabIndex];
- // Create additional player-specific data
- PlayerData playerData = new PlayerData();
- // Set additional player-specific properties
- playerData.playerName = "Player " + clientId;
- playerData.playerScore = 0;
- // Create additional scene manager data
- SceneManagerData sceneManagerData = new SceneManagerData();
- // Set additional scene manager properties
- sceneManagerData.levelName = "Level 1";
- sceneManagerData.maxPlayers = 4;
- // Complete the connection approval response by providing the player prefab, player data, and scene manager data
- callback(true, playerPrefab.PrefabHash, playerData, sceneManagerData, playerPrefab.Prefab);
- }
- }
- [System.Serializable]
- public class PlayerData
- {
- public string playerName;
- public int playerScore;
- // Add more properties as needed
- }
- [System.Serializable]
- public class SceneManagerData
- {
- public string levelName;
- public int maxPlayers;
- // Add more properties as needed
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement