Exemplo n.º 1
0
 private static void Recycle(GameObjectMeta obj, GameObject prefab)
 {
     _Instance._PooledObjects[prefab.GetInstanceID()].Add(obj.Object);
     _Instance._InstantiatedObjects.Remove(obj);
     obj.Object.transform.parent = _Instance.transform;
     obj.Object.SetActive(false);
 }
Exemplo n.º 2
0
    public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 position, Quaternion rotation)
    {
        List <GameObject> gos;

        if (_Instance._PooledObjects.TryGetValue(prefab.GetInstanceID(), out gos))
        {
            GameObject obj   = null;
            Transform  trans = null;

            if (gos.Count > 0)
            {
                while (obj == null && gos.Count > 0)
                {
                    obj = gos[0];
                    gos.RemoveAt(0);
                }

                if (obj != null)
                {
                    trans               = obj.transform;
                    trans.parent        = parent;
                    trans.localPosition = position;
                    trans.localRotation = rotation;
                    obj.SetActive(true);

                    GameObjectMeta gom = new GameObjectMeta();
                    gom.Object = obj;

                    _Instance._InstantiatedObjects.Add(gom, prefab);

                    return(obj);
                }
            }

            switch (_Instance.Behaviour)
            {
            case PoolBehaviour.HardLimit:
                obj = null;
                break;

            case PoolBehaviour.GrowOnMax:
                obj                 = (GameObject)Object.Instantiate(prefab);
                trans               = obj.transform;
                trans.parent        = parent;
                trans.localPosition = position;
                trans.localRotation = rotation;
                GameObjectMeta gom = new GameObjectMeta();
                gom.Object = obj;
                _Instance._InstantiatedObjects.Add(gom, prefab);
                break;

            case PoolBehaviour.RecycleOnMax:
                List <KeyValuePair <GameObjectMeta, GameObject> > gosOfType = _Instance._InstantiatedObjects.Where(pair => pair.Value == prefab).ToList();
                GameObjectMeta oldestGOM = null;

                foreach (KeyValuePair <GameObjectMeta, GameObject> kvp in gosOfType)
                {
                    if (oldestGOM == null)
                    {
                        oldestGOM = kvp.Key;
                    }
                    else
                    {
                        if (System.DateTime.Compare(kvp.Key.TimeStamp, oldestGOM.TimeStamp) < 0)
                        {
                            oldestGOM = kvp.Key;
                        }
                    }
                }

                if (oldestGOM != null)
                {
                    Recycle(oldestGOM, prefab);

                    // now that we KNOW one is free, recursively call spawn (only once though)
                    Spawn(prefab, parent, position, rotation);
                }
                break;
            }

            return(obj);
        }
        else
        {
            GameObject obj   = (GameObject)Object.Instantiate(prefab);
            Transform  trans = obj.GetComponent <Transform>();
            trans.parent        = parent;
            trans.localPosition = position;
            trans.localRotation = rotation;
            return(obj);
        }
    }