Example #1
0
 public void Dispose(IPooled pooled)
 {
     if (activeObjects.Contains(pooled))
     {
         activeObjects.Remove(pooled);
     }
 }
Example #2
0
    public List <GameObject> SpawnEntirePool(string _tag, Queue <Vector3> _positions, Queue <Quaternion> _rotations)
    {
        List <GameObject> objectsToSpawn = new List <GameObject>();

        if (!poolDictionary.ContainsKey(_tag))
        {
            Debug.Log("Object with tag " + _tag + " doesn't exist");
            return(null);
        }

        foreach (GameObject objectToSpawn in poolDictionary[_tag])
        {
            objectToSpawn.transform.localPosition = _positions.Dequeue();
            objectToSpawn.transform.localRotation = _rotations.Dequeue();

            objectToSpawn.SetActive(true);

            IPooled pooledObject = objectToSpawn.GetComponent <IPooled>();

            if (pooledObject != null)
            {
                pooledObject.OnObjectSpawn();
            }

            objectsToSpawn.Add(objectToSpawn);
        }

        return(objectsToSpawn);
    }
Example #3
0
    public GameObject SpawnPoolObject(string _tag, Vector3 _position, Quaternion _rotation)
    {
        if (!poolDictionary.ContainsKey(_tag))
        {
            Debug.Log("Object with tag " + _tag + " doesn't exist");
            return(null);
        }

        GameObject objectToSpawn = poolDictionary[_tag].Dequeue();

        objectToSpawn.transform.localPosition = _position;
        objectToSpawn.transform.localRotation = _rotation;

        objectToSpawn.SetActive(true);

        IPooled pooledObject = objectToSpawn.GetComponent <IPooled>();

        if (pooledObject != null)
        {
            pooledObject.OnObjectSpawn();
        }

        poolDictionary[_tag].Enqueue(objectToSpawn);

        return(objectToSpawn);
    }
Example #4
0
 public void Return(IPooled pooledObject)
 {
     if (activeItems.Remove(pooledObject))
     {
         poolStack.Push(pooledObject);
     }
 }
Example #5
0
 public void ReturnAll()
 {
     for (int i = activeObjects.Count - 1; i >= 0; i--)
     {
         IPooled pooled = activeObjects[i] as IPooled;
         pooled.ReturnToPool();
     }
 }
Example #6
0
        public void Return(IPooled pooledObject)
        {
            P item = pooledObject as P;

            instances.Push(item);
            activeObjects.Remove(item);
            OnItemReturnedToPool(item);
        }
Example #7
0
        private void AddInstanceToPool()
        {
            IPooled item = createInstanceHandler();

            item.Init(this);
            activeItems.Add(item);
            item.ReturnToPool();
        }
Example #8
0
 public ConnectToken(CancellationToken cancellation, Action <ConnectToken, SocketAsyncEventArgs> cancel, IPooled <SocketAsyncEventArgs> pooled)
 {
     _pooled = pooled;
     _tcs    = new TaskCompletionSource <ISocket>();
     if (cancellation.CanBeCanceled)
     {
         _cancellationTokenRegistration = cancellation.Register(() => cancel(this, pooled.Instance));
     }
 }
Example #9
0
    public void DestroyPool(GameObject spawned)
    {
        IPooled pooledObj = spawned.GetComponent <IPooled>();

        if (pooledObj != null)
        {
            pooledObj.OnUnSpawn();
        }
        spawned.transform.SetParent(transform);
        spawned.SetActive(false);
    }
Example #10
0
        public IPooled Take()
        {
            if (poolStack.Count == 0)
            {
                AddInstanceToPool();
            }
            IPooled item = poolStack.Pop();

            item.TakeFromPool();
            activeItems.Add(item);
            return(item);
        }
Example #11
0
 /// <summary>
 /// Recycles an object to the object pool
 /// </summary>
 /// <param name="InstanceId">Instance id of the object</param>
 public void Recycle(GameObject go)
 {
     if (!PooledObjects.Contains(go) && PooledObjects.Count < MaxItems)
     {
         IPooled ip = go.GetComponent <IPooled>();
         if (ip != null)
         {
             ip.OnRecycle();
         }
         go.SetActive(false);
         go.transform.position = Vector3.zero;
         PooledObjects.Push(go);
     }
 }
Example #12
0
        /// <summary>
        /// Returns a GameObject from object pool and enables it
        /// </summary>
        /// <param name="pos">New Position</param>
        /// <param name="rot">New Rotation</param>
        private GameObject GetFromPool(Vector3 pos, Quaternion rot)
        {
            if (PooledObjects.Count > 0)
            {
                GameObject g = PooledObjects.Pop();
                g.transform.position = pos;
                g.transform.rotation = rot;
                g.SetActive(true);


                IPooled ip = g.GetComponent <IPooled>();
                if (ip != null)
                {
                    ip.OnSpawn();
                }
                return(g);
            }
            return(null);
        }
Example #13
0
    /// <summary>
    /// Use thid method to instantiate a gameobject using a Gameobject reference
    /// </summary>
    /// <param name="obj">  PREFAB </param>
    /// <param name="pos">  Position    </param>
    /// <param name="rot">  Rotation    </param>
    /// <returns></returns>
    private GameObject InstantiatePool(GameObject obj, Vector3 pos, Quaternion rot)
    {
        if (!m_poolObjects.ContainsKey(obj.name))
        {
            CreatePool(obj);
        }
        foreach (GameObject poolObj in m_poolObjects[obj.name])
        {
            if (poolObj == null)
            {
                continue;
            }
            if (poolObj.activeSelf == false)
            {
                poolObj.transform.position = pos;
                poolObj.transform.rotation = rot;
                IPooled pooledObj = poolObj.GetComponent <IPooled>();
                if (pooledObj != null)
                {
                    pooledObj.OnInstantiate();
                }
                poolObj.SetActive(true);
                return(poolObj);
            }
        }


        GameObject currentObj = Instantiate(obj) as GameObject;

        currentObj.transform.position = pos;
        currentObj.transform.rotation = rot;
        currentObj.name = obj.name;
        currentObj.SetActive(true);
        IPooled newpooledObj = currentObj.GetComponent <IPooled>();

        if (newpooledObj != null)
        {
            newpooledObj.OnInstantiate();
        }
        m_poolObjects[obj.name].Add(currentObj);
        return(currentObj);
    }
Example #14
0
 public GpuRawFrame(FrameInfo info, IPooled<ITexture2D> texturePooled)
 {
     Info = info;
     TexturePooled = texturePooled;
 }
Example #15
0
 public PooledToken(IPooled <SocketAsyncEventArgs> pooled)
 {
     _pooled = pooled;
     TaskCompletionSource       = new TaskCompletionSource <T>();
     _pooled.Instance.UserToken = this;
 }
Example #16
0
 public CompressedFrame(IPooled<byte[]> data, int compressedSize)
 {
     DataPooled = data;
     CompressedSize = compressedSize;
 }
Example #17
0
 public void Return(IPooled pooledObject)
 {
     instances.Push(pooledObject as P);
 }
Example #18
0
 public void Dispose(IPooled pooled)
 {
     activeObjects.RemoveAll(o => o == (P)pooled);
 }