From 77c6a859884bdefb3af476d0f0e75bf94b8e3ae8 Mon Sep 17 00:00:00 2001 From: "DESKTOP-4BMTUHM\\Jim" Date: Sun, 4 Mar 2018 15:23:12 -0700 Subject: [PATCH] Allow pasing array of objects to CreatePool, such as from a sceneobject. This is to allow the use of scene objects containing such an array of prefabs, which is supposed to be quicker to create than calling instantiate. --- MLAPI/Data/NetworkPool.cs | 14 ++++++++++++++ .../NetworkPoolManager.cs | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/MLAPI/Data/NetworkPool.cs b/MLAPI/Data/NetworkPool.cs index 704a95f03e..885c8f8a1e 100644 --- a/MLAPI/Data/NetworkPool.cs +++ b/MLAPI/Data/NetworkPool.cs @@ -21,6 +21,20 @@ internal NetworkPool(GameObject prefab, uint size, string name) } } + internal NetworkPool(GameObject[] prefabs, string name) + { + objects = prefabs; + poolName = name; + int size = prefabs.Length; + + for (int i = 0; i < size; i++) + { + prefabs[i].name = "Pool " + poolName + " #" + i; + prefabs[i].SetActive(false); + } + + } + internal GameObject SpawnObject(Vector3 position, Quaternion rotation) { for (int i = 0; i < objects.Length; i++) diff --git a/MLAPI/NetworkingManagerComponents/NetworkPoolManager.cs b/MLAPI/NetworkingManagerComponents/NetworkPoolManager.cs index 0ed226b478..aed8332ddd 100644 --- a/MLAPI/NetworkingManagerComponents/NetworkPoolManager.cs +++ b/MLAPI/NetworkingManagerComponents/NetworkPoolManager.cs @@ -31,6 +31,24 @@ public static void CreatePool(string poolName, GameObject poolPrefab, uint size Pools.Add(poolName, new NetworkPool(poolPrefab, size, poolName)); } + + public static void CreatePool(string poolName, GameObject[] poolPrefabs) + { + if (Pools.ContainsKey(poolName)) + { + Debug.LogWarning("MLAPI: A pool with the name " + poolName + " already exists"); + return; + } + else if (poolPrefabs == null) + { + Debug.LogWarning("MLAPI: A pool prefab array is required"); + } + PoolIndexToPoolName.Add(PoolIndex, poolName); + PoolNamesToIndexes.Add(poolName, PoolIndex); + PoolIndex++; + Pools.Add(poolName, new NetworkPool(poolPrefabs, poolName)); + } + public static GameObject SpawnPoolObject(string poolName, Vector3 position, Quaternion rotation) { if(NetworkingManager.singleton.isServer)