@@ -162,12 +162,12 @@ internal set
162
162
/// <summary>
163
163
/// Delegate type called when connection has been approved
164
164
/// </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>
167
167
/// <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 ) ;
171
171
/// <summary>
172
172
/// The callback to invoke during connection approval
173
173
/// </summary>
@@ -284,15 +284,31 @@ private void OnValidate()
284
284
}
285
285
286
286
int playerPrefabCount = NetworkConfig . NetworkedPrefabs . Count ( x => x . PlayerPrefab == true ) ;
287
+
287
288
if ( playerPrefabCount == 0 )
289
+ {
290
+
291
+ }
292
+
293
+ if ( playerPrefabCount == 0 && ! NetworkConfig . ConnectionApproval && NetworkConfig . CreatePlayerPrefab )
288
294
{
289
295
if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "There is no NetworkedPrefab marked as a PlayerPrefab" ) ;
290
296
}
291
297
else if ( playerPrefabCount > 1 )
292
298
{
293
299
if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "Only one networked prefab can be marked as a player prefab" ) ;
294
300
}
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
+ }
296
312
}
297
313
298
314
private void Init ( bool server )
@@ -494,7 +510,7 @@ public void StopClient()
494
510
/// <summary>
495
511
/// Starts a Host
496
512
/// </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 )
498
514
{
499
515
if ( LogHelper . CurrentLogLevel <= LogLevel . Developer ) LogHelper . LogInfo ( "StartHost()" ) ;
500
516
if ( IsServer || IsClient )
@@ -527,12 +543,15 @@ public void StartHost(Vector3? position = null, Quaternion? rotation = null, ulo
527
543
528
544
ConnectedClientsList . Add ( ConnectedClients [ hostClientId ] ) ;
529
545
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 ) )
534
547
{
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
+ }
536
555
}
537
556
538
557
SpawnManager . ServerSpawnSceneObjectsOnStartSweep ( ) ;
@@ -1051,9 +1070,9 @@ private void SyncTime()
1051
1070
1052
1071
private readonly List < NetworkedObject > _observedObjects = new List < NetworkedObject > ( ) ;
1053
1072
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 )
1055
1074
{
1056
- if ( approved )
1075
+ if ( approved )
1057
1076
{
1058
1077
// Inform new client it got approved
1059
1078
byte [ ] aesKey = PendingClients . ContainsKey ( clientId ) ? PendingClients [ clientId ] . AesKey : null ;
@@ -1072,10 +1091,14 @@ internal void HandleApproval(ulong clientId, ulong? prefabHash, bool approved, V
1072
1091
// This packet is unreliable, but if it gets through it should provide a much better sync than the potentially huge approval message.
1073
1092
SyncTime ( ) ;
1074
1093
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 ) ;
1077
1094
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
+ }
1079
1102
1080
1103
_observedObjects . Clear ( ) ;
1081
1104
@@ -1178,29 +1201,29 @@ internal void HandleApproval(ulong clientId, ulong? prefabHash, bool approved, V
1178
1201
1179
1202
foreach ( KeyValuePair < ulong , NetworkedClient > clientPair in ConnectedClients )
1180
1203
{
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 ) )
1182
1205
continue ; //The new client.
1183
1206
1184
1207
using ( PooledBitStream stream = PooledBitStream . Get ( ) )
1185
1208
{
1186
1209
using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
1187
1210
{
1188
1211
writer . WriteBool ( true ) ;
1189
- writer . WriteUInt64Packed ( ConnectedClients [ clientId ] . PlayerObject . GetComponent < NetworkedObject > ( ) . NetworkId ) ;
1212
+ writer . WriteUInt64Packed ( ConnectedClients [ clientId ] . PlayerObject . NetworkId ) ;
1190
1213
writer . WriteUInt64Packed ( clientId ) ;
1191
1214
1192
- //Does not have a parrent
1215
+ //Does not have a parent
1193
1216
writer . WriteBool ( false ) ;
1194
1217
1195
1218
if ( NetworkConfig . UsePrefabSync )
1196
1219
{
1197
- writer . WriteUInt64Packed ( prefabHash == null ? NetworkConfig . PlayerPrefabHash : prefabHash . Value ) ;
1220
+ writer . WriteUInt64Packed ( playerPrefabHash == null ? NetworkConfig . PlayerPrefabHash . Value : playerPrefabHash . Value ) ;
1198
1221
}
1199
1222
else
1200
1223
{
1201
1224
// Not a softmap aka scene object
1202
1225
writer . WriteBool ( false ) ;
1203
- writer . WriteUInt64Packed ( prefabHash == null ? NetworkConfig . PlayerPrefabHash : prefabHash . Value ) ;
1226
+ writer . WriteUInt64Packed ( playerPrefabHash == null ? NetworkConfig . PlayerPrefabHash . Value : playerPrefabHash . Value ) ;
1204
1227
}
1205
1228
1206
1229
if ( ConnectedClients [ clientId ] . PlayerObject . IncludeTransformWhenSpawning == null || ConnectedClients [ clientId ] . PlayerObject . IncludeTransformWhenSpawning ( clientId ) )
0 commit comments