/// <summary> /// Add new object to pool /// </summary> /// <param name="poolObject">Pool objects interface</param> public void AddPoolObject(KS_IPoolObject poolObject) { KS_IPoolObject data; if (!pool.TryGetValue(poolObject._Id, out data)) { // check pooling limits for Debug.Log("A: " + GetAmountOfType(poolObject.PoolSettings().tag) + " T: " + poolObject.PoolSettings().poolLimit); if (GetAmountOfType(poolObject.PoolSettings().tag) > (poolObject.PoolSettings().poolLimit - 1)) { RemovePoolItem(poolObject.GameObject); return; } else { poolObject.GameObject.transform.parent = _inactive.transform; pool.Add(poolObject._Id, poolObject); poolObject.GameObject.SetActive(false); } } else { } }
/// <summary> /// Remove object from pool /// </summary> /// <param name="poolObject">Game object to reomve (Must use KS_IPoolObject interface) <see cref="KS_IPoolObject"/></param> public void RemovePoolItem(GameObject poolObject) { KS_IPoolObject i = poolObject.GetComponent <KS_IPoolObject>(); if (i.HasId) { pool.Remove(i._Id); } Destroy(poolObject); }
void Spawn() { int i = Random.Range(0, spawnObjects.Length); int p = Random.Range(0, spawnPoints.Length); string tag = spawnObjects[i].GetComponent <KS_IPoolObject>().PoolSettings().tag; KS_IPoolObject o = pool.Get(tag); GameObject obj; if (o != null) { obj = o.GameObject; obj.SetActive(true); } else { obj = Instantiate(spawnObjects[i]); } obj.transform.position = spawnPoints[p].position; obj.transform.rotation = Random.rotation; }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Q) || KS_Input.GetKeyDown(KS_Input.DS4ButtonToKey(DS4KeyCode.Square))) { Spawn(); } if (Input.GetKeyDown(KeyCode.F) || KS_Input.GetKeyDown(KS_Input.DS4ButtonToKey(DS4KeyCode.Triangle))) { ClearPool(); } string text = ""; for (int i = 0; i < spawnObjects.Length; i++) { KS_IPoolObject o = spawnObjects[i].GetComponent <KS_IPoolObject>(); text += o.PoolSettings().tag + ": Active in scene (" + GetAmountOfType(o.PoolSettings().tag) + ") ---- In pool (" + pool.GetAmountOfType(o.PoolSettings().tag) + ")\n"; } wallText.text = text; }