@@ -162,12 +162,12 @@ internal set
162162 /// <summary>
163163 /// Delegate type called when connection has been approved
164164 /// </summary>
165- /// <param name="clientId">The clientId of the approved client</param>
166- /// <param name="prefabHash ">The prefabHash to use for the client</param>
165+ /// <param name="createPlayerObject">If true, a player object will be created. Otherwise the client will have no object. </param>
166+ /// <param name="playerPrefabHash ">The prefabHash to use for the client. If createPlayerObject is false, this is ignored. If playerPrefabHash is null, the default player prefab is used. </param>
167167 /// <param name="approved">Whether or not the client was approved</param>
168- /// <param name="position">The position to spawn the client at</param>
169- /// <param name="rotation">The rotation to spawn the client with</param>
170- public delegate void ConnectionApprovedDelegate ( ulong clientId , ulong ? prefabHash , bool approved , Vector3 ? position , Quaternion ? rotation ) ;
168+ /// <param name="position">The position to spawn the client at. If null, the prefab position is used. </param>
169+ /// <param name="rotation">The rotation to spawn the client with. If null, the prefab position is used. </param>
170+ public delegate void ConnectionApprovedDelegate ( bool createPlayerObject , ulong ? playerPrefabHash , bool approved , Vector3 ? position , Quaternion ? rotation ) ;
171171 /// <summary>
172172 /// The callback to invoke during connection approval
173173 /// </summary>
@@ -284,15 +284,31 @@ private void OnValidate()
284284 }
285285
286286 int playerPrefabCount = NetworkConfig . NetworkedPrefabs . Count ( x => x . PlayerPrefab == true ) ;
287+
287288 if ( playerPrefabCount == 0 )
289+ {
290+
291+ }
292+
293+ if ( playerPrefabCount == 0 && ! NetworkConfig . ConnectionApproval && NetworkConfig . CreatePlayerPrefab )
288294 {
289295 if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "There is no NetworkedPrefab marked as a PlayerPrefab" ) ;
290296 }
291297 else if ( playerPrefabCount > 1 )
292298 {
293299 if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "Only one networked prefab can be marked as a player prefab" ) ;
294300 }
295- else NetworkConfig . PlayerPrefabHash = NetworkConfig . NetworkedPrefabs . Find ( x => x . PlayerPrefab == true ) . Hash ;
301+
302+ NetworkedPrefab prefab = NetworkConfig . NetworkedPrefabs . FirstOrDefault ( x => x . PlayerPrefab == true ) ;
303+
304+ if ( prefab == null )
305+ {
306+ NetworkConfig . PlayerPrefabHash = prefab . Hash ;
307+ }
308+ else
309+ {
310+ NetworkConfig . PlayerPrefabHash = null ;
311+ }
296312 }
297313
298314 private void Init ( bool server )
@@ -494,7 +510,7 @@ public void StopClient()
494510 /// <summary>
495511 /// Starts a Host
496512 /// </summary>
497- public void StartHost ( Vector3 ? position = null , Quaternion ? rotation = null , ulong ? prefabHash = null , Stream payloadStream = null )
513+ public void StartHost ( Vector3 ? position = null , Quaternion ? rotation = null , bool ? createPlayerObject = null , ulong ? prefabHash = null , Stream payloadStream = null )
498514 {
499515 if ( LogHelper . CurrentLogLevel <= LogLevel . Developer ) LogHelper . LogInfo ( "StartHost()" ) ;
500516 if ( IsServer || IsClient )
@@ -527,12 +543,15 @@ public void StartHost(Vector3? position = null, Quaternion? rotation = null, ulo
527543
528544 ConnectedClientsList . Add ( ConnectedClients [ hostClientId ] ) ;
529545
530- NetworkedObject netObject = SpawnManager . CreateLocalNetworkedObject ( false , 0 , ( prefabHash == null ? NetworkConfig . PlayerPrefabHash : prefabHash . Value ) , null , position , rotation ) ;
531- SpawnManager . SpawnNetworkedObjectLocally ( netObject , SpawnManager . GetNetworkObjectId ( ) , false , true , hostClientId , payloadStream , payloadStream != null , payloadStream == null ? 0 : ( int ) payloadStream . Length , false , false ) ;
532-
533- if ( netObject . CheckObjectVisibility == null || netObject . CheckObjectVisibility ( hostClientId ) )
546+ if ( ( createPlayerObject == null && NetworkConfig . CreatePlayerPrefab ) || ( createPlayerObject != null && createPlayerObject . Value ) )
534547 {
535- netObject . observers . Add ( hostClientId ) ;
548+ NetworkedObject netObject = SpawnManager . CreateLocalNetworkedObject ( false , 0 , ( prefabHash == null ? NetworkConfig . PlayerPrefabHash . Value : prefabHash . Value ) , null , position , rotation ) ;
549+ SpawnManager . SpawnNetworkedObjectLocally ( netObject , SpawnManager . GetNetworkObjectId ( ) , false , true , hostClientId , payloadStream , payloadStream != null , payloadStream == null ? 0 : ( int ) payloadStream . Length , false , false ) ;
550+
551+ if ( netObject . CheckObjectVisibility == null || netObject . CheckObjectVisibility ( hostClientId ) )
552+ {
553+ netObject . observers . Add ( hostClientId ) ;
554+ }
536555 }
537556
538557 SpawnManager . ServerSpawnSceneObjectsOnStartSweep ( ) ;
@@ -1051,9 +1070,9 @@ private void SyncTime()
10511070
10521071 private readonly List < NetworkedObject > _observedObjects = new List < NetworkedObject > ( ) ;
10531072
1054- internal void HandleApproval ( ulong clientId , ulong ? prefabHash , bool approved , Vector3 ? position , Quaternion ? rotation )
1073+ internal void HandleApproval ( ulong clientId , bool createPlayerObject , ulong ? playerPrefabHash , bool approved , Vector3 ? position , Quaternion ? rotation )
10551074 {
1056- if ( approved )
1075+ if ( approved )
10571076 {
10581077 // Inform new client it got approved
10591078 byte [ ] aesKey = PendingClients . ContainsKey ( clientId ) ? PendingClients [ clientId ] . AesKey : null ;
@@ -1072,10 +1091,14 @@ internal void HandleApproval(ulong clientId, ulong? prefabHash, bool approved, V
10721091 // This packet is unreliable, but if it gets through it should provide a much better sync than the potentially huge approval message.
10731092 SyncTime ( ) ;
10741093
1075- NetworkedObject netObject = SpawnManager . CreateLocalNetworkedObject ( false , 0 , ( prefabHash == null ? NetworkConfig . PlayerPrefabHash : prefabHash . Value ) , null , position , rotation ) ;
1076- SpawnManager . SpawnNetworkedObjectLocally ( netObject , SpawnManager . GetNetworkObjectId ( ) , false , true , clientId , null , false , 0 , false , false ) ;
10771094
1078- ConnectedClients [ clientId ] . PlayerObject = netObject ;
1095+ if ( createPlayerObject )
1096+ {
1097+ NetworkedObject netObject = SpawnManager . CreateLocalNetworkedObject ( false , 0 , ( playerPrefabHash == null ? NetworkConfig . PlayerPrefabHash . Value : playerPrefabHash . Value ) , null , position , rotation ) ;
1098+ SpawnManager . SpawnNetworkedObjectLocally ( netObject , SpawnManager . GetNetworkObjectId ( ) , false , true , clientId , null , false , 0 , false , false ) ;
1099+
1100+ ConnectedClients [ clientId ] . PlayerObject = netObject ;
1101+ }
10791102
10801103 _observedObjects . Clear ( ) ;
10811104
@@ -1178,29 +1201,29 @@ internal void HandleApproval(ulong clientId, ulong? prefabHash, bool approved, V
11781201
11791202 foreach ( KeyValuePair < ulong , NetworkedClient > clientPair in ConnectedClients )
11801203 {
1181- if ( clientPair . Key == clientId || ! ConnectedClients [ clientId ] . PlayerObject . observers . Contains ( clientPair . Key ) )
1204+ if ( clientPair . Key == clientId || ConnectedClients [ clientId ] . PlayerObject == null || ! ConnectedClients [ clientId ] . PlayerObject . observers . Contains ( clientPair . Key ) )
11821205 continue ; //The new client.
11831206
11841207 using ( PooledBitStream stream = PooledBitStream . Get ( ) )
11851208 {
11861209 using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
11871210 {
11881211 writer . WriteBool ( true ) ;
1189- writer . WriteUInt64Packed ( ConnectedClients [ clientId ] . PlayerObject . GetComponent < NetworkedObject > ( ) . NetworkId ) ;
1212+ writer . WriteUInt64Packed ( ConnectedClients [ clientId ] . PlayerObject . NetworkId ) ;
11901213 writer . WriteUInt64Packed ( clientId ) ;
11911214
1192- //Does not have a parrent
1215+ //Does not have a parent
11931216 writer . WriteBool ( false ) ;
11941217
11951218 if ( NetworkConfig . UsePrefabSync )
11961219 {
1197- writer . WriteUInt64Packed ( prefabHash == null ? NetworkConfig . PlayerPrefabHash : prefabHash . Value ) ;
1220+ writer . WriteUInt64Packed ( playerPrefabHash == null ? NetworkConfig . PlayerPrefabHash . Value : playerPrefabHash . Value ) ;
11981221 }
11991222 else
12001223 {
12011224 // Not a softmap aka scene object
12021225 writer . WriteBool ( false ) ;
1203- writer . WriteUInt64Packed ( prefabHash == null ? NetworkConfig . PlayerPrefabHash : prefabHash . Value ) ;
1226+ writer . WriteUInt64Packed ( playerPrefabHash == null ? NetworkConfig . PlayerPrefabHash . Value : playerPrefabHash . Value ) ;
12041227 }
12051228
12061229 if ( ConnectedClients [ clientId ] . PlayerObject . IncludeTransformWhenSpawning == null || ConnectedClients [ clientId ] . PlayerObject . IncludeTransformWhenSpawning ( clientId ) )
0 commit comments