diff --git a/Assets/ObjectPool/Scripts/ObjectPool.cs b/Assets/ObjectPool/Scripts/ObjectPool.cs index fba6c17..97ee7fc 100755 --- a/Assets/ObjectPool/Scripts/ObjectPool.cs +++ b/Assets/ObjectPool/Scripts/ObjectPool.cs @@ -15,10 +15,10 @@ public class StartupPool static ObjectPool _instance; static List tempList = new List(); - + Dictionary> pooledObjects = new Dictionary>(); Dictionary spawnedObjects = new Dictionary(); - + public StartupPoolMode startupPoolMode; public StartupPool[] startupPools; @@ -64,18 +64,17 @@ public static void CreatePool(GameObject prefab, int initialPoolSize) { bool active = prefab.activeSelf; prefab.SetActive(false); - Transform parent = instance.transform; while (list.Count < initialPoolSize) { var obj = (GameObject)Object.Instantiate(prefab); - obj.transform.parent = parent; + obj.transform.SetParent(instance.transform, false); // worldPositionStays=false to keep UI objects spawning consistently list.Add(obj); } prefab.SetActive(active); } } } - + public static T Spawn(T prefab, Transform parent, Vector3 position, Quaternion rotation) where T : Component { return Spawn(prefab.gameObject, parent, position, rotation).GetComponent(); @@ -104,7 +103,8 @@ public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 posi { List list; Transform trans; - GameObject obj; + GameObject obj = null; + CreatePool(prefab.gameObject, 0); // will create pool if none exists if (instance.pooledObjects.TryGetValue(prefab, out list)) { obj = null; @@ -118,7 +118,10 @@ public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 posi if (obj != null) { trans = obj.transform; - trans.parent = parent; + if (parent != null) + { + trans.SetParent(parent, false); // worldPositionStays=false to keep UI objects spawning consistently + } trans.localPosition = position; trans.localRotation = rotation; obj.SetActive(true); @@ -128,21 +131,15 @@ public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 posi } obj = (GameObject)Object.Instantiate(prefab); trans = obj.transform; - trans.parent = parent; + if (parent != null) + { + trans.SetParent(parent, false); // worldPositionStays=false to keep UI objects spawning consistently + } trans.localPosition = position; trans.localRotation = rotation; instance.spawnedObjects.Add(obj, prefab); - return obj; - } - else - { - obj = (GameObject)Object.Instantiate(prefab); - trans = obj.GetComponent(); - trans.parent = parent; - trans.localPosition = position; - trans.localRotation = rotation; - return obj; } + return obj; } public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 position) { @@ -173,15 +170,20 @@ public static void Recycle(GameObject obj) { GameObject prefab; if (instance.spawnedObjects.TryGetValue(obj, out prefab)) + { Recycle(obj, prefab); + } else + { + Debug.LogWarning(obj.name + "can not be recycled because it was never pooled. It will be destroyed instead."); Object.Destroy(obj); + } } static void Recycle(GameObject obj, GameObject prefab) { instance.pooledObjects[prefab].Add(obj); instance.spawnedObjects.Remove(obj); - obj.transform.parent = instance.transform; + obj.transform.SetParent(instance.transform, false); // worldPositionStays=false to keep UI objects spawning consistently obj.SetActive(false); } @@ -205,7 +207,7 @@ public static void RecycleAll() Recycle(tempList[i]); tempList.Clear(); } - + public static bool IsSpawned(GameObject obj) { return instance.spawnedObjects.ContainsKey(obj); @@ -356,7 +358,7 @@ public static void CreatePool(this GameObject prefab, int initialPoolSize) { ObjectPool.CreatePool(prefab, initialPoolSize); } - + public static T Spawn(this T prefab, Transform parent, Vector3 position, Quaternion rotation) where T : Component { return ObjectPool.Spawn(prefab, parent, position, rotation); @@ -405,7 +407,7 @@ public static GameObject Spawn(this GameObject prefab) { return ObjectPool.Spawn(prefab, null, Vector3.zero, Quaternion.identity); } - + public static void Recycle(this T obj) where T : Component { ObjectPool.Recycle(obj); @@ -509,4 +511,4 @@ public static void DestroyAll(this T prefab) where T : Component { ObjectPool.DestroyAll(prefab.gameObject); } -} +} \ No newline at end of file diff --git a/ObjectPool.userprefs b/ObjectPool.userprefs index 66f209e..ff50da4 100644 --- a/ObjectPool.userprefs +++ b/ObjectPool.userprefs @@ -1,10 +1,6 @@ - - - - - - - + + + diff --git a/ProjectSettings/ClusterInputManager.asset b/ProjectSettings/ClusterInputManager.asset new file mode 100644 index 0000000..de328e5 Binary files /dev/null and b/ProjectSettings/ClusterInputManager.asset differ diff --git a/ProjectSettings/GraphicsSettings.asset b/ProjectSettings/GraphicsSettings.asset index 4c8d526..96548d5 100644 Binary files a/ProjectSettings/GraphicsSettings.asset and b/ProjectSettings/GraphicsSettings.asset differ diff --git a/ProjectSettings/NavMeshAreas.asset b/ProjectSettings/NavMeshAreas.asset new file mode 100644 index 0000000..7e9342e Binary files /dev/null and b/ProjectSettings/NavMeshAreas.asset differ diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 4adf55b..fc7eeed 100644 Binary files a/ProjectSettings/ProjectSettings.asset and b/ProjectSettings/ProjectSettings.asset differ diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt new file mode 100644 index 0000000..558807b --- /dev/null +++ b/ProjectSettings/ProjectVersion.txt @@ -0,0 +1,2 @@ +m_EditorVersion: 5.3.5f1 +m_StandardAssetsVersion: 0 diff --git a/ProjectSettings/UnityAdsSettings.asset b/ProjectSettings/UnityAdsSettings.asset new file mode 100644 index 0000000..b8c7eb7 Binary files /dev/null and b/ProjectSettings/UnityAdsSettings.asset differ diff --git a/ProjectSettings/UnityConnectSettings.asset b/ProjectSettings/UnityConnectSettings.asset new file mode 100644 index 0000000..ec04bc7 Binary files /dev/null and b/ProjectSettings/UnityConnectSettings.asset differ