VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_PlayerSpawn.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using Unity.Netcode;
5 using AVR.Core;
6 using System.Linq;
7 
8 namespace AVR.Net {
9  /// <summary>
10  /// Used for convenient spawning of players with respective network prefabs.
11  /// </summary>
12  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_net_1_1_a_v_r___player_spawn.html")]
14  {
15  public int max_players = 4;
16 
17  public List<Transform> spawnLocations = new List<Transform>();
18 
19  public bool disconnectClientIfFailed = true;
20 
21  public NetworkObject prefab;
22 
23  protected virtual NetworkObject getPrefab() => prefab;
24 
25  public override void OnNetworkSpawn()
26  {
27  base.OnNetworkSpawn();
28  spawnServerRpc(NetworkManager.Singleton.LocalClientId);
29  }
30 
31  void OnDrawGizmos() {
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);
37  }
38  }
39 
40  [ServerRpc(RequireOwnership = false)]
41  protected virtual void spawnServerRpc(ulong clientId)
42  {
43  AVR_DevConsole.cprint("Client #"+clientId+" requested spawn", this);
44 
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);
48  return;
49  }
50 
51  Transform spawnLocation = transform;
52  if(spawnLocations.Count>0) {
53  spawnLocation = spawnLocations[(int)clientId % spawnLocations.Count];
54  }
55 
56  try {
57  // If the client already has a playerobject, destroy it before creating a new one.
58  if (NetworkManager.Singleton.SpawnManager.GetPlayerNetworkObject(clientId) != null)
59  {
60  AVR_DevConsole.cprint("Client #"+clientId+" already has a playerNetworkObject! Despawning.", this);
61  NetworkManager.Singleton.SpawnManager.GetPlayerNetworkObject(clientId).Despawn(destroy: true);
62  }
63 
64  // Instantiate and spawn the object
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);
68  }
69  // Deal with errors
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);
75  }
76  }
77  }
78  }
79 }
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()