Skip to content

Commit d7b2dc4

Browse files
authored
Merge pull request #169 from MidLevel/custom-spawn-handlers
Added custom spawn and destroy handlers and removed built in pooling support
2 parents 9043316 + 1fa9614 commit d7b2dc4

File tree

11 files changed

+200
-263
lines changed

11 files changed

+200
-263
lines changed

MLAPI-Editor/NetworkedObjectEditor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public override void OnInspectorGUI()
4747
EditorGUILayout.LabelField("IsLocalPlayer: ", networkedObject.IsLocalPlayer.ToString(), EditorStyles.label);
4848
EditorGUILayout.LabelField("IsOwner: ", networkedObject.IsOwner.ToString(), EditorStyles.label);
4949
EditorGUILayout.LabelField("IsOwnedByServer: ", networkedObject.IsOwnedByServer.ToString(), EditorStyles.label);
50-
EditorGUILayout.LabelField("IsPoolObject: ", networkedObject.IsPooledObject.ToString(), EditorStyles.label);
5150
EditorGUILayout.LabelField("IsPlayerObject: ", networkedObject.IsPlayerObject.ToString(), EditorStyles.label);
5251

5352
if (NetworkingManager.Singleton != null && NetworkingManager.Singleton.IsServer)

MLAPI/Data/MLAPIConstants.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,18 @@ public static class MLAPIConstants
1717
public const byte MLAPI_DESTROY_OBJECT = 6;
1818
public const byte MLAPI_SWITCH_SCENE = 7;
1919
public const byte MLAPI_CLIENT_SWITCH_SCENE_COMPLETED = 8;
20-
public const byte MLAPI_SPAWN_POOL_OBJECT = 9;
21-
public const byte MLAPI_DESTROY_POOL_OBJECT = 10;
22-
public const byte MLAPI_CHANGE_OWNER = 11;
23-
public const byte MLAPI_ADD_OBJECTS = 12;
24-
public const byte MLAPI_TIME_SYNC = 13;
25-
public const byte MLAPI_NETWORKED_VAR_DELTA = 14;
26-
public const byte MLAPI_NETWORKED_VAR_UPDATE = 15;
27-
public const byte MLAPI_SERVER_RPC = 16;
28-
public const byte MLAPI_SERVER_RPC_REQUEST = 17;
29-
public const byte MLAPI_SERVER_RPC_RESPONSE = 18;
30-
public const byte MLAPI_CLIENT_RPC = 19;
31-
public const byte MLAPI_CLIENT_RPC_REQUEST = 20;
32-
public const byte MLAPI_CLIENT_RPC_RESPONSE = 21;
33-
public const byte MLAPI_CUSTOM_MESSAGE = 22;
20+
public const byte MLAPI_CHANGE_OWNER = 9;
21+
public const byte MLAPI_ADD_OBJECTS = 10;
22+
public const byte MLAPI_TIME_SYNC = 11;
23+
public const byte MLAPI_NETWORKED_VAR_DELTA = 12;
24+
public const byte MLAPI_NETWORKED_VAR_UPDATE = 13;
25+
public const byte MLAPI_SERVER_RPC = 14;
26+
public const byte MLAPI_SERVER_RPC_REQUEST = 15;
27+
public const byte MLAPI_SERVER_RPC_RESPONSE = 16;
28+
public const byte MLAPI_CLIENT_RPC = 17;
29+
public const byte MLAPI_CLIENT_RPC_REQUEST = 18;
30+
public const byte MLAPI_CLIENT_RPC_RESPONSE = 19;
31+
public const byte MLAPI_CUSTOM_MESSAGE = 20;
3432
public const byte INVALID = 32;
3533

3634
public static readonly string[] MESSAGE_NAMES = {
@@ -43,16 +41,14 @@ public static class MLAPIConstants
4341
"MLAPI_DESTROY_OBJECT",
4442
"MLAPI_SWITCH_SCENE",
4543
"MLAPI_CLIENT_SWITCH_SCENE_COMPLETED",
46-
"MLAPI_SPAWN_POOL_OBJECT",
47-
"MLAPI_DESTROY_POOL_OBJECT",
4844
"MLAPI_CHANGE_OWNER",
4945
"MLAPI_ADD_OBJECTS",
5046
"MLAPI_TIME_SYNC",
5147
"MLAPI_NETWORKED_VAR_DELTA",
5248
"MLAPI_NETWORKED_VAR_UPDATE",
53-
"MLAPI_SERVER_RPC", // 16
49+
"MLAPI_SERVER_RPC",
5450
"MLAPI_SERVER_RPC_REQUEST",
55-
"MLAPI_SERVER_RPC_RESPONSE",
51+
"MLAPI_SERVER_RPC_RESPONSE", // 16
5652
"MLAPI_CLIENT_RPC",
5753
"MLAPI_CLIENT_RPC_REQUEST",
5854
"MLAPI_CLIENT_RPC_RESPONSE",
@@ -66,6 +62,8 @@ public static class MLAPIConstants
6662
"",
6763
"",
6864
"",
65+
"",
66+
"",
6967
"INVALID" // 32
7068
};
7169
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member

