Skip to content

Commit f9041ee

Browse files
committed
Added per prefabId to PlayerPrefabs
1 parent e604f9a commit f9041ee

File tree

4 files changed

+33
-79
lines changed

4 files changed

+33
-79
lines changed

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private void OnDestroy()
224224
public void Spawn()
225225
{
226226
if (NetworkingManager.singleton != null)
227-
SpawnManager.SpawnPrefabIndexServer(this);
227+
SpawnManager.SpawnObject(this);
228228
}
229229
/// <summary>
230230
/// Spawns an object across the network with a given owner. Can only be called from server
@@ -233,7 +233,7 @@ public void Spawn()
233233
public void SpawnWithOwnership(uint clientId)
234234
{
235235
if (NetworkingManager.singleton != null)
236-
SpawnManager.SpawnPrefabIndexServer(this, clientId);
236+
SpawnManager.SpawnObject(this, clientId);
237237
}
238238
/// <summary>
239239
/// Removes all ownership of an object from any client. Can only be called from server

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,11 @@ public bool IsClientConnected
148148
/// The callback to invoke once the server is ready
149149
/// </summary>
150150
public Action OnServerStarted = null;
151+
public delegate void ConnectionApprovedDelegate(uint clientId, int prefabId, bool approved, Vector3 position, Quaternion rotation);
151152
/// <summary>
152153
/// The callback to invoke during connection approval
153154
/// </summary>
154-
public Action<byte[], uint, Action<uint, bool, Vector3, Quaternion>> ConnectionApprovalCallback = null;
155+
public Action<byte[], uint, ConnectionApprovedDelegate> ConnectionApprovalCallback = null;
155156
/// <summary>
156157
/// The current NetworkingConfiguration
157158
/// </summary>
@@ -599,7 +600,7 @@ public void StopClient()
599600
/// <summary>
600601
/// Starts a Host
601602
/// </summary>
602-
public void StartHost(Vector3? pos = null, Quaternion? rot = null)
603+
public void StartHost(Vector3? pos = null, Quaternion? rot = null, int prefabId = -1)
603604
{
604605
if (LogHelper.CurrentLogLevel <= LogLevel.Developer) LogHelper.LogInfo("StartHost()");
605606
if (isServer || isClient)
@@ -631,7 +632,8 @@ public void StartHost(Vector3? pos = null, Quaternion? rot = null)
631632

632633
if (NetworkConfig.HandleObjectSpawning)
633634
{
634-
SpawnManager.SpawnPlayerObject(hostClientId, 0, pos.GetValueOrDefault(), rot.GetValueOrDefault());
635+
prefabId = prefabId == -1 ? NetworkConfig.NetworkPrefabIds[NetworkConfig.PlayerPrefabName] : prefabId;
636+
SpawnManager.CreateSpawnedObject(prefabId, 0, hostClientId, true, pos.GetValueOrDefault(), rot.GetValueOrDefault(), null);
635637
}
636638

637639
if (OnServerStarted != null)
@@ -1111,7 +1113,7 @@ private void SyncTime()
11111113
}
11121114
}
11131115

1114-
internal void HandleApproval(uint clientId, bool approved, Vector3 position, Quaternion rotation)
1116+
internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3 position, Quaternion rotation)
11151117
{
11161118
if(approved)
11171119
{
@@ -1158,7 +1160,8 @@ internal void HandleApproval(uint clientId, bool approved, Vector3 position, Qua
11581160
if(NetworkConfig.HandleObjectSpawning)
11591161
{
11601162
uint networkId = SpawnManager.GetNetworkObjectId();
1161-
GameObject go = SpawnManager.SpawnPlayerObject(clientId, networkId, position, rotation);
1163+
prefabId = prefabId == -1 ? NetworkConfig.NetworkPrefabIds[NetworkConfig.PlayerPrefabName] : prefabId;
1164+
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, clientId, true, position, rotation);
11621165
netObject = go.GetComponent<NetworkedObject>();
11631166
connectedClients[clientId].PlayerObject = go;
11641167
}
@@ -1243,7 +1246,7 @@ internal void HandleApproval(uint clientId, bool approved, Vector3 position, Qua
12431246
writer.WriteBool(true);
12441247
writer.WriteUInt(connectedClients[clientId].PlayerObject.GetComponent<NetworkedObject>().NetworkId);
12451248
writer.WriteUInt(clientId);
1246-
writer.WriteInt(-1);
1249+
writer.WriteInt(prefabId);
12471250
writer.WriteBool(false);
12481251
writer.WriteBool(connectedClients[clientId].PlayerObject.GetComponent<NetworkedObject>().observers.Contains(clientPair.Key));
12491252

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal static void HandleConnectionRequest(uint clientId, BitReader reader, in
3434
}
3535
else
3636
{
37-
netManager.HandleApproval(clientId, true, Vector3.zero, Quaternion.identity);
37+
netManager.HandleApproval(clientId, -1, true, Vector3.zero, Quaternion.identity);
3838
}
3939
}
4040

@@ -105,20 +105,11 @@ internal static void HandleConnectionApproved(uint clientId, BitReader reader, i
105105
float yRot = reader.ReadFloat();
106106
float zRot = reader.ReadFloat();
107107

108-
if (isPlayerObject)
109-
{
110-
GameObject go = SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
111-
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
112-
}
113-
else
114-
{
115-
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
116-
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
117-
118-
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
119-
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
120-
go.SetActive(isActive);
121-
}
108+
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject,
109+
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
110+
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
111+
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
112+
go.SetActive(isActive);
122113
}
123114
}
124115

@@ -155,17 +146,13 @@ internal static void HandleAddObject(uint clientId, BitReader reader, int channe
155146
{
156147
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
157148
netManager.connectedClientsList.Add(netManager.connectedClients[ownerId]);
158-
GameObject go = SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
159-
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
160149
}
161-
else
162-
{
163-
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
164-
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
150+
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject,
151+
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
152+
153+
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
154+
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
165155

166-
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
167-
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
168-
}
169156
}
170157
else
171158
{
@@ -283,18 +270,11 @@ internal static void HandleAddObjects(uint clientId, BitReader reader, int chann
283270
{
284271
netManager.connectedClients.Add(ownerId, new NetworkedClient() { ClientId = ownerId });
285272
netManager.connectedClientsList.Add(netManager.connectedClients[ownerId]);
286-
GameObject go = SpawnManager.SpawnPlayerObject(ownerId, networkId, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
287-
288-
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
289273
}
290-
else
291-
{
292-
GameObject go = SpawnManager.SpawnPrefabIndexClient(prefabId, networkId, ownerId,
293-
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
274+
GameObject go = SpawnManager.CreateSpawnedObject(prefabId, networkId, ownerId, isPlayerObject, new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot), visible ? reader : null);
275+
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
276+
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
294277

295-
go.GetComponent<NetworkedObject>().SetLocalVisibility(visible);
296-
go.GetComponent<NetworkedObject>().sceneObject = sceneObject;
297-
}
298278
}
299279
}
300280
}

MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ internal static void FlushSceneObjects()
152152
}
153153
}
154154

