1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 namespace Poolspace
6 {
7 public abstract class PoolBase<T> : IEnumerable<T>
8 {
9 public List<T> inactiveObjectsPool { get; protected set; }
10 public List<T> activeObjectsPool { get; protected set; }
11 public Transform rootObject { get; protected set; }
12 public T baseObject { get; protected set; }
13 public int startSize { get; protected set; }
14
15 protected PoolBase(string _PoolName,T baseObject, int startSize = 32,Transform Path=null)
16 {
17 this.baseObject = baseObject;
18 this.startSize = startSize;
19 this.activeObjectsPool = new List<T>(startSize);
20 inactiveObjectsPool = new List<T>(startSize);
21
22
23 rootObject = Path ? Path : new GameObject(_PoolName).transform;
24 Init();
25 }
26
27 private void Init()
28 {
29 for (int i = 0; i < startSize; i++)
30 {
31 Instantiate();
32 }
33 }
34
35 public abstract T Instantiate();
36
37 public abstract T Get(bool createWhenNoneLeft = true);
38
39 public abstract void Destroy(T item);
40
41 public void DestroyAll()
42 {
43 int num = 0;
44 while (activeObjectsPool.Count > 0 && num++ < startSize)
45 Destroy(activeObjectsPool[0]);
46 }
47
48 public IEnumerator<T> GetEnumerator()
49 {
50 return activeObjectsPool.GetEnumerator();
51 }
52
53 IEnumerator IEnumerable.GetEnumerator()
54 {
55 return GetEnumerator();
56 }
57
58
59 }
60
61 }