From 7e4cc2516daee7ccab795d686e9cb5af434303b2 Mon Sep 17 00:00:00 2001 From: sfariv Date: Mon, 7 Aug 2017 14:33:20 -0700 Subject: [PATCH 1/4] added a parameter to make it possible to get a gameobject from pool and making it active in hierarchy later. --- Assets/UnityPooler/Scripts/PoolableGameObject.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/UnityPooler/Scripts/PoolableGameObject.cs b/Assets/UnityPooler/Scripts/PoolableGameObject.cs index a3e099e..2249f04 100644 --- a/Assets/UnityPooler/Scripts/PoolableGameObject.cs +++ b/Assets/UnityPooler/Scripts/PoolableGameObject.cs @@ -41,7 +41,7 @@ public class PoolableGameObject : MonoBehaviour /// Returns an object from the pool /// /// The object returned. - public PoolableGameObject Get() + public PoolableGameObject Get(bool gameObjectActiveState = true) { Initialize(); @@ -65,7 +65,7 @@ public PoolableGameObject Get() while (obj == null || obj.gameObject == null); obj._isActive = true; - obj.gameObject.SetActive(true); + obj.gameObject.SetActive(gameObjectActiveState); _numOfActiveObjs++; if (useCap || persistAcrossScenes) @@ -472,4 +472,4 @@ private void OnDestroy() #endregion } -} \ No newline at end of file +} From 10fb4c2eb57e4e389ea87cfd1836abef7fcfc8d2 Mon Sep 17 00:00:00 2001 From: sfariv Date: Mon, 7 Aug 2017 14:49:10 -0700 Subject: [PATCH 2/4] updating extension methods... updating extension methods to enable fetching inactive gameobjects from the pool. --- Assets/UnityPooler/Scripts/GameObjectPool.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Assets/UnityPooler/Scripts/GameObjectPool.cs b/Assets/UnityPooler/Scripts/GameObjectPool.cs index 379838f..c11e8a5 100644 --- a/Assets/UnityPooler/Scripts/GameObjectPool.cs +++ b/Assets/UnityPooler/Scripts/GameObjectPool.cs @@ -20,7 +20,7 @@ public static class GameObjectPool /// The prefab or GameObject that we /// want a duplicated object of. /// A GameObject from the object pool. - public static GameObject GetObj(GameObject objToCreateFrom) + public static GameObject GetObj(GameObject objToCreateFrom, bool gameObjectActiveState = true) { PoolableGameObject poolable = objToCreateFrom.GetComponent(); @@ -30,7 +30,7 @@ public static GameObject GetObj(GameObject objToCreateFrom) return null; } - return poolable.Get().gameObject; + return poolable.Get(gameObjectActiveState).gameObject; } /// @@ -112,9 +112,9 @@ public static void IncrementPoolWithObj(GameObject objToInc) /// /// /// - public static GameObject Get(this GameObject obj) + public static GameObject Get(this GameObject obj, bool gameObjectActiveState = true) { - return GetObj(obj); + return GetObj(obj, gameObjectActiveState); } /// From 27e6b825b3b848ca1da1f0dc4060ef9a75c25f6f Mon Sep 17 00:00:00 2001 From: sfariv Date: Thu, 21 Dec 2017 09:46:07 -0800 Subject: [PATCH 3/4] fixed an issue when calling "OnObjectCreated", and "OnObjectReused" wrong instance was used to invoke these 2 functions. --- Assets/UnityPooler/Scripts/PoolableGameObject.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Assets/UnityPooler/Scripts/PoolableGameObject.cs b/Assets/UnityPooler/Scripts/PoolableGameObject.cs index 2249f04..2a17a5e 100644 --- a/Assets/UnityPooler/Scripts/PoolableGameObject.cs +++ b/Assets/UnityPooler/Scripts/PoolableGameObject.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections.Generic; using UnityEngine.SceneManagement; @@ -41,7 +41,7 @@ public class PoolableGameObject : MonoBehaviour /// Returns an object from the pool /// /// The object returned. - public PoolableGameObject Get(bool gameObjectActiveState = true) + public PoolableGameObject Get() { Initialize(); @@ -65,7 +65,7 @@ public PoolableGameObject Get(bool gameObjectActiveState = true) while (obj == null || obj.gameObject == null); obj._isActive = true; - obj.gameObject.SetActive(gameObjectActiveState); + obj.gameObject.SetActive(true); _numOfActiveObjs++; if (useCap || persistAcrossScenes) @@ -234,8 +234,8 @@ public void ReleaseObjectsAndClearPool() [System.NonSerialized] private bool _initialized; - [System.NonSerialized] - private IGameObjectPoolable[] _poolables; + //[System.NonSerialized] + //private IGameObjectPoolable[] _poolables; /// /// The function that will be called for reuse on MonoBehaviours. Change this if it conflicts. @@ -369,7 +369,7 @@ private void SendCreationMessage(PoolableGameObject newObj) onObjectCreation(newObj.gameObject); } - _poolables = GetComponentsInChildren(true); + var _poolables = newObj.GetComponentsInChildren(true); for (int i = 0; i < _poolables.Length; i++) { @@ -397,6 +397,8 @@ private PoolableGameObject ReuseObject() } while (objToUse == null || objToUse.gameObject == null || !objToUse._isActive); + var _poolables = objToUse.GetComponentsInChildren(true); + for (int i = 0; i < _poolables.Length; i++) { _poolables[i].OnObjectReused(); From 72e9e837a623a596f5fbc0ac51a7348b5f685a1a Mon Sep 17 00:00:00 2001 From: sfariv Date: Wed, 3 Jan 2018 09:51:12 -0800 Subject: [PATCH 4/4] fixed an issue with pooler cap size logic. the release function is called on instances of the original poolable gameobject. that's why we should change `_numOfActiveObjs` of the original gameobject. --- Assets/UnityPooler/Scripts/PoolableGameObject.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/UnityPooler/Scripts/PoolableGameObject.cs b/Assets/UnityPooler/Scripts/PoolableGameObject.cs index 2a17a5e..b681967 100644 --- a/Assets/UnityPooler/Scripts/PoolableGameObject.cs +++ b/Assets/UnityPooler/Scripts/PoolableGameObject.cs @@ -121,7 +121,7 @@ public void Release() transform.SetParent(container, false); gameObject.SetActive(false); _originalObject._pooledObjs.Push(this); - _numOfActiveObjs--; + _originalObject._numOfActiveObjs--; } ///