MLAPI/Data/NetworkPool.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.

MLAPI/MLAPI.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
<Compile Include="Data\Transports\NetEventType.cs" />
9999
<Compile Include="Data\NetworkConfig.cs" />
100100
<Compile Include="Data\NetworkedPrefab.cs" />
101-
<Compile Include="Data\NetworkPool.cs" />
102101
<Compile Include="Data\TrackedPointData.cs" />
103102
<Compile Include="Data\Transports\UNET\RelayedTransport.cs" />
104103
<Compile Include="Data\Transports\UNET\RelayTransport.cs" />
@@ -135,7 +134,6 @@
135134
<Compile Include="MonoBehaviours\Core\TrackedObject.cs" />
136135
<Compile Include="MonoBehaviours\Prototyping\NetworkedTransform.cs" />
137136
<Compile Include="NetworkingManagerComponents\Core\MessageManager.cs" />
138-
<Compile Include="NetworkingManagerComponents\Core\NetworkPoolManager.cs" />
139137
<Compile Include="NetworkingManagerComponents\Core\NetworkSceneManager.cs" />
140138
<Compile Include="NetworkingManagerComponents\Core\SpawnManager.cs" />
141139
<Compile Include="Properties\AssemblyInfo.cs" />

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@ internal set
6767
/// Gets if this object is a player object
6868
/// </summary>
6969
public bool IsPlayerObject { get; internal set; }
70-
[Obsolete("Use IsPooledObject instead", false)]
71-
public bool isPooledObject => IsPooledObject;
72-
/// <summary>
73-
/// Gets if this object is part of a pool
74-
/// </summary>
75-
public bool IsPooledObject { get; internal set; }
76-
/// <summary>
77-
/// Gets the poolId this object is part of
78-
/// </summary>
79-
public ushort PoolId { get; internal set; }
8070
[Obsolete("Use IsLocalPlayer instead", false)]
8171
public bool isLocalPlayer => IsLocalPlayer;
8272
/// <summary>

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ private object Init(bool server)
269269
SpawnManager.SpawnedObjectsList.Clear();
270270
SpawnManager.releasedNetworkObjectIds.Clear();
271271
SpawnManager.PendingSpawnObjects.Clear();
272-
NetworkPoolManager.Pools.Clear();
273-
NetworkPoolManager.PoolNamesToIndexes.Clear();
272+
SpawnManager.customSpawnHandlers.Clear();
273+
SpawnManager.customDestroyHandlers.Clear();
274274
NetworkSceneManager.registeredSceneNames.Clear();
275275
NetworkSceneManager.sceneIndexToString.Clear();
276276
NetworkSceneManager.sceneNameToIndex.Clear();
@@ -879,13 +879,6 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
879879
case MLAPIConstants.MLAPI_SWITCH_SCENE:
880880
if (IsClient) InternalMessageHandler.HandleSwitchScene(clientId, messageStream, channelId);
881881
break;
882-
case MLAPIConstants.MLAPI_SPAWN_POOL_OBJECT:
883-
if (IsClient) InternalMessageHandler.HandleSpawnPoolObject(clientId, messageStream, channelId);
884-
break;
885-
case MLAPIConstants.MLAPI_DESTROY_POOL_OBJECT:
886-
if (IsClient)
887-
InternalMessageHandler.HandleDestroyPoolObject(clientId, messageStream, channelId);
888-
break;
889882
case MLAPIConstants.MLAPI_CHANGE_OWNER:
890883
if (IsClient) InternalMessageHandler.HandleChangeOwner(clientId, messageStream, channelId);
891884
break;
@@ -981,15 +974,33 @@ internal void OnClientDisconnectFromServer(uint clientId)
981974
if (IsServer)
982975
{
983976
if (ConnectedClients[clientId].PlayerObject != null)
984-
Destroy(ConnectedClients[clientId].PlayerObject.gameObject);
977+
{
978+
if (SpawnManager.customDestroyHandlers.ContainsKey(ConnectedClients[clientId].PlayerObject.NetworkedPrefabHash))
979+
{
980+
SpawnManager.customDestroyHandlers[ConnectedClients[clientId].PlayerObject.NetworkedPrefabHash](ConnectedClients[clientId].PlayerObject);
981+
SpawnManager.OnDestroyObject(ConnectedClients[clientId].PlayerObject.NetworkId, false);
982+
}
983+
else
984+
{
985+
Destroy(ConnectedClients[clientId].PlayerObject.gameObject);
986+
}
987+
}
985988

986989
for (int i = 0; i < ConnectedClients[clientId].OwnedObjects.Count; i++)
987990
{
988991
if (ConnectedClients[clientId].OwnedObjects[i] != null)
989992
{
990993
if (!ConnectedClients[clientId].OwnedObjects[i].DontDestroyWithOwner)
991994
{
992-
Destroy(ConnectedClients[clientId].OwnedObjects[i].gameObject);
995+
if (SpawnManager.customDestroyHandlers.ContainsKey(ConnectedClients[clientId].OwnedObjects[i].NetworkedPrefabHash))
996+
{
997+
SpawnManager.customDestroyHandlers[ConnectedClients[clientId].OwnedObjects[i].NetworkedPrefabHash](ConnectedClients[clientId].OwnedObjects[i]);
998+
SpawnManager.OnDestroyObject(ConnectedClients[clientId].OwnedObjects[i].NetworkId, false);
999+
}
1000+
else
1001+
{
1002+
Destroy(ConnectedClients[clientId].OwnedObjects[i].gameObject);
1003+
}
9931004
}
9941005
else
9951006
{

MLAPI/NetworkingManagerComponents/Core/InternalMessageHandler.Receive.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -305,35 +305,6 @@ internal static void HandleClientSwitchSceneCompleted(uint clientId, Stream stre
305305
}
306306
}
307307

308-
internal static void HandleSpawnPoolObject(uint clientId, Stream stream, int channelId)
309-
{
310-
using (PooledBitReader reader = PooledBitReader.Get(stream))
311-
{
312-
uint netId = reader.ReadUInt32Packed();
313-
314-
float xPos = reader.ReadSinglePacked();
315-
float yPos = reader.ReadSinglePacked();
316-
float zPos = reader.ReadSinglePacked();
317-
318-
float xRot = reader.ReadSinglePacked();
319-
float yRot = reader.ReadSinglePacked();
320-
float zRot = reader.ReadSinglePacked();
321-
322-
SpawnManager.SpawnedObjects[netId].transform.position = new Vector3(xPos, yPos, zPos);
323-
SpawnManager.SpawnedObjects[netId].transform.rotation = Quaternion.Euler(xRot, yRot, zRot);
324-
SpawnManager.SpawnedObjects[netId].gameObject.SetActive(true);
325-
}
326-
}
327-
328-
internal static void HandleDestroyPoolObject(uint clientId, Stream stream, int channelId)
329-
{
330-
using (PooledBitReader reader = PooledBitReader.Get(stream))
331-
{
332-
uint netId = reader.ReadUInt32Packed();
333-
SpawnManager.SpawnedObjects[netId].gameObject.SetActive(false);
334-
}
335-
}
336-
337308
internal static void HandleChangeOwner(uint clientId, Stream stream, int channelId)
338309
{
339310
using (PooledBitReader reader = PooledBitReader.Get(stream))

MLAPI/NetworkingManagerComponents/Core/NetworkPoolManager.cs

Lines changed: 0 additions & 114 deletions
This file was deleted.

MLAPI/NetworkingManagerComponents/Core/NetworkSceneManager.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,17 @@ private static void OnSceneUnload(AsyncOperation operation, Guid switchSceneGuid
159159
for (int i = 0; i < netObjects.Length; i++)
160160
{
161161
if (netObjects[i].destroyWithScene == null)
162-
MonoBehaviour.Destroy(netObjects[i].gameObject);
162+
{
163+
if (SpawnManager.customDestroyHandlers.ContainsKey(netObjects[i].NetworkedPrefabHash))
164+
{
165+
SpawnManager.customDestroyHandlers[netObjects[i].NetworkedPrefabHash](netObjects[i]);
166+
SpawnManager.OnDestroyObject(netObjects[i].NetworkId, false);
167+
}
168+
else
169+
{
170+
MonoBehaviour.Destroy(netObjects[i].gameObject);
171+
}
172+
}
163173
}
164174
}
165175

0 commit comments

Comments
 (0)