Advertisement
Dieton

ClientConnectionHandler

Jun 11th, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Netcode;
  5.  
  6. public class ClientConnectionHandler : NetworkBehaviour
  7. {
  8.     public NetworkPrefab[] alternatePlayerPrefabs;
  9.  
  10.     public void SetClientPlayerPrefab(int index)
  11.     {
  12.         if (index >= alternatePlayerPrefabs.Length)
  13.         {
  14.             Debug.LogError($"Trying to assign player prefab index of {index} when there are only {alternatePlayerPrefabs.Length} entries!");
  15.             return;
  16.         }
  17.  
  18.         if (NetworkManager.Singleton.IsListening || IsSpawned)
  19.         {
  20.             Debug.LogError("This needs to be set before connecting!");
  21.             return;
  22.         }
  23.  
  24.         NetworkManager.Singleton.NetworkConfig.ConnectionData = System.BitConverter.GetBytes(index);
  25.     }
  26.  
  27.     public override void OnNetworkSpawn()
  28.     {
  29.         if (IsServer)
  30.         {
  31.             NetworkManager.Singleton.ConnectionApprovalCallback += ConnectionApprovalCallback;
  32.         }
  33.     }
  34.  
  35.     private void ConnectionApprovalCallback(byte[] connectionData, ulong clientId, NetworkManager.ConnectionApprovedDelegate callback)
  36.     {
  37.         int playerPrefabIndex = System.BitConverter.ToInt32(connectionData, 0);
  38.  
  39.         if (playerPrefabIndex >= alternatePlayerPrefabs.Length)
  40.         {
  41.             Debug.LogError($"Client provided player prefab index of {playerPrefabIndex} when there are only {alternatePlayerPrefabs.Length} entries!");
  42.             callback(false, 0, null, null, null);
  43.             return;
  44.         }
  45.  
  46.         NetworkPrefab playerPrefab = alternatePlayerPrefabs[playerPrefabIndex];
  47.  
  48.         // Create additional player-specific data
  49.         PlayerData playerData = new PlayerData();
  50.  
  51.         // Set additional player-specific properties
  52.         playerData.playerName = "Player " + clientId;
  53.         playerData.playerScore = 0;
  54.  
  55.         // Create additional scene manager data
  56.         SceneManagerData sceneManagerData = new SceneManagerData();
  57.  
  58.         // Set additional scene manager properties
  59.         sceneManagerData.levelName = "Level 1";
  60.         sceneManagerData.maxPlayers = 4;
  61.  
  62.         // Complete the connection approval response by providing the player prefab, player data, and scene manager data
  63.         callback(true, playerPrefab.PrefabHash, playerData, sceneManagerData, playerPrefab.Prefab);
  64.     }
  65. }
  66.  
  67. [System.Serializable]
  68. public class PlayerData
  69. {
  70.     public string playerName;
  71.     public int playerScore;
  72.     // Add more properties as needed
  73. }
  74.  
  75. [System.Serializable]
  76. public class SceneManagerData
  77. {
  78.     public string levelName;
  79.     public int maxPlayers;
  80.     // Add more properties as needed
  81. }
Tags: Unity Netcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement