Skip to content

Commit cd306b4

Browse files
committed
Pool expands itself in case.
1 parent 18a6098 commit cd306b4

File tree

6 files changed

+116
-123
lines changed

6 files changed

+116
-123
lines changed

Assets/Scenes/Main.unity

Lines changed: 6 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Transform:
149149
m_LocalScale: {x: 1, y: 1, z: 1}
150150
m_Children: []
151151
m_Father: {fileID: 0}
152-
m_RootOrder: 5
152+
m_RootOrder: 4
153153
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
154154
--- !u!114 &31903662
155155
MonoBehaviour:
@@ -167,15 +167,11 @@ MonoBehaviour:
167167
- Tag: 0
168168
Prefab: {fileID: 7656099617140954774, guid: 28715032db92b744a8e96b5161ec036c,
169169
type: 3}
170-
Size: 200
170+
Size: 10
171171
- Tag: 10
172172
Prefab: {fileID: 5893199740330628399, guid: 0e0865df4c258b44aae355574015c698,
173173
type: 3}
174-
Size: 200
175-
- Tag: 20
176-
Prefab: {fileID: 7759290838618530551, guid: 17571cfc482d06d4484dac8e9d43514f,
177-
type: 3}
178-
Size: 5
174+
Size: 10
179175
--- !u!1 &170076733
180176
GameObject:
181177
m_ObjectHideFlags: 0
@@ -440,87 +436,8 @@ Transform:
440436
m_LocalScale: {x: 20, y: 20, z: 20}
441437
m_Children: []
442438
m_Father: {fileID: 0}
443-
m_RootOrder: 4
439+
m_RootOrder: 3
444440
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
445-
--- !u!1001 &1722596145
446-
PrefabInstance:
447-
m_ObjectHideFlags: 0
448-
serializedVersion: 2
449-
m_Modification:
450-
m_TransformParent: {fileID: 0}
451-
m_Modifications:
452-
- target: {fileID: 2177757300998581467, guid: bd4d1f98f4d30434ba7078f3b5219708,
453-
type: 3}
454-
propertyPath: m_Name
455-
value: SphereSpawner
456-
objectReference: {fileID: 0}
457-
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
458-
type: 3}
459-
propertyPath: m_LocalPosition.x
460-
value: -10
461-
objectReference: {fileID: 0}
462-
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
463-
type: 3}
464-
propertyPath: m_LocalPosition.y
465-
value: 20
466-
objectReference: {fileID: 0}
467-
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
468-
type: 3}
469-
propertyPath: m_LocalPosition.z
470-
value: 0
471-
objectReference: {fileID: 0}
472-
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
473-
type: 3}
474-
propertyPath: m_LocalRotation.x
475-
value: 0
476-
objectReference: {fileID: 0}
477-
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
478-
type: 3}
479-
propertyPath: m_LocalRotation.y
480-
value: 0
481-
objectReference: {fileID: 0}
482-
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
483-
type: 3}
484-
propertyPath: m_LocalRotation.z
485-
value: 0
486-
objectReference: {fileID: 0}
487-
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
488-
type: 3}
489-
propertyPath: m_LocalRotation.w
490-
value: 1
491-
objectReference: {fileID: 0}
492-
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
493-
type: 3}
494-
propertyPath: m_RootOrder
495-
value: 3
496-
objectReference: {fileID: 0}
497-
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
498-
type: 3}
499-
propertyPath: m_LocalEulerAnglesHint.x
500-
value: 0
501-
objectReference: {fileID: 0}
502-
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
503-
type: 3}
504-
propertyPath: m_LocalEulerAnglesHint.y
505-
value: 0
506-
objectReference: {fileID: 0}
507-
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
508-
type: 3}
509-
propertyPath: m_LocalEulerAnglesHint.z
510-
value: 0
511-
objectReference: {fileID: 0}
512-
- target: {fileID: 2177757300998581466, guid: bd4d1f98f4d30434ba7078f3b5219708,
513-
type: 3}
514-
propertyPath: SpawnRate
515-
value: 0.1
516-
objectReference: {fileID: 0}
517-
- target: {fileID: 2177757300998581466, guid: bd4d1f98f4d30434ba7078f3b5219708,
518-
type: 3}
519-
propertyPath: Type
520-
value: 10
521-
objectReference: {fileID: 0}
522-
m_RemovedComponents: []
523-
m_SourcePrefab: {fileID: 100100000, guid: bd4d1f98f4d30434ba7078f3b5219708, type: 3}
524441
--- !u!1001 &2177757301718279969
525442
PrefabInstance:
526443
m_ObjectHideFlags: 0
@@ -536,7 +453,7 @@ PrefabInstance:
536453
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
537454
type: 3}
538455
propertyPath: m_LocalPosition.x
539-
value: 10
456+
value: 23.66
540457
objectReference: {fileID: 0}
541458
- target: {fileID: 2177757300998581469, guid: bd4d1f98f4d30434ba7078f3b5219708,
542459
type: 3}
@@ -591,7 +508,7 @@ PrefabInstance:
591508
- target: {fileID: 2177757300998581466, guid: bd4d1f98f4d30434ba7078f3b5219708,
592509
type: 3}
593510
propertyPath: SpawnRate
594-
value: 0.2
511+
value: 0.5
595512
objectReference: {fileID: 0}
596513
m_RemovedComponents: []
597514
m_SourcePrefab: {fileID: 100100000, guid: bd4d1f98f4d30434ba7078f3b5219708, type: 3}

