Added object pooling

This commit is contained in:
Albin Corén 2018-03-04 21:56:35 +01:00
parent b6399270e0
commit 4071ebda1b
4 changed files with 138 additions and 0 deletions

40
MLAPI/Data/NetworkPool.cs Normal file
View File

@ -0,0 +1,40 @@
using UnityEngine;
namespace MLAPI.Data
{
internal class NetworkPool
{
internal GameObject prefab;
internal GameObject[] objects;
internal string poolName;
internal NetworkPool(GameObject prefab, uint size, string name)
{
objects = new GameObject[size];
poolName = name;
for (int i = 0; i < size; i++)
{
GameObject go = UnityEngine.Object.Instantiate(prefab, Vector3.zero, Quaternion.identity);
go.name = "Pool " + poolName + " #" + i;
go.SetActive(false);
}
}
internal GameObject SpawnObject(Vector3 position, Quaternion rotation)
{
for (int i = 0; i < objects.Length; i++)
{
if (objects[i].activeInHierarchy)
{
GameObject go = objects[i];
go.transform.position = position;
go.transform.rotation = rotation;
go.SetActive(true);
}
}
Debug.LogWarning("MLAPI: The pool " + poolName + " has ran out of space");
return null;
}
}
}

View File

@ -55,6 +55,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Data\NetworkPool.cs" />
<Compile Include="Data\TrackedPointData.cs" />
<Compile Include="NetworkingManagerComponents\LagCompensationManager.cs" />
<Compile Include="MonoBehaviours\Core\NetworkedBehaviour.cs" />
@ -65,6 +66,7 @@
<Compile Include="MonoBehaviours\Core\TrackedObject.cs" />
<Compile Include="MonoBehaviours\Prototyping\NetworkedTransform.cs" />
<Compile Include="NetworkingManagerComponents\MessageManager.cs" />
<Compile Include="NetworkingManagerComponents\NetworkPoolManager.cs" />
<Compile Include="NetworkingManagerComponents\NetworkSceneManager.cs" />
<Compile Include="NetworkingManagerComponents\SpawnManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -72,6 +72,7 @@ namespace MLAPI
MessageManager.reverseMessageTypes = new Dictionary<ushort, string>();
SpawnManager.spawnedObjects = new Dictionary<uint, NetworkedObject>();
SpawnManager.releasedNetworkObjectIds = new Stack<uint>();
NetworkPoolManager.Pools = new Dictionary<string, Data.NetworkPool>();
NetworkSceneManager.registeredSceneNames = new HashSet<string>();
NetworkSceneManager.sceneIndexToString = new Dictionary<uint, string>();
NetworkSceneManager.sceneNameToIndex = new Dictionary<string, uint>();
@ -98,6 +99,7 @@ namespace MLAPI
MessageManager.messageTypes.Add("MLAPI_CLIENT_DISCONNECT", 3);
MessageManager.messageTypes.Add("MLAPI_DESTROY_OBJECT", 4);
MessageManager.messageTypes.Add("MLAPI_SWITCH_SCENE", 5);
MessageManager.messageTypes.Add("MLAPI_SPAWN_POOL_OBJECT", 6);
NetworkConfig.MessageTypes.Add("MLAPI_OnRecieveTransformFromClient");
NetworkConfig.MessageTypes.Add("MLAPI_OnRecieveTransformFromServer");
@ -578,6 +580,39 @@ namespace MLAPI
}
}
break;
case 6: //Spawn pool object
if(isClient)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
ushort poolIndex = messageReader.ReadUInt16();
float xPos = messageReader.ReadSingle();
float yPos = messageReader.ReadSingle();
float zPos = messageReader.ReadSingle();
float xRot = messageReader.ReadSingle();
float yRot = messageReader.ReadSingle();
float zRot = messageReader.ReadSingle();
NetworkPoolManager.SpawnPoolObject(NetworkPoolManager.PoolIndexToPoolName[poolIndex],
new Vector3(xPos, yPos, zPos), Quaternion.Euler(xRot, yRot, zRot));
}
}
}
break;
case 7: //Destroy pool object
if(isClient)
{
using (MemoryStream messageReadStream = new MemoryStream(incommingData))
{
using (BinaryReader messageReader = new BinaryReader(messageReadStream))
{
uint netId = messageReader.ReadUInt32();
SpawnManager.spawnedObjects[netId].gameObject.SetActive(false);
}
}
}
break;
}
}
}

View File

@ -0,0 +1,61 @@
using MLAPI.Data;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace MLAPI.NetworkingManagerComponents
{
public static class NetworkPoolManager
{
internal static Dictionary<string, NetworkPool> Pools;
//We want to keep the pool indexes incrementing, this is to prevent new pools getting old names and the wrong objects being spawned.
private static ushort PoolIndex = 0;
internal static Dictionary<ushort, string> PoolIndexToPoolName = new Dictionary<ushort, string>();
internal static Dictionary<string, ushort> PoolNamesToIndexes = new Dictionary<string, ushort>();
public static void CreatePool(string poolName, GameObject poolPrefab, uint size = 16)
{
if(Pools.ContainsKey(poolName))
{
Debug.LogWarning("MLAPI: A pool with the name " + poolName + " already exists");
return;
}
else if(poolPrefab == null)
{
Debug.LogWarning("MLAPI: A pool prefab is required");
}
PoolIndexToPoolName.Add(PoolIndex, poolName);
PoolNamesToIndexes.Add(poolName, PoolIndex);
PoolIndex++;
Pools.Add(poolName, new NetworkPool(poolPrefab, size, poolName));
}
public static GameObject SpawnPoolObject(string poolName, Vector3 position, Quaternion rotation)
{
if(NetworkingManager.singleton.isServer)
{
using(MemoryStream stream = new MemoryStream(26))
{
using(BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(PoolNamesToIndexes[poolName]);
writer.Write(position.x);
writer.Write(position.y);
writer.Write(position.z);
writer.Write(rotation.eulerAngles.x);
writer.Write(rotation.eulerAngles.y);
writer.Write(rotation.eulerAngles.z);
}
NetworkingManager.singleton.Send("MLAPI_SPAWN_POOL_OBJECT", "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED", stream.GetBuffer());
}
}
return Pools[poolName].SpawnObject(position, rotation);
}
public static void DestroyPoolObject(GameObject gameObject)
{
gameObject.SetActive(false);
}
}
}