Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5de5745

Browse files
Initial commit
1 parent 55fad48 commit 5de5745

File tree

61 files changed

+6710
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+6710
-0
lines changed

‎.vsconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": "1.0",
3+
"components": [
4+
"Microsoft.VisualStudio.Workload.ManagedGame"
5+
]
6+
}

‎Assets/01. Scripts.meta‎

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

‎Assets/01. Scripts/Object Pooling.meta‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Unity Object Pooling System Documentation
2+
3+
## Overview
4+
Object Pooling is a design pattern used to improve performance by reusing objects instead of creating and destroying them repeatedly. This implementation provides a robust object pooling system for Unity using the built-in ObjectPool class.
5+
6+
## Components
7+
8+
### 1. ObjectPoolContainer
9+
A serializable class that holds the configuration for each pool:
10+
11+
```csharp
12+
[System.Serializable]
13+
public class ObjectPoolContainer
14+
{
15+
public string PoolName;
16+
public GameObject PoolObject;
17+
18+
public ObjectPoolContainer(string poolName, GameObject poolObject)
19+
{
20+
PoolName = poolName;
21+
PoolObject = poolObject;
22+
}
23+
}
24+
```
25+
26+
### 2. ObjectPoolManager
27+
The main manager class that handles the creation and management of object pools:
28+
29+
```csharp
30+
public class ObjectPoolManager : MonoBehaviour
31+
{
32+
[SerializeField]
33+
private ObjectPoolContainer[] objectPools;
34+
private Dictionary<string, IObjectPool<GameObject>> poolsDictionary;
35+
36+
// Implementation details below...
37+
}
38+
```
39+
40+
## How It Works
41+
42+
1. **Pool Initialization**:
43+
- Each pool is defined by an ObjectPoolContainer with a unique name and prefab
44+
- Pools are automatically initialized in Awake
45+
- Default capacity is 10 objects with a maximum of 100 objects per pool
46+
47+
2. **Pool Operations**:
48+
- Objects are retrieved using `GetObjectFromPool(string poolName)`
49+
- Objects are returned using `ReleaseObjectToPool(string poolName, GameObject obj)`
50+
- Active/inactive state is automatically managed
51+
52+
## Usage Example
53+
54+
1. **Setup in Inspector**:
55+
```csharp
56+
// Attach ObjectPoolManager to a GameObject
57+
public class GameManager : MonoBehaviour
58+
{
59+
[SerializeField] private ObjectPoolManager poolManager;
60+
[SerializeField] private GameObject bulletPrefab;
61+
62+
void Start()
63+
{
64+
// Configure your pools in the inspector
65+
// Add ObjectPoolContainer with name "Bullets" and bulletPrefab
66+
}
67+
}
68+
```
69+
70+
2. **Spawning Objects**:
71+
```csharp
72+
// Get an object from the pool
73+
GameObject bullet = poolManager.GetObjectFromPool("Bullets");
74+
bullet.transform.position = spawnPoint.position;
75+
76+
// Return object to pool when done
77+
poolManager.ReleaseObjectToPool("Bullets", bullet);
78+
```
79+
80+
## Features
81+
82+
1. **Automatic Management**:
83+
- Automatic object activation/deactivation
84+
- Built-in capacity management
85+
- Error checking for non-existent pools
86+
87+
2. **Performance Benefits**:
88+
- Reduces garbage collection calls
89+
- Minimizes memory fragmentation
90+
- Improves frame rate stability
91+
92+
3. **Unity Integration**:
93+
- Uses Unity's built-in ObjectPool class
94+
- Serializable in Inspector
95+
- Compatible with Prefab system
96+
97+
## Best Practices
98+
99+
1. **Pool Sizing**:
100+
- Set appropriate initial capacity based on maximum expected simultaneous objects
101+
- Consider memory vs. performance trade-offs when setting maxSize
102+
103+
2. **Object Management**:
104+
- Always release objects back to the pool when done
105+
- Don't destroy pooled objects manually
106+
- Use pool names consistently throughout your code
107+
108+
3. **Performance Optimization**:
109+
- Cache pool references when accessing frequently
110+
- Initialize pools during loading screens or quiet moments
111+
- Consider using multiple smaller pools instead of one large pool
112+
113+
## Common Pitfalls to Avoid
114+
115+
1. **Memory Leaks**:
116+
- Not releasing objects back to the pool
117+
- Keeping references to pooled objects after release
118+
119+
2. **Performance Issues**:
120+
- Setting pool capacity too low
121+
- Creating new pools during gameplay
122+
- Excessive pool resizing
123+
124+
3. **Logic Errors**:
125+
- Using destroyed pooled objects
126+
- Modifying pooled objects permanently
127+
- Not resetting object state on reuse
128+
129+
## Implementation Details
130+
131+
The system uses Unity's built-in `ObjectPool<T>` class with the following configuration:
132+
133+
```csharp
134+
new ObjectPool<GameObject>(
135+
createFunc: () => Instantiate(poolContainer.PoolObject),
136+
actionOnGet: obj => obj.SetActive(true),
137+
actionOnRelease: obj => obj.SetActive(false),
138+
actionOnDestroy: obj => Destroy(obj),
139+
collectionCheck: false,
140+
defaultCapacity: 10,
141+
maxSize: 100
142+
);
143+
```
144+
145+
- `createFunc`: Creates new instances when needed
146+
- `actionOnGet`: Activates objects when retrieved
147+
- `actionOnRelease`: Deactivates objects when released
148+
- `actionOnDestroy`: Properly destroys objects when pool is cleared
149+
- `collectionCheck`: Disabled for performance
150+
- `defaultCapacity`: Initial pool size
151+
- `maxSize`: Maximum number of objects per pool
152+
153+
## Notes
154+
- Consider implementing pool expansion monitoring for optimization
155+
- Add state reset logic for complex pooled objects
156+
- Test pool performance under various load conditions

