Advertisement
Crazist

Untitled

Aug 9th, 2022
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5.  
  6. namespace GameInit.PoolOfCoins
  7. {
  8.     public class CoinsPool : MonoBehaviour
  9.     {
  10.         [SerializeField] private GameObject prefabCoin;
  11.  
  12.         [Space(10)] [SerializeField] private Transform _container;
  13.         [SerializeField] private int _minCapacity;
  14.         [SerializeField] private int _maxCapacity;
  15.         [Space(10)] [SerializeField] private bool _isExpand;
  16.  
  17.         public List<GameObject> _pool { get; private set; }
  18.         private void OnExpand()
  19.         {
  20.             if (_isExpand)
  21.             {
  22.                 _maxCapacity = Int32.MaxValue;
  23.             }
  24.         }
  25.  
  26.         public void CreatePool()
  27.         {
  28.             _pool = new List<GameObject>();
  29.  
  30.             for (int i = 0; i < _minCapacity; i++)
  31.             {
  32.                 CreateObj();
  33.             }
  34.         }
  35.         private GameObject CreateObj(bool isActibebydefault = false)
  36.         {
  37.             var createdObj = Instantiate(prefabCoin, _container);
  38.             createdObj.gameObject.SetActive(isActibebydefault);
  39.  
  40.             _pool.Add(createdObj);
  41.  
  42.             return createdObj;
  43.         }
  44.         public bool TryGetElement(out GameObject gameobj)
  45.         {
  46.             foreach (var item in _pool)
  47.             {
  48.                 if (!item.gameObject.activeInHierarchy)
  49.                 {
  50.                     gameobj = item;
  51.                     item.gameObject.SetActive(true);
  52.                     return true;
  53.                 }
  54.             }
  55.             gameobj = null;
  56.             return false;
  57.         }
  58.         public bool TryGetEngagedElement(out List<GameObject> _list)
  59.         {
  60.             _list = new List<GameObject>();
  61.             bool isNotEmpty = false;
  62.             foreach (var item in _pool)
  63.             {
  64.                 if (item.gameObject.activeInHierarchy)
  65.                 {
  66.                     _list.Add(item);
  67.                     isNotEmpty = true;
  68.                 }
  69.             }
  70.            
  71.             return isNotEmpty;
  72.         }
  73.         public GameObject GetFreeElements(Vector3 pos, Quaternion rotation)
  74.         {
  75.             var obj = GetFreeElements(pos);
  76.             obj.transform.rotation = rotation;
  77.             return obj;
  78.         }
  79.         public GameObject GetFreeElements(Vector3 pos)
  80.         {
  81.             var obj = GetFreeElements();
  82.             obj.transform.position = pos;
  83.             return obj;
  84.         }
  85.         public GameObject GetClosestEngagedElements(Vector3 pos)
  86.         {
  87.             var obj = GetEngagedElements();
  88.             GameObject closestObj = null;
  89.             for (int i = 0; i < obj.Count - 1; i++)
  90.             {
  91.                 if(Vector3.Distance(pos, obj[i].transform.position) < Vector3.Distance(pos, obj[i + 1].transform.position))
  92.                 {
  93.                     closestObj = obj[i];
  94.                 }
  95.                 else
  96.                 {
  97.                     closestObj = obj[i + 1];
  98.                 }
  99.             }
  100.             if (obj.Count - 1 == 0 && obj.Count != 0) return obj[0];
  101.             return closestObj;
  102.         }
  103.         public GameObject GetFreeElements()
  104.         {
  105.             if(TryGetElement(out var gameObj))
  106.             {
  107.                 return gameObj;
  108.             }
  109.             if (_isExpand)
  110.             {
  111.                 return CreateObj(true);
  112.             }
  113.  
  114.             if(_pool.Count < _maxCapacity)
  115.             {
  116.                 return CreateObj(true);
  117.             }
  118.             throw new Exception("Pool is over!");
  119.         }
  120.         public List<GameObject> GetEngagedElements()
  121.         {
  122.             if (TryGetEngagedElement(out var gameObj))
  123.             {
  124.                 return gameObj;
  125.             }
  126.             throw new Exception("Pool is over!");
  127.         }
  128.     }
  129. }
  130.  
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement