Skip to content

Commit 4071ebd

Browse files
committed
Added object pooling
1 parent b639927 commit 4071ebd

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

MLAPI/Data/NetworkPool.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using UnityEngine;
2+
3+
namespace MLAPI.Data
4+
{
5+
internal class NetworkPool
6+
{
7+
internal GameObject prefab;
8+
internal GameObject[] objects;
9+
internal string poolName;
10+
11+
internal NetworkPool(GameObject prefab, uint size, string name)
12+
{
13+
objects = new GameObject[size];
14+
poolName = name;
15+
16+
for (int i = 0; i < size; i++)
17+
{
18+
GameObject go = UnityEngine.Object.Instantiate(prefab, Vector3.zero, Quaternion.identity);
19+
go.name = "Pool " + poolName + " #" + i;
20+
go.SetActive(false);
21+
}
22+
}
23+
24+
internal GameObject SpawnObject(Vector3 position, Quaternion rotation)
25+
{
26+
for (int i = 0; i < objects.Length; i++)
27+
{
28+
if (objects[i].activeInHierarchy)
29+
{
30+
GameObject go = objects[i];
31+
go.transform.position = position;
32+
go.transform.rotation = rotation;
33+
go.SetActive(true);
34+
}
35+
}
36+
Debug.LogWarning("MLAPI: The pool " + poolName + " has ran out of space");
37+
return null;
38+
}
39+
}
40+
}

MLAPI/MLAPI.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
</Reference>
5656
</ItemGroup>
5757
<ItemGroup>
58+
<Compile Include="Data\NetworkPool.cs" />
5859
<Compile Include="Data\TrackedPointData.cs" />
5960
<Compile Include="NetworkingManagerComponents\LagCompensationManager.cs" />
6061
<Compile Include="MonoBehaviours\Core\NetworkedBehaviour.cs" />
@@ -65,6 +66,7 @@
6566
<Compile Include="MonoBehaviours\Core\TrackedObject.cs" />
6667
<Compile Include="MonoBehaviours\Prototyping\NetworkedTransform.cs" />
6768
<Compile Include="NetworkingManagerComponents\MessageManager.cs" />
69+
<Compile Include="NetworkingManagerComponents\NetworkPoolManager.cs" />
6870
<Compile Include="NetworkingManagerComponents\NetworkSceneManager.cs" />
6971
<Compile Include="NetworkingManagerComponents\SpawnManager.cs" />
7072
<Compile Include="Properties\AssemblyInfo.cs" />

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
7272
MessageManager.reverseMessageTypes = new Dictionary<ushort, string>();
7373
SpawnManager.spawnedObjects = new Dictionary<uint, NetworkedObject>();
7474
SpawnManager.releasedNetworkObjectIds = new Stack<uint>();
75+
NetworkPoolManager.Pools = new Dictionary<string, Data.NetworkPool>();
7576
NetworkSceneManager.registeredSceneNames = new HashSet<string>();
7677
NetworkSceneManager.sceneIndexToString = new Dictionary<uint, string>();
7778
NetworkSceneManager.sceneNameToIndex = new Dictionary<string, uint>();
@@ -98,6 +99,7 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
9899
MessageManager.messageTypes.Add("MLAPI_CLIENT_DISCONNECT", 3);
99100
MessageManager.messageTypes.Add("MLAPI_DESTROY_OBJECT", 4);
100101
MessageManager.messageTypes.Add("MLAPI_SWITCH_SCENE", 5);
102+
MessageManager.messageTypes.Add("MLAPI_SPAWN_POOL_OBJECT", 6);
101103
NetworkConfig.MessageTypes.Add("MLAPI_OnRecieveTransformFromClient");
102104
NetworkConfig.MessageTypes.Add("MLAPI_OnRecieveTransformFromServer");
103105

@@ -578,6 +580,39 @@ private void HandleIncomingData(int clientId, byte[] data, int channelId)
578580
}
579581
}
580582
break;
583+
case 6: //Spawn pool object
584+
if(isClient)
585+
{
586+
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
587+
{
588+
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
589+
{
590+
ushort poolIndex = messageReader.ReadUInt16();
591+
float xPos = messageReader.ReadSingle();
592+
float yPos = messageReader.ReadSingle();
593+
float zPos = messageReader.ReadSingle();
594+
float xRot = messageReader.ReadSingle();
595+
float yRot = messageReader.ReadSingle();
596+
float zRot = messageReader.ReadSingle();
597+
NetworkPoolManager.SpawnPoolObject(NetworkPoolManager.PoolIndexToPoolName[poolIndex],
598+
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
599+
}
600+
}
601+
}
602+
break;
603+
case 7: //Destroy pool object
604+
if(isClient)
605+
{
606+
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
607+
{
608+
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
609+
{
610+
uint netId = messageReader.ReadUInt32();
611+
SpawnManager.spawnedObjects[netId].gameObject.SetActive(false);
612+
}
613+
}
614+
}
615+
break;
581616
}
582617
}
583618
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using MLAPI.Data;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using UnityEngine;
5+
6+
namespace MLAPI.NetworkingManagerComponents
7+
{
8+
public static class NetworkPoolManager
9+
{
10+
internal static Dictionary<string, NetworkPool> Pools;
11+
//We want to keep the pool indexes incrementing, this is to prevent new pools getting old names and the wrong objects being spawned.
12+
private static ushort PoolIndex = 0;
13+
14+
internal static Dictionary<ushort, string> PoolIndexToPoolName = new Dictionary<ushort, string>();
15+
internal static Dictionary<string, ushort> PoolNamesToIndexes = new Dictionary<string, ushort>();
16+
17+
public static void CreatePool(string poolName, GameObject poolPrefab, uint size = 16)
18+
{
19+
if(Pools.ContainsKey(poolName))
20+
{
21+
Debug.LogWarning("MLAPI: A pool with the name " + poolName + " already exists");
22+
return;
23+
}
24+
else if(poolPrefab == null)
25+
{
26+
Debug.LogWarning("MLAPI: A pool prefab is required");
27+
}
28+
PoolIndexToPoolName.Add(PoolIndex, poolName);
29+
PoolNamesToIndexes.Add(poolName, PoolIndex);
30+
PoolIndex++;
31+
Pools.Add(poolName, new NetworkPool(poolPrefab, size, poolName));
32+
}
33+
34+
public static GameObject SpawnPoolObject(string poolName, Vector3 position, Quaternion rotation)
35+
{
36+
if(NetworkingManager.singleton.isServer)
37+
{
38+
using(MemoryStream stream = new MemoryStream(26))
39+
{
40+
using(BinaryWriter writer = new BinaryWriter(stream))
41+
{
42+
writer.Write(PoolNamesToIndexes[poolName]);
43+
writer.Write(position.x);
44+
writer.Write(position.y);
45+
writer.Write(position.z);
46+
writer.Write(rotation.eulerAngles.x);
47+
writer.Write(rotation.eulerAngles.y);
48+
writer.Write(rotation.eulerAngles.z);
49+
}
50+
NetworkingManager.singleton.Send("MLAPI_SPAWN_POOL_OBJECT", "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED", stream.GetBuffer());
51+
}
52+
}
53+
return Pools[poolName].SpawnObject(position, rotation);
54+
}
55+
56+
public static void DestroyPoolObject(GameObject gameObject)
57+
{
58+
gameObject.SetActive(false);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)