コード例 #1
0
    public RecycleGameObject NextObject(Vector3 pos)
    {
        RecycleGameObject instance = null;

        foreach (var go in poolInstances)
        // go through each game object and see if its set to false
        {
            if (go.gameObject.activeSelf != true)
            // since 'go' is autmoaticllay typed to a recycled game object
            // we need a reference of the object its attached to
            {
                instance = go;
                instance.transform.position = pos;
            }
        }

        if (instance == null)
        {
            instance = CreateInstance(pos);
        }


        instance.Restart();

        return(instance);
    }
コード例 #2
0
    public RecycleGameObject NextObject(Vector3 pos)
    {
        RecycleGameObject instance = null;

        foreach (var go in poolInstances)
        {
            if (go == null)
            {
                instance = CreateInstance(pos);
            }
            else
            {
                if (go.gameObject.activeSelf != true)
                {
                    instance = go;
                    instance.transform.position = pos;
                }
            }
        }

        if (instance == null)
        {
            instance = CreateInstance(pos);
        }


        instance.Restart();

        return(instance);
    }
コード例 #3
0
/*	public static void Destroy(GameObject gameObject){
 *              //获取具有可复用功能的待销毁对象
 *              var recyleGameObject = gameObject.GetComponent<RecycleGameObject>();
 *              //判断对象是否具有可复用功能
 *              if (RecycleGameObject != null) {
 *                      //具有可复用性关闭活性
 *                      RecycleGameObject.Shutdown ();
 *              }
 *              else {
 *                      //不具备可复用性销毁
 *                      GameObject.Destroy (gameObject);
 *              }
 *      }*/


    //查询对象池中的克隆体对象
    private static ObjectPool GetObjectPool(RecycleGameObject 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);
    }
コード例 #4
0
    //Return the next object in the pool
    public RecycleGameObject NextObject(Vector3 pos)
    {
        RecycleGameObject instance = null;

        //Check the pool of instances
        foreach (var go in poolInstances)
        {
            //Debug.Log("Check the pool of instances");
            if (go.gameObject.activeSelf != true)
            {
                //Debug.Log("activate instances");
                instance = go;
                instance.transform.position = pos;
            }
        }

        //If there is no instance active... Create it!
        if (instance == null)
        {
            instance = CreateInstance(pos);
        }

        //Restart the instance
        instance.Restart();

        return(instance);
    }
コード例 #5
0
    // visszaadja a következő szabad példányt a poolból
    public RecycleGameObject NextObject(Vector3 position)
    {
        RecycleGameObject instance = null;

        // végigmegyünk az összes példányon a poolban
        foreach (var poolInstance in _poolInstances)
        {
            if (poolInstance.gameObject.activeSelf)
            {
                continue;                                        // ha aktív, akkor épp használatban van
            }
            instance = poolInstance;
            instance.transform.position = position;
        }

        // ha nem találtunk szabad példányt, létrehozunk egyet
        if (instance == null)
        {
            instance = CreateInstance(position);
        }

        // inicializáljuk a szabad példányt
        instance.Restart();
        return(instance);
    }
コード例 #6
0
    public RecycleGameObject NextObject(Vector3 pos)
    {
        RecycleGameObject instance = null;

        instance = CreateInstance(pos);
        instance.Restart();

        return(instance);
    }
コード例 #7
0
    private RecycleGameObject CreateInstance(Vector3 pos)
    {
        RecycleGameObject clone = GameObject.Instantiate(prefab);

        clone.transform.position = pos;
        clone.transform.parent   = transform;

        poolInstances.Add(clone);

        return(clone);
    }
コード例 #8
0
 private static ObjectPool GetObjectPool(RecycleGameObject 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;
 }
コード例 #9
0
    public static void Destroy(GameObject gameObject)
    {
        RecycleGameObject recycleGameObject = gameObject.GetComponent <RecycleGameObject>();

        if (recycleGameObject != null)
        {
            recycleGameObject.Shutdown();
        }
        else
        {
            GameObject.Destroy(gameObject);
        }
    }
コード例 #10
0
    private static ObjectPool GetObjectPool(RecycleGameObject 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);
    }
