Skip to content

Commit e422831

Browse files
committed
Added Unitys native object pool implementation. Updated readme. Refactored object pooling code
1 parent b55896e commit e422831

32 files changed

+808
-97
lines changed
File renamed without changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace ObjectPool.Gun
6+
{
7+
//Parent bullet class to avoid code duplication
8+
public class BulletBase : MonoBehaviour
9+
{
10+
private readonly float bulletSpeed = 10f;
11+
12+
private readonly float deactivationDistance = 30f;
13+
14+
15+
16+
protected void MoveBullet()
17+
{
18+
transform.Translate(bulletSpeed * Time.deltaTime * Vector3.forward);
19+
}
20+
21+
22+
23+
protected bool IsBulletDead()
24+
{
25+
bool isDead = false;
26+
27+
//The gun is at 0
28+
if (Vector3.SqrMagnitude(Vector3.zero - transform.position) > deactivationDistance * deactivationDistance)
29+
{
30+
isDead = true;
31+
}
32+
33+
return isDead;
34+
}
35+
}
36+
}

Assets/Patterns/18. Object Pool/Gun/Object pools/BulletBase.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Patterns/18. Object Pool/Gun/Scripts/GunController.cs renamed to Assets/Patterns/18. Object Pool/Gun/Object pools/GunController.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44

5+
56
namespace ObjectPool.Gun
67
{
78
public class GunController : MonoBehaviour
89
{
9-
//Pick which object pool you want to use
10-
public BulletObjectPool bulletPool;
10+
//Pick which object pool you want to use by activating it in the hierarchy and drag it to its uncommented slot
11+
12+
//Simplest possible object pool
13+
//public BulletObjectPoolSimple bulletPool;
1114

15+
//Optimized object pool
1216
//public BulletObjectPoolOptimized bulletPool;
1317

18+
//Unity's native object pool
19+
public BulletObjectPoolUnity bulletPool;
20+
1421

1522
//Private
16-
private float rotationSpeed = 60f;
23+
private readonly float rotationSpeed = 60f;
1724

1825
private float fireTimer;
1926

20-
private float fireInterval = 0.1f;
27+
private readonly float fireInterval = 0.1f;
2128

2229

2330

@@ -35,7 +42,7 @@ void Start()
3542

3643
void Update()
3744
{
38-
//Rotate gun
45+
//Rotate gun with A and D keys
3946
if (Input.GetKey(KeyCode.A))
4047
{
4148
transform.Rotate(Vector3.up, -rotationSpeed * Time.deltaTime);
@@ -46,17 +53,15 @@ void Update()
4653
}
4754

4855

49-
//Fire gun
56+
//Fire gun with spacebar
5057
if (Input.GetKey(KeyCode.Space) && fireTimer > fireInterval)
5158
{
5259
fireTimer = 0f;
5360

54-
GameObject newBullet = GetABullet();
61+
GameObject newBullet = bulletPool.GetBullet();
5562

5663
if (newBullet != null)
5764
{
58-
newBullet.SetActive(true);
59-
6065
newBullet.transform.forward = transform.forward;
6166

6267
//Move the bullet to the tip of the gun or it will look strange if we rotate while firing
@@ -72,14 +77,5 @@ void Update()
7277
//Update the time since we last fired a bullet
7378
fireTimer += Time.deltaTime;
7479
}
75-
76-
77-
78-
private GameObject GetABullet()
79-
{
80-
GameObject bullet = bulletPool.GetBullet();
81-
82-
return bullet;
83-
}
8480
}
8581
}
File renamed without changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace ObjectPool.Gun
6+
{
7+
//Parent object pool class to avoid code duplication
8+
public class ObjectPoolBase : MonoBehaviour
9+
{
10+
//How many bullets do we start with when the game starts
11+
protected const int INITIAL_POOL_SIZE = 10;
12+
13+
//Sometimes it can be good to put a limit to how many bullets we can instantiate or we might get millions of them
14+
protected const int MAX_POOL_SIZE = 20;
15+
}
16+
}

Assets/Patterns/18. Object Pool/Gun/Object pools/ObjectPoolBase.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)