|
| 1 | +using UnityEngine; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | + |
| 5 | +// By Anish Dhesikan |
| 6 | +public abstract class BetterObjectPool : MonoBehaviour { |
| 7 | + |
| 8 | + public GameObject objectPrefab; |
| 9 | + |
| 10 | + // initialMax: How many objects should the pool initially contain? |
| 11 | + public int initialMax = 100; |
| 12 | + // minInPool: What is the smallest number of items this pool should contain at any point? |
| 13 | + public int minInPool = 20; |
| 14 | + |
| 15 | + /* |
| 16 | + * -- How does it work? -- |
| 17 | + * You can set 3 thresholds: lowerBound, middleBound, and upperBound. |
| 18 | + * At any time, if the percent of active objects from the pool hits |
| 19 | + * the lowerBound or upperBound thresholds, the object pool will begin to dynamically |
| 20 | + * create or destroy objects to get the number back to the middle threshold. |
| 21 | + * Try out the demo in ExampleScenes for more info. |
| 22 | + */ |
| 23 | + public float upperBound = 0.75f; // between 0 and 1 |
| 24 | + public float middleBound = 0.5f; // between 0 and 1 |
| 25 | + public float lowerBound = 0.25f; // between 0 and 1 |
| 26 | + |
| 27 | + |
| 28 | + // maxInstantiatesPerFrame: How many times can Instantiate() be called in a single frame? |
| 29 | + // For mobile and for best performance in general, keep this number low. |
| 30 | + // Currently, numbers less than 1 are not supported. |
| 31 | + public int maxInstantiatesPerFrame = 1; |
| 32 | + |
| 33 | + // maxDestroysPerFrame: How many times can Destroy() be called in a single frame? |
| 34 | + // For mobile and for best performance in general, keep this number low. |
| 35 | + // Currently, numbers less than 1 are not supported. |
| 36 | + public int maxDestroysPerFrame = 1; |
| 37 | + |
| 38 | + protected HashSet<GameObject> activeObjectPool = new HashSet<GameObject>(); |
| 39 | + protected Transform activeObjectPoolParent; |
| 40 | + protected List<GameObject> inactiveObjectPool = new List<GameObject>(); |
| 41 | + protected Transform inactiveObjectPoolParent; |
| 42 | + |
| 43 | + private int totalCount = 0; |
| 44 | + |
| 45 | + private float percentActive = 1; |
| 46 | + |
| 47 | + // Use this for initialization |
| 48 | + public virtual void Awake () { |
| 49 | + activeObjectPoolParent = new GameObject ("ActiveObjectPool " + objectPrefab.name).transform; |
| 50 | + inactiveObjectPoolParent = new GameObject ("InactiveObjectPool" + objectPrefab.name).transform; |
| 51 | + inactiveObjectPoolParent.gameObject.SetActive (false); |
| 52 | + InstantiateMultiple (Mathf.FloorToInt(initialMax * middleBound)); |
| 53 | + } |
| 54 | + |
| 55 | + // Update is called once per frame |
| 56 | + public virtual void Update () { |
| 57 | + UpdatePercentActive (); |
| 58 | + if (percentActive > upperBound) { |
| 59 | + StopAllCoroutines (); |
| 60 | + StartCoroutine (ScaleUp()); |
| 61 | + } else if (percentActive < lowerBound) { |
| 62 | + StopAllCoroutines (); |
| 63 | + StartCoroutine (ScaleDown()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + IEnumerator ScaleUp () { |
| 68 | + while (percentActive > middleBound) { |
| 69 | + //Mathf.FloorToInt (Mathf.Pow (totalCount, 0.5f)) |
| 70 | + int numToInstantiate = Mathf.Min (maxInstantiatesPerFrame, Mathf.CeilToInt(totalCount / 200f)); |
| 71 | + InstantiateMultiple (numToInstantiate); |
| 72 | + UpdatePercentActive (); |
| 73 | + yield return 0; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + IEnumerator ScaleDown () { |
| 78 | + if (percentActive > 0 && totalCount > minInPool) { |
| 79 | + while (percentActive < middleBound) { |
| 80 | + DestroyMultiple (maxDestroysPerFrame); |
| 81 | + UpdatePercentActive (); |
| 82 | + yield return 0; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public virtual bool ObjectIsActive (GameObject instance) { |
| 88 | + return activeObjectPool.Contains (instance); |
| 89 | + } |
| 90 | + |
| 91 | + public virtual void RemoveInstanceFromPool (GameObject instance) { |
| 92 | + StopAllCoroutines (); |
| 93 | + activeObjectPool.Remove (instance); |
| 94 | + inactiveObjectPool.Add (instance); |
| 95 | + instance.transform.parent = inactiveObjectPoolParent; |
| 96 | + } |
| 97 | + |
| 98 | + public virtual GameObject GetInstanceFromPool () { |
| 99 | + StopAllCoroutines (); |
| 100 | + GameObject curObject = null; |
| 101 | + if (inactiveObjectPool.Count <= 0) { |
| 102 | + InstantiateOne (); |
| 103 | + } |
| 104 | + |
| 105 | + curObject = inactiveObjectPool [0] as GameObject; |
| 106 | +// curObject.SetActive (true); |
| 107 | + inactiveObjectPool.Remove (curObject); |
| 108 | + activeObjectPool.Add (curObject); |
| 109 | + curObject.transform.parent = activeObjectPoolParent; |
| 110 | + |
| 111 | + return curObject; |
| 112 | + } |
| 113 | + |
| 114 | + public virtual GameObject GetInstanceFromPool (Vector3 position, Quaternion rotation) { |
| 115 | + GameObject curObject = GetInstanceFromPool (); |
| 116 | + if (curObject != null) { |
| 117 | + curObject.transform.position = position; |
| 118 | + curObject.transform.rotation = rotation; |
| 119 | + } |
| 120 | + return curObject; |
| 121 | + } |
| 122 | + |
| 123 | + // Instantiates, deactivates, and adds one to the pool |
| 124 | + private void InstantiateOne () { |
| 125 | + GameObject newObject = Instantiate (objectPrefab) as GameObject; |
| 126 | +// newObject.SetActive (false); |
| 127 | + inactiveObjectPool.Add (newObject); |
| 128 | + newObject.transform.parent = inactiveObjectPoolParent; |
| 129 | + totalCount++; |
| 130 | + } |
| 131 | + |
| 132 | + private void InstantiateMultiple (int count) { |
| 133 | + for (int i = 0; i < count; i++) { |
| 134 | + InstantiateOne (); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private void DestroyOne () { |
| 139 | + if (inactiveObjectPool.Count > 0) { |
| 140 | + GameObject curObject = inactiveObjectPool [0] as GameObject; |
| 141 | + inactiveObjectPool.Remove (curObject); |
| 142 | + DestroyImmediate (curObject); |
| 143 | + totalCount--; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private void DestroyMultiple (int count) { |
| 148 | + for (int i = 0; i < count; i++) { |
| 149 | + DestroyOne (); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public string GetInfoText () { |
| 154 | + return "Percent Active: " + percentActive * 100 + "%" + "\n" + |
| 155 | + "Number Active: " + activeObjectPool.Count + "\n" + |
| 156 | + "Number Inactive: " + inactiveObjectPool.Count; |
| 157 | + } |
| 158 | + |
| 159 | + private void UpdatePercentActive () { |
| 160 | + percentActive = (float) activeObjectPool.Count / totalCount; |
| 161 | + |
| 162 | + |
| 163 | +// if (percentActive < lowerBound) { |
| 164 | +// Debug.Log ("Percent Active: " + percentActive); |
| 165 | +// Debug.Log ("TotalCount: " + totalCount); |
| 166 | +// } |
| 167 | + } |
| 168 | +} |
0 commit comments