コード例 #11
0
    public static GameObject Instantiate(GameObject prefab, Vector3 position)
    {
        GameObject instance = null;

        RecycleGameObject recycledScript = prefab.GetComponent <RecycleGameObject>();

        if (recycledScript != null)
        {
            ObjectPool pool = GetObjectPool(recycledScript);
            instance = pool.NextObject(position).gameObject;
        }
        else
        {
            instance = GameObject.Instantiate(prefab);
            instance.transform.position = position;
        }
        return(instance);
    }
コード例 #12
0
    // visszaadja az adott típusú prefabhez tartozó object poolt
    private static ObjectPool GetObjectPool(RecycleGameObject recycleScript)
    {
        ObjectPool pool;

        if (Pools.ContainsKey(recycleScript)) // ha a pool tartalmaz a szkripthez tartozó object poolt
        {
            pool = Pools[recycleScript];      // beállítjuk a megfelelő poolt, amit később vissza is adunk
        }
        else                                  // ellenkező esetben új poolt hozunk létre
        {
            var poolContainer = new GameObject(recycleScript.gameObject.name + " Pool");
            pool        = poolContainer.AddComponent <ObjectPool>();
            pool.Prefab = recycleScript;
            Pools.Add(recycleScript, pool);
        }

        return(pool);
    }
コード例 #13
0
    // a method that'll return us the instance of the pool based on the gameObject we're requesting
    private static ObjectPool GetObjectPool(RecycleGameObject reference)
    {
        ObjectPool pool = null;           // the object that we are going to return

        if (pools.ContainsKey(reference)) //making sure that the key exists
        {
            pool = pools [reference];
        }
        else
        {
            //if we don't have the objectPool, we need to create it.
            var poolContainer = new GameObject(reference.gameObject.name + "ObjectPool");
            pool        = poolContainer.AddComponent <ObjectPool> (); //adding the objectpool script to the new poolContainer and setting it to the pool value
            pool.prefab = reference;                                  //so that the pool knows which type of object to create
            pools.Add(reference, pool);                               //adding the new pool to the dictionary
        }
        return(pool);
    }
コード例 #14
0
    //Return the instance of the pool based on the gameobject we're requesting
    private static ObjectPool GetObjectPool(RecycleGameObject reference)
    {
        ObjectPool pool = null;

        //Acces the dictionary
        if (pools.ContainsKey(reference))
        {
            pool = pools[reference];
        }
        else //if we dont have the pool object, make it
        {
            var poolContainer = new GameObject(reference.gameObject.name + "_ObjectPool");//Add a name
            pool        = poolContainer.AddComponent <ObjectPool>();
            pool.prefab = reference;
            pools.Add(reference, pool);
        }

        return(pool);
    }
コード例 #15
0
    //获取游戏对象方法
    public RecycleGameObject NextObject(Vector3 pos)
    {
        //创建一个获取克隆体变量
        RecycleGameObject instance = null;

        //调用方法获取克隆体实例
        instance = CreateInstance(pos);
        //激活克隆对象
        instance.Restart();
        foreach (var go in poolInstance)
        {
            if (go.gameObject.activeSelf != true)
            {
                instance = go;
                instance.transform.position = pos;
            }
        }
        //返回克隆体对象
        return(instance);
    }
コード例 #16
0
    private static ObjectPool GetObjectPool(RecycleGameObject reference)
    {
        ObjectPool pool = null;

        //if we find the pool we return it
        if (pools.ContainsKey(reference))
        {
            pool = pools [reference];
        }
        else
        {
            //we need to create new pool
            var poolContainer = new GameObject(reference.gameObject.name + "ObjectPool");
            pool        = poolContainer.AddComponent <ObjectPool>();
            pool.prefab = reference;
            pools.Add(reference, pool);
        }

        return(pool);
    }
コード例 #17
0
    private static ObjectPool GetObjectPool(RecycleGameObject reference)
    {
        ObjectPool pool = null;

        if (pools.ContainsKey(reference))
        {
            pool = pools [reference];
        }
        // if the pool doesn't exist, create it from scratch
        else
        {
            // way for us to store the refence of the pool script on it
            var poolContainer = new GameObject(reference.gameObject.name + "ObjectPool");
            pool        = poolContainer.AddComponent <ObjectPool>();
            pool.prefab = reference;
            pools.Add(reference, pool);
        }

        return(pool);
    }