Assets/Scripts/ObjectPool/ObjectPooler.cs

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,43 @@
44

55
public class ObjectPooler : MonoBehaviour
66
{
7-
87

9-
public Dictionary<string, Queue<GameObject>> PoolDictionary;
8+
public Dictionary<PooledObjectType, Queue<GameObject>> PoolDictionary;
109
public List<PoolObjects> Pool;
1110

12-
#region Singleton
11+
private Dictionary<PooledObjectType, int> _poolIndexes = new Dictionary<PooledObjectType, int>();
12+
private Dictionary<PooledObjectType, Transform> _poolMasters = new Dictionary<PooledObjectType, Transform>();
13+
1314
public static ObjectPooler Instance;
1415

1516
private void Awake()
1617
{
1718
Instance = this;
1819
}
19-
#endregion
2020

21-
// Start is called before the first frame update
22-
void Start()
21+
private void Start()
2322
{
24-
PoolDictionary = new Dictionary<string, Queue<GameObject>>();
23+
PoolDictionary = new Dictionary<PooledObjectType, Queue<GameObject>>();
2524

2625
GameObject master = new GameObject("Pool");
2726

28-
foreach (PoolObjects pool in Pool)
27+
for (int j = 0; j < Pool.Count; j++)
2928
{
30-
GameObject poolSpecifiMaster = new GameObject(pool.Tag.ToString());
29+
GameObject poolSpecifiMaster = new GameObject(Pool[j].Tag.ToString());
3130
poolSpecifiMaster.transform.parent = master.transform;
3231

3332
Queue<GameObject> objectPool = new Queue<GameObject>();
33+
_poolIndexes.Add(Pool[j].Tag, j);
34+
35+
_poolMasters.Add(Pool[j].Tag, poolSpecifiMaster.transform);
36+
3437

35-
for (int i = 0; i < pool.Size; i++)
38+
for (int i = 0; i < Pool[j].Size; i++)
3639
{
37-
GameObject obj = Instantiate(pool.Prefab);
40+
GameObject obj = Instantiate(Pool[j].Prefab);
3841
obj.transform.parent = poolSpecifiMaster.transform;
3942

40-
if(obj.GetComponent<IPooledObject>()==null)
43+
if (obj.GetComponent<IPooledObject>() == null)
4144
{
4245
obj.AddComponent<PooledObject>();
4346
}
@@ -46,36 +49,84 @@ void Start()
4649
objectPool.Enqueue(obj);
4750
}
4851

49-
PoolDictionary.Add(pool.Tag.ToString(), objectPool);
50-
52+
PoolDictionary.Add(Pool[j].Tag, objectPool);
5153
}
54+
5255
}
5356

5457
public GameObject SpawnFromPool(PooledObjectType tag, Vector3 pos, Quaternion rot)
5558
{
56-
if (!PoolDictionary.ContainsKey(tag.ToString())) { Debug.LogWarning("PoolObjects with Tag " + tag + " doesn't exist .."); return null; }
5759

58-
GameObject objToSpawn = PoolDictionary[tag.ToString()].Dequeue();
59-
objToSpawn.SetActive(true);
60-
objToSpawn.transform.position = pos;
61-
objToSpawn.transform.rotation = rot;
60+
if (!PoolDictionary.ContainsKey(tag))
61+
{
62+
Debug.LogWarning("PoolObjects with Tag " + tag + " doesn't exist ..");
63+
return null;
64+
}
65+
66+
GameObject objToSpawn;
67+
68+
69+
if(PoolDictionary[tag].Count!=0)
70+
{
71+
objToSpawn = PoolDictionary[tag].Peek();
72+
objToSpawn.SetActive(true);
73+
objToSpawn.transform.position = pos;
74+
objToSpawn.transform.rotation = rot;
75+
76+
IPooledObject iPooledObj = objToSpawn.GetComponent<IPooledObject>();
77+
iPooledObj.Init();
78+
iPooledObj.OnObjectSpawn();
79+
80+
PoolDictionary[tag].Dequeue();
81+
}
82+
else
83+
{
84+
objToSpawn = ExpandPool(tag, pos, rot);
85+
}
86+
6287

63-
IPooledObject iPooledObj = objToSpawn.GetComponent<IPooledObject>();
64-
iPooledObj.Init();
65-
iPooledObj.OnObjectSpawn();
6688

67-
PoolDictionary[tag.ToString()].Enqueue(objToSpawn);
89+
90+
91+
92+
Debug.Log(PoolDictionary[tag].Count);
93+
6894
return objToSpawn;
6995
}
7096

71-
public void Despawn(PooledObjectType tag)
97+
public void Despawn(PooledObjectType tag,GameObject obj)
98+
{
99+
100+
PoolDictionary[tag].Enqueue(obj);
101+
102+
IPooledObject iPooledObj = obj.GetComponent<IPooledObject>();
103+
if (iPooledObj != null) iPooledObj.OnObjectDespawn();
104+
obj.SetActive(false);
105+
106+
}
107+
108+
private GameObject ExpandPool(PooledObjectType tag, Vector3 pos, Quaternion rot)
72109
{
73-
GameObject objToDespawn = PoolDictionary[tag.ToString()].Dequeue();
74-
objToDespawn.SetActive(false);
75-
objToDespawn.transform.position = Vector3.zero;
76-
IPooledObject iPooledObj = objToDespawn.GetComponent<IPooledObject>();
77-
if (iPooledObj!=null) iPooledObj.OnObjectDespawn();
78-
110+
int index = _poolIndexes[tag];
111+
GameObject temp = Instantiate(Pool[index].Prefab);
112+
temp.SetActive(true);
113+
temp.transform.SetParent(_poolMasters[tag]);
114+
115+
temp.transform.position = pos;
116+
temp.transform.rotation = rot;
117+
118+
IPooledObject iPooledObj = temp.GetComponent<IPooledObject>();
119+
iPooledObj.Init();
120+
iPooledObj.OnObjectSpawn();
121+
122+
123+
PoolDictionary[tag].Enqueue(temp);
124+
125+
Pool[index].Size++;
126+
127+
return temp;
128+
//Pool.Add();
129+
79130
}
80131

81132
}

