Skip to content

Commit 04756fd

Browse files
committed
Improving pool and fixing minor leaks
1 parent 5fdd594 commit 04756fd

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

Assets/CustomTools/ObjectPooling/Scripts/ObjectPool/IPooledObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
public interface IPooledObject
22
{
3+
PooledObjectType PoolType { get; set; }
34
void Init();
45
void OnObjectSpawn();
56
void OnObjectDespawn();

Assets/CustomTools/ObjectPooling/Scripts/ObjectPool/ObjectPooler.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using UnityEngine;
43

54
public class ObjectPooler : MonoBehaviour
@@ -39,12 +38,14 @@ private void Start()
3938
{
4039
GameObject obj = Instantiate(Pool[j].Prefab);
4140
obj.transform.parent = poolSpecifiMaster.transform;
42-
43-
if (obj.GetComponent<IPooledObject>() == null)
41+
IPooledObject iPool = obj.GetComponent<IPooledObject>();
42+
if (iPool == null)
4443
{
4544
PooledObject temp = obj.AddComponent<PooledObject>();
46-
temp.Type = Pool[j].Tag;
45+
iPool = temp;
4746
}
47+
iPool.PoolType = Pool[j].Tag;
48+
4849

4950
obj.SetActive(false);
5051
objectPool.Enqueue(obj);
@@ -66,14 +67,15 @@ public GameObject SpawnFromPool(PooledObjectType tag, Vector3 pos, Quaternion ro
6667

6768
GameObject objToSpawn;
6869

69-
if(PoolDictionary[tag].Count!=0)
70+
if (PoolDictionary[tag].Count != 0)
7071
{
7172
objToSpawn = PoolDictionary[tag].Peek();
7273
objToSpawn.SetActive(true);
7374
objToSpawn.transform.position = pos;
7475
objToSpawn.transform.rotation = rot;
7576

7677
IPooledObject iPooledObj = objToSpawn.GetComponent<IPooledObject>();
78+
7779
iPooledObj.Init();
7880
iPooledObj.OnObjectSpawn();
7981

@@ -87,7 +89,7 @@ public GameObject SpawnFromPool(PooledObjectType tag, Vector3 pos, Quaternion ro
8789
return objToSpawn;
8890
}
8991

90-
public void Despawn(PooledObjectType tag,GameObject obj)
92+
public void Despawn(PooledObjectType tag, GameObject obj)
9193
{
9294

9395
PoolDictionary[tag].Enqueue(obj);
@@ -108,12 +110,15 @@ private GameObject ExpandPool(PooledObjectType tag, Vector3 pos, Quaternion rot)
108110
temp.transform.position = pos;
109111
temp.transform.rotation = rot;
110112

111-
if (temp.GetComponent<IPooledObject>() == null)
113+
IPooledObject iPool = temp.GetComponent<IPooledObject>();
114+
if (iPool == null)
112115
{
113116
PooledObject tempPool = temp.AddComponent<PooledObject>();
114-
tempPool.Type = tag;
117+
iPool = tempPool;
115118
}
116119

120+
iPool.PoolType = tag;
121+
117122
IPooledObject iPooledObj = temp.GetComponent<IPooledObject>();
118123
iPooledObj.Init();
119124
iPooledObj.OnObjectSpawn();

Assets/CustomTools/ObjectPooling/Scripts/ObjectPool/PooledObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class PooledObject : MonoBehaviour, IPooledObject
44
{
55

66

7-
public PooledObjectType Type;
7+
public PooledObjectType PoolType { get; set; }
88

99

1010

@@ -25,7 +25,7 @@ public virtual void Init()
2525

2626
public void Despawn()
2727
{
28-
ObjectPooler.Instance.Despawn(Type, gameObject);
28+
ObjectPooler.Instance.Despawn(PoolType, gameObject);
2929
}
3030

3131

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
public enum PooledObjectType
22
{
3-
Cube=0,
4-
Sphere=10,
5-
CubeAndSphere=20
3+
Type1 = 0,
4+
Type2 = 10,
5+
Type3 = 20
66
}

ProjectSettings/ProjectVersion.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
m_EditorVersion: 2019.2.16f1
2-
m_EditorVersionWithRevision: 2019.2.16f1 (b9898e2d04a4)
1+
m_EditorVersion: 2019.2.21f1
2+
m_EditorVersionWithRevision: 2019.2.21f1 (9d528d026557)

0 commit comments

Comments
 (0)