1 using System.Collections;
2 using System.Collections.Generic;
15 public int max_players = 4;
17 public List<Transform> spawnLocations =
new List<Transform>();
19 public bool disconnectClientIfFailed =
true;
23 protected virtual NetworkObject getPrefab() => prefab;
27 base.OnNetworkSpawn();
28 spawnServerRpc(NetworkManager.Singleton.LocalClientId);
32 Gizmos.color = Color.yellow;
33 foreach(var loc
in spawnLocations) {
34 if(loc==null)
continue;
35 Gizmos.DrawSphere(loc.position, 0.1f);
36 Gizmos.DrawLine(loc.position, loc.position + loc.forward);
40 [ServerRpc(RequireOwnership =
false)]
43 AVR_DevConsole.cprint(
"Client #"+clientId+
" requested spawn",
this);
45 if(NetworkManager.ConnectedClientsList.Count + 1 > max_players) {
46 AVR_DevConsole.cerror(
"Connected client would exceed the maximum number of allowed players ("+max_players+
"). Disconnecting client.",
this);
47 NetworkManager.DisconnectClient(clientId);
51 Transform spawnLocation = transform;
52 if(spawnLocations.Count>0) {
53 spawnLocation = spawnLocations[(int)clientId % spawnLocations.Count];
58 if (NetworkManager.Singleton.SpawnManager.GetPlayerNetworkObject(clientId) != null)
60 AVR_DevConsole.cprint(
"Client #"+clientId+
" already has a playerNetworkObject! Despawning.",
this);
61 NetworkManager.Singleton.SpawnManager.GetPlayerNetworkObject(clientId).Despawn(destroy:
true);
65 AVR_DevConsole.cprint(
"Instantiating playerPrefab as playerObject for client #"+clientId,
this);
66 GameObject obj = Instantiate(getPrefab().gameObject, spawnLocation.position, spawnLocation.rotation);
67 obj.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
70 catch(System.Exception e) {
71 AVR_DevConsole.cerror(
"PlayerSpawn failed with following exception: "+e.Message,
this);
72 if(disconnectClientIfFailed) {
73 AVR_DevConsole.cerror(
"Disconnecting client due to failed spawn.",
this);
74 NetworkManager.DisconnectClient(clientId);
virtual void spawnServerRpc(ulong clientId)
Monobehaviour but with an added URL button to a documentation page.
Used for convenient spawning of players with respective network prefabs.
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
override void OnNetworkSpawn()