Skip to content

Commit

Permalink
tab->space
Browse files Browse the repository at this point in the history
Add namespace SG (SoulGame), according to suggestions from Issue qiankanglai#13
  • Loading branch information
qiankanglai committed Aug 27, 2017
1 parent 0ee3f9e commit 8425a58
Show file tree
Hide file tree
Showing 7 changed files with 372 additions and 386 deletions.
228 changes: 116 additions & 112 deletions Assets/Scripts/EasyObjectPool/EasyObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,155 +7,159 @@
using UnityEngine;
using System.Collections.Generic;

[DisallowMultipleComponent]
[AddComponentMenu("")]
public class PoolObject : MonoBehaviour
{
public string poolName;
//defines whether the object is waiting in pool or is in use
public bool isPooled;
}

public enum PoolInflationType
{
/// When a dynamic pool inflates, add one to the pool.
INCREMENT,
/// When a dynamic pool inflates, double the size of the pool
DOUBLE
}

class Pool
namespace SG
{
private Stack<PoolObject> availableObjStack = new Stack<PoolObject>();
[DisallowMultipleComponent]
[AddComponentMenu("")]
public class PoolObject : MonoBehaviour
{
public string poolName;
//defines whether the object is waiting in pool or is in use
public bool isPooled;
}

//the root obj for unused obj
private GameObject rootObj;
private float lastUsedTime = -1;
private PoolInflationType inflationType;
private string poolName;
private int objectsInUse = 0;
public enum PoolInflationType
{
/// When a dynamic pool inflates, add one to the pool.
INCREMENT,
/// When a dynamic pool inflates, double the size of the pool
DOUBLE
}

public Pool(string poolName, GameObject poolObjectPrefab, GameObject rootPoolObj, int initialCount, PoolInflationType type)
class Pool
{
lastUsedTime = Time.time;
private Stack<PoolObject> availableObjStack = new Stack<PoolObject>();

//the root obj for unused obj
private GameObject rootObj;
private float lastUsedTime = -1;
private PoolInflationType inflationType;
private string poolName;
private int objectsInUse = 0;

if (poolObjectPrefab == null)
public Pool(string poolName, GameObject poolObjectPrefab, GameObject rootPoolObj, int initialCount, PoolInflationType type)
{
lastUsedTime = Time.time;

if (poolObjectPrefab == null)
{
#if UNITY_EDITOR
Debug.LogError("[ObjPoolManager] null pool object prefab !");
Debug.LogError("[ObjPoolManager] null pool object prefab !");
#endif
return;
}
this.poolName = poolName;
this.inflationType = type;
this.rootObj = new GameObject(poolName + "Pool");
this.rootObj.transform.SetParent(rootPoolObj.transform, false);

// In case the origin one is Destroyed, we should keep at least one
GameObject go = GameObject.Instantiate(poolObjectPrefab);
PoolObject po = go.GetComponent<PoolObject>();
if (po == null)
{
po = go.AddComponent<PoolObject>();
}
po.poolName = poolName;
AddObjectToPool(po);
return;
}
this.poolName = poolName;
this.inflationType = type;
this.rootObj = new GameObject(poolName + "Pool");
this.rootObj.transform.SetParent(rootPoolObj.transform, false);

//populate the pool
populatePool(Mathf.Max(initialCount, 1));
}
// In case the origin one is Destroyed, we should keep at least one
GameObject go = GameObject.Instantiate(poolObjectPrefab);
PoolObject po = go.GetComponent<PoolObject>();
if (po == null)
{
po = go.AddComponent<PoolObject>();
}
po.poolName = poolName;
AddObjectToPool(po);

//o(1)
private void AddObjectToPool(PoolObject po)
{
//add to pool
po.gameObject.SetActive(false);
po.gameObject.name = poolName;
availableObjStack.Push(po);
po.isPooled = true;
//add to a root obj
po.gameObject.transform.SetParent(rootObj.transform, false);
}
//populate the pool
populatePool(Mathf.Max(initialCount, 1));
}