155-
internal static GameObject SpawnPrefabIndexClient(int networkedPrefabId, uint networkId, uint owner, Vector3 position, Quaternion rotation, BitReader reader = null)
155+
internal static GameObject CreateSpawnedObject(int networkedPrefabId, uint networkId, uint owner, bool playerObject, Vector3 position, Quaternion rotation, BitReader reader = null)
156156
{
157157
if (!netManager.NetworkConfig.NetworkPrefabNames.ContainsKey(networkedPrefabId))
158158
{
@@ -174,16 +174,21 @@ internal static GameObject SpawnPrefabIndexClient(int networkedPrefabId, uint ne
174174
netObject.NetworkedPrefabName = netManager.NetworkConfig.NetworkPrefabNames[networkedPrefabId];
175175
netObject._isSpawned = true;
176176
netObject._isPooledObject = false;
177-
netObject.networkId = networkId;
177+
178+
if (netManager.isServer) netObject.networkId = GetNetworkObjectId();
179+
else netObject.networkId = networkId;
180+
181+
netObject.sceneObject = false;
178182
netObject.ownerClientId = owner;
183+
netObject._isPlayerObject = playerObject;
179184
netObject.transform.position = position;
180185
netObject.transform.rotation = rotation;
181186
spawnedObjects.Add(netObject.NetworkId, netObject);
182187
netObject.InvokeBehaviourNetworkSpawn();
183188
return go;
184189
}
185190

186-
internal static void SpawnPrefabIndexServer(NetworkedObject netObject, uint? clientOwnerId = null)
191+
internal static void SpawnObject(NetworkedObject netObject, uint? clientOwnerId = null)
187192
{
188193
if (netObject.isSpawned)
189194
{
@@ -243,40 +248,6 @@ internal static void SpawnPrefabIndexServer(NetworkedObject netObject, uint? cli
243248
}
244249
}
245250

246-
internal static GameObject SpawnPlayerObject(uint clientId, uint networkId, Vector3 position, Quaternion rotation, BitReader reader = null)
247-
{
248-
if (string.IsNullOrEmpty(netManager.NetworkConfig.PlayerPrefabName) || !netManager.NetworkConfig.NetworkPrefabIds.ContainsKey(netManager.NetworkConfig.PlayerPrefabName))
249-
{
250-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("There is no player prefab in the NetworkConfig, or it's not registered at as a spawnable prefab");
251-
return null;
252-
}
253-
GameObject go = MonoBehaviour.Instantiate(netManager.NetworkConfig.NetworkedPrefabs[netManager.NetworkConfig.NetworkPrefabIds[netManager.NetworkConfig.PlayerPrefabName]].prefab, position, rotation);
254-
NetworkedObject netObject = go.GetComponent<NetworkedObject>();
255-
if (netObject == null)
256-
{
257-
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Please add a NetworkedObject component to the root of the player prefab");
258-
netObject = go.AddComponent<NetworkedObject>();
259-
}
260-
261-
if (NetworkingManager.singleton.isServer)
262-
netObject.networkId = GetNetworkObjectId();
263-
else
264-
netObject.networkId = networkId;
265-
266-
if (reader != null)
267-
netObject.SetFormattedSyncedVarData(reader);
268-
269-
netObject._isPooledObject = false;
270-
netObject.ownerClientId = clientId;
271-
netObject._isPlayerObject = true;
272-
netObject._isSpawned = true;
273-
netObject.sceneObject = false;
274-
netManager.connectedClients[clientId].PlayerObject = go;
275-
spawnedObjects.Add(netObject.NetworkId, netObject);
276-
netObject.InvokeBehaviourNetworkSpawn();
277-
return go;
278-
}
279-
280251
internal static void OnDestroyObject(uint networkId, bool destroyGameObject)
281252
{
282253
if (!spawnedObjects.ContainsKey(networkId) || (netManager != null && !netManager.NetworkConfig.HandleObjectSpawning))

0 commit comments

Comments
 (0)