コード例 #18
0
    public static GameObject Instantiate(GameObject prefab, Vector3 pos, GameObject root, string name)
    {
        GameObject instance = null;

        RecycleGameObject recycledScript = prefab.GetComponent <RecycleGameObject>();

        if (recycledScript != null)
        {
            ObjectPool pool = GetObjectPool(recycledScript);
            instance = pool.NextObject(pos).gameObject;
        }
        else
        {
            instance = GameObject.Instantiate(prefab);
            instance.transform.position = pos;
            instance.transform.parent   = root.transform;
        }

        instance.name = name;
        return(instance);
    }
コード例 #19
0
    /// <summary>
    /// 实例化指定的 Prefab,并放置到指定位置上
    /// </summary>
    public static GameObject Instantiate(GameObject prefab, Vector3 position)
    {
        GameObject instance = null;

        RecycleGameObject recycleGameObject = prefab.GetComponent <RecycleGameObject>();

        if (null != recycleGameObject)
        {
            // 如果该 Prefab 为可复用对象,则从相应的对象池中获取对象实例
            ObjectPool pool = GetObjectPool(recycleGameObject);
            instance = pool.NextObject(position).gameObject;
        }
        else
        {
            // 如果该 Prefab 不是可复用对象,则创建该 Prefab 的实例
            instance = GameObject.Instantiate(prefab);
            instance.transform.position = position;
        }

        return(instance);
    }
コード例 #20
0
    private static ObjectPool GetObjectPool(RecycleGameObject reference)
    {
        ObjectPool pool = null;

        //pool exists already
        if (pools.ContainsKey(reference))
        {
            pool = pools[reference];

            //otherwise a new pool needs to be created
        }
        else
        {
            var poolContainer = new GameObject(reference.gameObject.name + "ObjectPool");
            pool        = poolContainer.AddComponent <ObjectPool>();
            pool.prefab = reference; //allows pool to know what type of object to create
            pools.Add(reference, pool);
        }

        return(pool);
    }
コード例 #21
0
    // Return the relevant pool, based on the requested GameObject
    private static ObjectPool GetObjectPool(RecycleGameObject reference)
    {
        ObjectPool pool = null;

        // Check dictionary key exists, if not the dictionary will throw error
        if (pools.ContainsKey(reference))
        {
            // Get dict key
            pool = pools[reference];
        }
        else     // Pool doesn't exist yet, create it
        // Create a container GameObject to hold the pool
        {
            var poolContainer = new GameObject(reference.gameObject.name + "ObjectPool");
            pool        = poolContainer.AddComponent <ObjectPool>();
            pool.prefab = reference;
            pools.Add(reference, pool);
        }

        return(pool);
    }
コード例 #22
0
    /// <summary>
    /// 根据可复用对象引用获取相应的可复用对象池
    /// </summary>
    private static ObjectPool GetObjectPool(RecycleGameObject reference)
    {
        ObjectPool pool = null;

        if (pools.ContainsKey(reference))
        {
            pool = pools[reference];
        }
        else
        {
            // 如果该可复用对象未有相应的对象池,创建之
            GameObject poolContainer = new GameObject(reference.gameObject.name + "ObjectPool");

            pool        = poolContainer.AddComponent <ObjectPool>();
            pool.prefab = reference;

            // 把新的对象池添加到字典容器中
            pools.Add(reference, pool);
        }

        return(pool);
    }
コード例 #23
0
    // get an obstacle and add it to the object pool
    private static ObjectPool GetObjectPool(RecycleGameObject reference)
    {
        ObjectPool pool = null;

        // code to access the dictionary

        if (pools.ContainsKey(reference))
        {
            // key/value pair
            pool = pools[reference];
        }
        else
        {
            // if there is not pool then create it from scratch
            var poolContainer = new GameObject(reference.gameObject.name + "ObjectPool");
            pool        = poolContainer.AddComponent <ObjectPool> ();
            pool.prefab = reference;
            pools.Add(reference, pool);
        }
        // return pool instance from dictionary or that was created from scratch
        return(pool);
    }
コード例 #24
0
ファイル: ObjectPool.cs プロジェクト: phulsechinmay/ZRunner
    public RecycleGameObject NextObject(Vector3 pos)
    {
        RecycleGameObject nextObj = null;

        foreach (var obj in instanceList)
        {
            if (obj.gameObject.activeSelf != true)
            {
                nextObj = obj;
                nextObj.transform.position = pos;
            }
        }

        if (nextObj == null)
        {
            nextObj = CreateInstance(pos);
        }

        nextObj.Restart();

        return(nextObj);
    }