private void populatePool(int initialCount)
{
for (int index = 0; index < initialCount; index++)
//o(1)
private void AddObjectToPool(PoolObject po)
{
PoolObject po = GameObject.Instantiate(availableObjStack.Peek());
AddObjectToPool(po);
//add to pool
po.gameObject.SetActive(false);
po.gameObject.name = poolName;
availableObjStack.Push(po);
po.isPooled = true;
//add to a root obj
po.gameObject.transform.SetParent(rootObj.transform, false);
}
}

//o(1)
public GameObject NextAvailableObject(bool autoActive)
{
lastUsedTime = Time.time;

PoolObject po = null;
if (availableObjStack.Count > 1)
private void populatePool(int initialCount)
{
po = availableObjStack.Pop();
for (int index = 0; index < initialCount; index++)
{
PoolObject po = GameObject.Instantiate(availableObjStack.Peek());
AddObjectToPool(po);
}
}
else

//o(1)
public GameObject NextAvailableObject(bool autoActive)
{
int increaseSize = 0;
//increment size var, this is for info purpose only
if (inflationType == PoolInflationType.INCREMENT)
lastUsedTime = Time.time;

PoolObject po = null;
if (availableObjStack.Count > 1)
{
increaseSize = 1;
po = availableObjStack.Pop();
}
else if (inflationType == PoolInflationType.DOUBLE)
else
{
increaseSize = availableObjStack.Count + Mathf.Max(objectsInUse, 0);
}
int increaseSize = 0;
//increment size var, this is for info purpose only
if (inflationType == PoolInflationType.INCREMENT)
{
increaseSize = 1;
}
else if (inflationType == PoolInflationType.DOUBLE)
{
increaseSize = availableObjStack.Count + Mathf.Max(objectsInUse, 0);
}
#if UNITY_EDITOR
Debug.Log(string.Format("Growing pool {0}: {1} populated", poolName, increaseSize));
Debug.Log(string.Format("Growing pool {0}: {1} populated", poolName, increaseSize));
#endif
if (increaseSize > 0)
{
populatePool(increaseSize);
po = availableObjStack.Pop();
if (increaseSize > 0)
{
populatePool(increaseSize);
po = availableObjStack.Pop();
}
}
}

GameObject result = null;
if (po != null)
{
objectsInUse++;
po.isPooled = false;
result = po.gameObject;
if (autoActive)
GameObject result = null;
if (po != null)
{
result.SetActive(true);
objectsInUse++;
po.isPooled = false;
result = po.gameObject;
if (autoActive)
{
result.SetActive(true);
}
}
}

return result;
}
return result;
}

//o(1)
public void ReturnObjectToPool(PoolObject po)
{
if (poolName.Equals(po.poolName))
//o(1)
public void ReturnObjectToPool(PoolObject po)
{
objectsInUse--;
/* we could have used availableObjStack.Contains(po) to check if this object is in pool.
* While that would have been more robust, it would have made this method O(n)
*/
if (po.isPooled)
if (poolName.Equals(po.poolName))
{
objectsInUse--;
/* we could have used availableObjStack.Contains(po) to check if this object is in pool.
* While that would have been more robust, it would have made this method O(n)
*/
if (po.isPooled)
{
#if UNITY_EDITOR
Debug.LogWarning(po.gameObject.name + " is already in pool. Why are you trying to return it again? Check usage.");
Debug.LogWarning(po.gameObject.name + " is already in pool. Why are you trying to return it again? Check usage.");
#endif
}
else
{
AddObjectToPool(po);
}
}
else
{
AddObjectToPool(po);
Debug.LogError(string.Format("Trying to add object to incorrect pool {0} {1}", po.poolName, poolName));
}
}
else
{
Debug.LogError(string.Format("Trying to add object to incorrect pool {0} {1}", po.poolName, poolName));
}
}
}
15 changes: 0 additions & 15 deletions Assets/Scripts/EasyObjectPool/PoolObject.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Assets/Scripts/EasyObjectPool/PoolObject.cs.meta

This file was deleted.

Loading

0 comments on commit 8425a58

Please sign in to comment.