‎Assets/01. Scripts/Object Pooling/Object Pooling Pattern.md.meta‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using UnityEngine;
2+
3+
/// <summary>
4+
/// Represents a container for object pooling.
5+
/// </summary>
6+
[System.Serializable]
7+
public class ObjectPoolContainer
8+
{
9+
/// <summary>
10+
/// Gets the name of the pool.
11+
/// </summary>
12+
public string PoolName;
13+
14+
/// <summary>
15+
/// Gets the object to be pooled.
16+
/// </summary>
17+
public GameObject PoolObject;
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="ObjectPoolContainer"/> class.
21+
/// </summary>
22+
/// <param name="poolName">The name of the pool.</param>
23+
/// <param name="poolObject">The object to be pooled.</param>
24+
public ObjectPoolContainer(string poolName, GameObject poolObject)
25+
{
26+
PoolName = poolName;
27+
PoolObject = poolObject;
28+
}
29+
}

‎Assets/01. Scripts/Object Pooling/ObjectPoolContainer.cs.meta‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
using UnityEngine.Pool;
4+
5+
/// <summary>
6+
/// Manages object pools for efficient reuse of game objects.
7+
/// </summary>
8+
public class ObjectPoolManager : MonoBehaviour
9+
{
10+
/// <summary>
11+
/// Array of object pool containers to be managed.
12+
/// </summary>
13+
[SerializeField]
14+
private ObjectPoolContainer[] objectPools;
15+
16+
/// <summary>
17+
/// Dictionary to store the object pools with their names as keys.
18+
/// </summary>
19+
private Dictionary<string, IObjectPool<GameObject>> poolsDictionary = new Dictionary<string, IObjectPool<GameObject>>();
20+
21+
/// <summary>
22+
/// Initializes the object pools.
23+
/// </summary>
24+
private void Awake()
25+
{
26+
foreach (var poolContainer in objectPools)
27+
{
28+
var pool = new ObjectPool<GameObject>(
29+
createFunc: () => Instantiate(poolContainer.PoolObject),
30+
actionOnGet: obj => obj.SetActive(true),
31+
actionOnRelease: obj => obj.SetActive(false),
32+
actionOnDestroy: obj => Destroy(obj),
33+
collectionCheck: false,
34+
defaultCapacity: 10,
35+
maxSize: 100
36+
);
37+
38+
poolsDictionary.Add(poolContainer.PoolName, pool);
39+
}
40+
}
41+
42+
/// <summary>
43+
/// Gets an object from the specified pool.
44+
/// </summary>
45+
/// <param name="poolName">The name of the pool.</param>
46+
/// <returns>The pooled game object.</returns>
47+
public GameObject GetObjectFromPool(string poolName)
48+
{
49+
if (poolsDictionary.TryGetValue(poolName, out var pool))
50+
{
51+
return pool.Get();
52+
}
53+
Debug.LogError($"Pool with name {poolName} does not exist.");
54+
return null;
55+
}
56+
57+
/// <summary>
58+
/// Releases an object back to the specified pool.
59+
/// </summary>
60+
/// <param name="poolName">The name of the pool.</param>
61+
/// <param name="obj">The game object to be released.</param>
62+
public void ReleaseObjectToPool(string poolName, GameObject obj)
63+
{
64+
if (poolsDictionary.TryGetValue(poolName, out var pool))
65+
{
66+
pool.Release(obj);
67+
}
68+
else
69+
{
70+
Debug.LogError($"Pool with name {poolName} does not exist.");
71+
}
72+
}
73+
}

‎Assets/01. Scripts/Object Pooling/ObjectPoolManager.cs.meta‎

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

‎Assets/01. Scripts/Singleton.meta‎

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /