/* CreateInstance: Instantiates a prefab of the object at the particular
     * location provided.  This specficially creates a prefab clone if the prefab has
     * the recycle script attached.
     */
    private RecycleGameObj CreateInstance(Vector3 pos)
    {
        RecycleGameObj clone = GameObject.Instantiate(prefab);

        clone.transform.position = pos;
        clone.transform.parent   = transform;
        poolInstances.Add(clone);
        return(clone);
    }
    private static ObjectPool GetObjectPool(RecycleGameObj reference)
    {
        ObjectPool pool = null;

        if (pools.ContainsKey(reference))
        {
            pool = pools [reference];
        }
        else
        {
            var poolContainer = new GameObject(reference.gameObject.name + "ObjectPool");
            pool        = poolContainer.AddComponent <ObjectPool>();
            pool.prefab = reference;
            pools.Add(reference, pool);
        }
        return(pool);
    }
Exemplo n.º 3
0
    /* Destroy: Similarly to Instantiate, it either shuts down the object
     * if the object contains a RecycleGameObject script, else destroys
     * the object like in GameObject.
     */
    public static void Destroy(GameObject gameObject)
    {
        if (gameObject == null)
        {
            return;
        }
        RecycleGameObj recycleGameObject = gameObject.GetComponent <RecycleGameObj> ();

        if (recycleGameObject != null)
        {
            recycleGameObject.Shutdown();
        }
        else
        {
            GameObject.Destroy(gameObject);
        }
    }
Exemplo n.º 4
0
    /* Instantiate: If it detects a recycle script on the object requested,
     * it finds the correct object pool for that requested prefab
     * and requests a new game object from the object pool (regardless of status
     * of the status of the object pool).
     * Otherwise it just instantiates like in GameObject.
     */
    public static GameObject Instantiate(GameObject prefab, Vector3 pos)
    {
        GameObject     instance       = null;
        RecycleGameObj recycledScript = prefab.GetComponent <RecycleGameObj> ();

        if (recycledScript != null)
        {
            ObjPool pool = GetObjPool(recycledScript);
            instance = pool.NextObject(pos).gameObject;
        }
        else
        {
            instance = GameObject.Instantiate(prefab);
            instance.transform.position = pos;
        }
        return(instance);
    }
    /* NextObject: Searches object pool for an instance that has been
     * turned off.  If any are, then it restarts the instance and
     * returns that object.  Otherwise it calls the creation of a new
     * instance.
     */
    public RecycleGameObj NextObject(Vector3 pos)
    {
        RecycleGameObj instance = null;

        foreach (RecycleGameObj go in poolInstances)
        {
            if (!(go.gameObject.activeSelf))
            {
                instance = go;
                instance.transform.position = pos;
            }
        }
        if (instance == null)
        {
            instance = CreateInstance(pos);
        }
        instance.Restart();
        return(instance);
    }