Assets/Scripts/ObjectPool/PooledObject.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using UnityEngine;
5+
using Random = UnityEngine.Random;
46

57
public class PooledObject : MonoBehaviour, IPooledObject
68
{
9+
710
private Rigidbody _rb;
811

12+
public PooledObjectType Type;
13+
14+
private float _timer = 3f;
15+
16+
917
public virtual void OnObjectSpawn()
1018
{
1119
_rb.velocity = Vector3.zero;
@@ -20,6 +28,7 @@ public virtual void Init()
2028
{
2129
_rb = GetComponent<Rigidbody>();
2230
AddRandomForce();
31+
_timer = 3f;
2332
}
2433

2534
public virtual void AddRandomForce()
@@ -33,4 +42,16 @@ public virtual void AddRandomForce()
3342
_rb.AddForce(random);
3443
}
3544

36-
}
45+
private void Update()
46+
{
47+
if (_timer > 0)
48+
{
49+
_timer -= Time.deltaTime;
50+
}
51+
else
52+
{
53+
ObjectPooler.Instance.Despawn(Type,gameObject);
54+
}
55+
}
56+
57+
}

Assets/Scripts/ObjectPool/PooledObjects.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using UnityEngine;
22

33
[System.Serializable]
4-
public struct PoolObjects
4+
public class PoolObjects
55
{
66
public PooledObjectType Tag;
77
public GameObject Prefab;

Assets/Scripts/PooledObjects/Sphere.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
public class Sphere : PooledObject
66
{
7-
public PooledObjectType Type;
87

98
public override void Init()
109
{

Assets/Scripts/Spawner.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ void Update()
2222
}
2323
else
2424
{
25+
26+
2527
GameObject obj = ObjectPooler.Instance.SpawnFromPool(Type, transform.position, Random.rotation);
28+
2629
obj.GetComponent<IPooledObject>().Init();
2730
obj.GetComponent<IPooledObject>().OnObjectSpawn();
31+
2832
_timer = SpawnRate;
33+
2934
}
3035
}
3136
}

0 commit comments

Comments
 (0)