Ejemplo n.º 1
0
    //Returns first deactivated poolable of chosen type. If none, instanciate one
    public T GetPoolable <T>(Vector3 position, Quaternion rotation, float size = 1f, PoolableType type = PoolableType.None, Transform parent = null, bool defaultParent = true) where T : IPoolable
    {
        T poolable = default(T);

        foreach (var p in Poolables.ToArray())
        {
            if (Utils.IsNull(p))
            {
                Poolables.Remove(p);
                continue;
            }

            if (type != PoolableType.None && p.PoolableType != type)
            {
                continue;
            }

            if (p is T && !((MonoBehaviour)p).gameObject.activeSelf)
            {
                poolable = (T)p;
                break;
            }
        }

        //var poolable = (T)Poolables.Where(type == PoolableType.None || x.PoolableType == type).FirstOrDefault(p => p is T && !((MonoBehaviour)p).gameObject.activeSelf);

        if (poolable != null)
        {
            return(SelectPoolable(poolable, position, rotation, size));
        }

        // Create poolable if doesn't exist
        GameObject poolableModel = null;

        foreach (var model in _poolableModels)
        {
            if (model.TryGetComponent(out IPoolable pool) && pool is T && (type == PoolableType.None || pool.PoolableType == type))
            {
                poolableModel = model;
                break;
            }
        }

        if (poolableModel == null)
        {
            return(default);
Ejemplo n.º 2
0
 public T Alloc <T>(Poolables what, bool activate = true) where T : Component
 {
     return(Alloc <T>(PoolableUtil.ToFileName(what), activate));
 }