コード例 #1
0
    public PoolableBehaviour GetObjectFromPool(PoolableBehaviour prefab)
    {
        int ID = prefab.GetInstanceID();

        // If there is no key in dic
        if (!objectPool.ContainsKey(ID))
        {
            CreatePool(prefab, _InitialPoolSize);
        }

        List <PoolableBehaviour> objectPoolList = objectPool[ID];

        for (int i = 0; i < objectPoolList.Count; ++i)
        {
            IncreaseIndexPool(ID);

            // check if it is active or not
            if (!objectPoolList[indexPool[ID]].gameObject.activeInHierarchy)
            {
                PoolableBehaviour obj = objectPoolList[indexPool[ID]];
                obj.gameObject.SetActive(true);
                return(obj);
            }
        }

        PoolableBehaviour tmp = AddObjectToPool(ID, prefab);

        tmp.gameObject.SetActive(true);
        return(tmp);
    }
コード例 #2
0
ファイル: PoolManager.cs プロジェクト: jwinder86/ArenaShooter
    // Get a pooled instance of the specified prefab, creating pool or instances as necessary
    public PoolableBehaviour GetPooledObject(PoolableBehaviour prefab) {
        if (!poolMap.ContainsKey(prefab)) {
            poolMap[prefab] = new ObjectPool(prefab, defaultPoolSize);
            Debug.LogWarning("Creating pool for uninitialized prefab: " + prefab + " with initial size of " + defaultPoolSize);
        }

        return poolMap[prefab].GetFromPool();
    }
コード例 #3
0
    private PoolableBehaviour AddObjectToPool(int ID, PoolableBehaviour prefab)
    {
        PoolableBehaviour obj = Instantiate(prefab);

        obj.gameObject.SetActive(false);
        objectPool[ID].Add(obj);

        return(obj);
    }
コード例 #4
0
    // Get a pooled instance of the specified prefab, creating pool or instances as necessary
    public PoolableBehaviour GetPooledObject(PoolableBehaviour prefab)
    {
        if (!poolMap.ContainsKey(prefab))
        {
            poolMap[prefab] = new ObjectPool(prefab, defaultPoolSize);
            Debug.LogWarning("Creating pool for uninitialized prefab: " + prefab + " with initial size of " + defaultPoolSize);
        }

        return(poolMap[prefab].GetFromPool());
    }
コード例 #5
0
        public void testAutoPoolManagerCreation()
        {
            PoolableBehaviour prefab = RuntimeTestHelpers.CreatePrefabWithComponent <TestablePoolableBehaviour>("Test Poolable");

            // request object without pre-creating PoolManager
            PoolableBehaviour obj = PoolManager.Instance.GetPooledObject(prefab);

            Assert.NotNull(obj, "Retrieved null object from pool");
            Assert.AreNotEqual(prefab, obj, "Received prefab itself from pool");
        }
コード例 #6
0
    // create a single object and configure for this pool
    private PoolableBehaviour createObject()
    {
        totalCreated++;
        PoolableBehaviour created = (PoolableBehaviour)Object.Instantiate(prefab);

        created.transform.parent = parent;
        created.SetOwner(this);
        created.Deactivate();
        return(created);
    }
コード例 #7
0
        public void testEmptyPool()
        {
            PoolableBehaviour prefab = RuntimeTestHelpers.CreatePrefabWithComponent <TestablePoolableBehaviour>("Test Poolable");
            int        initialSize   = 0;
            int        incrementSize = 1;
            ObjectPool pool          = new ObjectPool(prefab, initialSize, incrementSize);

            PoolableBehaviour retrieved = pool.GetFromPool();

            Assert.IsNotNull(retrieved);
        }
コード例 #8
0
 /// <summary>
 ///  Make the tentacle length shorter
 /// </summary>
 /// <param name="times">Define how short it should be</param>
 public void Shorten(int times)
 {
     //  print("shortening " + times + " times");
     for (int i = 0; i < times; i++)
     {
         if (armParts.Count > 1)
         {
             //   print("destroying arm segment");
             PoolableBehaviour target = armParts[armParts.Count - 1];
             armParts.Remove(target);
             target.SetActive(false);
         }
     }
 }
