Ejemplo n.º 1
0
    public bool Reclaim(string type, GameObject obj)
    {
        // try to get the subpool from the subpool cache
        Subpool subpool;

        this._subpools.TryGetValue(type, out subpool);

        // if no subpool exists
        if (subpool == null)
        {
            Debug.LogError(string.Format("Could not reclaim object of type {0} – no subpool exists.", type));
            return(false);
        }

        // ask the subpool to reclaim the object
        bool result = subpool.Reclaim(obj);

        if (!result)
        {
            Debug.LogError(string.Format("Subpool could not reclaim object of type {0}.", type));
            return(false);
        }

        // get PooledBehavior from object
        PooledBehavior behavior = obj.GetComponent <PooledBehavior>();

        // if behavior exists
        if (behavior)
        {
            // call events on it
            behavior.OnReclaim();
        }

        return(true);
    }
Ejemplo n.º 2
0
    public GameObject Spawn(string type, bool active = true)
    {
        // try to get the subpool from the subpool cache
        Subpool subpool;

        this._subpools.TryGetValue(type, out subpool);

        // if no subpool exists
        if (subpool == null)
        {
            subpool = CreatePool(type);

            if (subpool == null)
            {
                return(null);
            }
        }

        // ask the subpool to spawn a new object
        GameObject obj = subpool.Spawn();

        // set active status
        obj.SetActive(active);

        // get PooledBehavior from object
        PooledBehavior behavior = obj.GetComponent <PooledBehavior>();

        // if behavior exists
        if (behavior)
        {
            // if this is the first time spawning the object, trigger its OnFirstSpawn method
            if (behavior.previouslySpawned == false)
            {
                behavior.OnFirstSpawn();
            }

            // trigger its OnSpawn method
            behavior.OnSpawn();

            // set its pool name
            behavior.poolName = type;
        }

        return(obj);
    }
Ejemplo n.º 3
0
 public bool Reclaim(PooledBehavior behavior)
 {
     // return behavior to the pool that it came from
     return(this.Reclaim(behavior.poolName, behavior.gameObject));
 }