Example #1
0
    private void AddInstancesForType(CardType type, int num)
    {
        TypeCardsPool pool   = _subpools.SingleOrDefault(x => x.Type == type);
        GameObject    prefab = GetCardPrefab(type);

        for (int i = 0; i < num; i++)
        {
            GameObject go = Instantiate(prefab, transform);
            go.SetActive(false);
            pool.Free.Add(go);
        }
    }
Example #2
0
 private void Init()
 {
     if (!_initialized)
     {
         foreach (CardPrefab cp in _prefabs)
         {
             TypeCardsPool pool = new TypeCardsPool();
             pool.Type  = cp.type;
             pool.Free  = new List <GameObject>();
             pool.InUse = new List <GameObject>();
             _subpools.Add(pool);
         }
         _initialized = true;
     }
 }
Example #3
0
    public void ReturnInstance(CardType type, GameObject instance)
    {
        Init();
        TypeCardsPool pool = _subpools.SingleOrDefault(x => x.Type == type);

        if (pool == null)
        {
            pool = _subpools.SingleOrDefault(x => x.Type == type.Parent);
        }
        if (pool != null)
        {
            pool.InUse.Remove(instance);
            pool.Free.Add(instance);
            instance.SetActive(false);
        }
    }
Example #4
0
    public List <GameObject> GetInstances(CardType type, int num)
    {
        Init();
        List <GameObject> instances = new List <GameObject>();
        TypeCardsPool     pool      = _subpools.SingleOrDefault(x => x.Type == type);

        if (pool == null)
        {
            pool = _subpools.SingleOrDefault(x => x.Type == type.Parent);
        }
        if (pool != null)
        {
            int diff = num - pool.Free.Count;
            if (diff > 0)
            {
                AddInstancesForType(pool.Type, diff);
                // indices = Enumerable.Range(0, pool.Free.Count).ToList();
                instances = pool.Free;
                pool.InUse.AddRange(instances);
                pool.Free = new List <GameObject>();
            }
            else
            {
                instances = new List <GameObject>();
                List <int> indices = new List <int>();
                for (int i = 0; i < pool.Free.Count; i++)
                {
                    if (indices.Count < num)
                    {
                        instances.Add(pool.Free[i]);
                        indices.Add(i);
                    }
                }
                foreach (int i in indices.OrderByDescending(x => x))
                {
                    pool.InUse.Add(pool.Free[i]);
                    pool.Free.RemoveAt(i);
                }
            }
        }
        // foreach (GameObject go in instances)
        //     go.SetActive(true);
        return(instances);
    }
Example #5
0
    public GameObject GetInstance(CardType type)
    {
        Init();
        GameObject    instance = null;
        TypeCardsPool pool     = _subpools.SingleOrDefault(x => x.Type == type);

        if (pool == null)
        {
            pool = _subpools.SingleOrDefault(x => x.Type == type.Parent);
        }
        if (pool != null)
        {
            if (pool.Free.Count == 0)
            {
                AddInstancesForType(pool.Type, 1);
            }
            instance = pool.Free[0];
            pool.Free.RemoveAt(0);
            pool.InUse.Add(instance);
            // instance.SetActive(true);
        }
        return(instance);
    }