コード例 #9
0
    public void CreatePool(PoolableBehaviour prefab, int poolSize)
    {
        // different for each instance
        int ID = prefab.GetInstanceID();

        objectPool[ID] = new List <PoolableBehaviour>();
        indexPool[ID]  = 0;

        for (int i = 0; i < poolSize; ++i)
        {
            PoolableBehaviour obj = Instantiate(prefab);
            obj.gameObject.SetActive(false);
            objectPool[ID].Add(obj);
        }
    }
コード例 #10
0
    /// <summary>
    ///  Spawn the tentacle parts
    /// </summary>
    /// <param name="spawnPoint">Tentacle root location</param>
    /// <param name="direction">Direction of the arm</param>
    private void Extend(Transform spawnPoint, Vector3 direction)
    {
        if (!autoExtend)
        {
            return;
        }

        //print("tenatcle Extend");
        //TentacleSegment currentSegment = Instantiate(armPrefab, GetSegmentLocation(spawnPoint.position, direction), spawnPoint.rotation);

        PoolableBehaviour currentSegment = PoolManager.instance.GetObjectFromPool(armPrefab);

        // reset the position of the obj
        currentSegment.transform.position = Vector3.zero;

        armParts.Add(currentSegment);
        (currentSegment as TentacleSegment).rootTentacle = this;
    }
コード例 #11
0
ファイル: ObjectPool.cs プロジェクト: jwinder86/GJGDCJam
    public ObjectPool(PoolableBehaviour prefab, int initialSize, int incrementSize) {
        this.prefab = prefab;
        if (incrementSize < 1) {
            this.incrementSize = 1;
            Debug.LogError("Non-positive increment requested, defaulting to increment size of 1");
        } else {
            this.incrementSize = incrementSize;
        }
        if (initialSize < 0) {
            initialSize = 0;
            Debug.LogError("Negative initial size requested, defaulting to size of 0");
        }

        pool = new Stack<PoolableBehaviour>(initialSize);
        totalCreated = 0;

        CreateAndAddToPool(initialSize);
    }
コード例 #12
0
        public void testPoolReuse()
        {
            PoolableBehaviour prefab = RuntimeTestHelpers.CreatePrefabWithComponent <TestablePoolableBehaviour>("Test Poolable");
            ObjectPool        pool   = new ObjectPool(prefab, 1, 1);

            // pool should never return the prefab itself
            PoolableBehaviour retrieved = pool.GetFromPool();

            Assert.AreNotEqual(prefab, retrieved, "Prefab itself returned from pool");

            // pool should not return an in-use object
            PoolableBehaviour retrieved2 = pool.GetFromPool();

            Assert.AreNotEqual(retrieved, retrieved2, "Pool returned in-use object twice");

            // object returned to pool should be retrieved from pool
            retrieved.ReturnToPool();
            PoolableBehaviour retrieved3 = pool.GetFromPool();

            Assert.AreEqual(retrieved, retrieved3, "Pool did not re-use returned object");
        }
コード例 #13
0
    public ObjectPool(PoolableBehaviour prefab, int initialSize, int incrementSize)
    {
        this.prefab = prefab;
        if (incrementSize < 1)
        {
            this.incrementSize = 1;
            Debug.LogError("Non-positive increment requested, defaulting to increment size of 1");
        }
        else
        {
            this.incrementSize = incrementSize;
        }
        if (initialSize < 0)
        {
            initialSize = 0;
            Debug.LogError("Negative initial size requested, defaulting to size of 0");
        }

        pool         = new Stack <PoolableBehaviour>(initialSize);
        totalCreated = 0;

        CreateAndAddToPool(initialSize);
    }
コード例 #14
0
 public void Return(PoolableBehaviour c)
 {
     c.gameObject.SetActive(false);
     objectStack.Push((T)c);
 }
コード例 #15
0
 public ObjectPool(PoolableBehaviour prefab, int initialSize) : this(prefab, initialSize, initialSize)
 {
 }
コード例 #16
0
ファイル: ObjectPool.cs プロジェクト: jwinder86/ArenaShooter
 // return an object to this pool
 public void ReturnToPool(PoolableBehaviour obj) {
     pool.Push(obj);
     //Debug.Log("Created new object");
 }
コード例 #17
0
ファイル: ObjectPool.cs プロジェクト: jwinder86/ArenaShooter
 public ObjectPool(PoolableBehaviour prefab, int initialSize) : this(prefab, initialSize, initialSize) { }
コード例 #18
0
 // return an object to this pool
 public void ReturnToPool(PoolableBehaviour obj)
 {
     pool.Push(obj);
     //Debug.Log("Created new object");
 }