コード例 #25
0
    /// <summary>
    /// 从列表容器中获取下一个可复用对象实例,并且重置它
    /// </summary>
    public RecycleGameObject NextObject(Vector3 position)
    {
        RecycleGameObject instance = null;

        foreach (RecycleGameObject recycleGameObject in instances)
        {
            if (recycleGameObject.gameObject.activeSelf != true)
            {
                instance = recycleGameObject;
                instance.transform.position = position;
            }
        }

        if (null == instance)
        {
            instance = CreateInstance(position);
        }

        instance.Restart();

        return(instance);
    }
コード例 #26
0
    public RecycleGameObject NextObject(Vector3 position)
    {
        RecycleGameObject instance = null;

        foreach (var go in poolInstances)
        {
            // test each to see if it's set to false
            if (go.gameObject.activeSelf != true)
            {
                instance = go;
                instance.transform.position = position;
            }
        }

        // If we haven't found one that is inactive, create a new one
        if (instance == null)
        {
            instance = CreateInstance(position);
        }

        instance.Restart();
        return(instance);
    }
コード例 #27
0
    // returns an object pool from the pools dictionary based on the type of recycled game object referenced, assuming the dictionary contains it
    private static ObjectPool GetObjectPool(RecycleGameObject reference)    // NOTE: the reference here is actually (ex.) "Ostacles (RecycleGameObject)" which is how the type of RGO is differentiated
    {
        ObjectPool pool = null;

        if (pools.ContainsKey(reference))
        {
            // pool will store the value (an object pool) associated with the reference (recycle game object) key
            pool = pools[reference];
        }
        else
        {
            // if pool does not already exist in dictionary, we create a new one in a container and give it a name based on the game object type referenced
            var poolContainer = new GameObject(reference.gameObject.name + "ObjectPool");
            // adds ObjectPool script component to the new container
            pool = poolContainer.AddComponent <ObjectPool>();
            // set pool prefab property to reference so that the pool identifies what type of objects to contain
            pool.prefab = reference;
            // add to pool dictionary
            pools.Add(reference, pool);
        }

        return(pool);
    }
コード例 #28
0
    public RecycleGameObject NextObject(Vector3 pos)
    {
        RecycleGameObject instance = null;

        // Go through list of deactivated GameObjects and return one of those if there is one
        foreach (var go in poolInstances)
        {
            if (go.gameObject.activeSelf != true)   // Found deactivated GameObject in pool
            {
                instance = go;
                instance.transform.position = pos;
            }
        }

        // Not found deactivated GameObject to reuse in pool, so create a new GameObject
        if (instance == null)
        {
            instance = CreateInstance(pos);
        }

        instance.Restart();
        return(instance);
    }
コード例 #29
0
    //return instance when we need it
    public RecycleGameObject NextObject(Vector3 pos)
    {
        RecycleGameObject instance = null;

        //create a loop that will go through and check which objects can be reused

        foreach (var go in poolInstances)
        {
            if (go.gameObject.activeSelf != true)
            {
                instance = go;
                instance.transform.position = pos;
            }
        }
        //create new instance if there are no recycled objects in the pool
        if (instance == null)
        {
            instance = CreateInstance(pos);
        }
        //call restart method to reload object
        instance.Restart();

        return(instance);
    }
コード例 #30
0
    public RecycleGameObject NextObject(Vector3 pos)
    {
        RecycleGameObject instance = null;

        foreach (var go in poolInstances)   //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ why does this cycle through every item instead of breaking out after first success?
        {
            if (go.gameObject.activeSelf != true)
            {
                instance = go;
                instance.transform.position = pos;
                // break here??? why do we need to cycle through each if we've already found a match?
            }
        }

        if (instance == null)
        {
            instance = CreateInstance(pos);
        }


        instance.Restart();

        return(instance);
    }
コード例 #31
0
    //return next object in pool
    public RecycleGameObject NextObject(Vector3 pos)
    {
        RecycleGameObject instance = null;

        //recycle game objects that already exist but are disabled
        foreach (var go in poolInstances)
        {
            if (go.gameObject.activeSelf != true)
            {
                instance = go;
                instance.transform.position = pos;
            }
        }

        //create a new instance if the game object does not exist
        if (instance == null)
        {
            instance = createInstance(pos);
        }

        instance.Restart();

        return(instance);
    }