Object Pool
Introduction
The object pool is a crucial component for optimizing memory management and performance. It improves game performance by reusing objects to reduce memory allocation and garbage collection.
GameObject Object Pool
Core Features
GameObjectPoolHelper
is a static utility class for managing Unity GameObject object pools, providing the following main features:
Pool Management
- Uses dictionary to store multiple object pools
- Supports both synchronous and asynchronous initialization
- Features automatic expansion mechanism
Object Acquisition and Recycling
- Supports both synchronous/asynchronous object acquisition
- Automatically manages object activation states
- Implements smart recycling mechanism
Main APIs
1. Initialize Object Pool
csharp
// Synchronous initialization
GameObjectPoolHelper.InitPool(string poolName, int size, PoolInflationType type = PoolInflationType.DOUBLE);
// Asynchronous initialization
await GameObjectPoolHelper.InitPoolWithPathAsync(string poolName, string assetPath, int size, PoolInflationType type = PoolInflationType.DOUBLE);
2. Get Object
csharp
// Synchronous acquisition
GameObject obj = GameObjectPoolHelper.GetObjectFromPool(string poolName, bool autoActive = true, int autoCreate = 0);
// Asynchronous acquisition
GameObject obj = await GameObjectPoolHelper.GetObjectFromPoolAsync(string poolName, string assetPath, bool autoActive = true, int autoCreate = 0);
3. Recycle Object
csharp
// Recycle GameObject
GameObjectPoolHelper.ReturnObjectToPool(gameObject);
// Recycle Transform
GameObjectPoolHelper.ReturnTransformToPool(transform);
Usage Examples
csharp
// 1. Initialize object pool
GameObjectPoolHelper.InitPool("EnemyPool", 10);
// 2. Get object
GameObject enemy = GameObjectPoolHelper.GetObjectFromPool("EnemyPool");
// 3. Use object
enemy.transform.position = Vector3.zero;
// 4. Recycle object
GameObjectPoolHelper.ReturnObjectToPool(enemy);
Asynchronous Usage Examples
csharp
// 1. Asynchronously initialize object pool
await GameObjectPoolHelper.InitPoolWithPathAsync("EnemyPool", "Enemy", 10);
// 2. Asynchronously get object
GameObject enemy = await GameObjectPoolHelper.GetObjectFromPoolAsync("EnemyPool", "Enemy");
// 3. Use object
enemy.transform.position = Vector3.zero;
// 4. Recycle object
GameObjectPoolHelper.ReturnObjectToPool(enemy);
Feature Description
1. Automatic Expansion
- DOUBLE: Doubles the capacity when insufficient
- INCREMENT: Incrementally expands capacity when insufficient
2. Automatic Activation
- Optional automatic activation when getting objects
- Automatic object state management during recycling
Best Practices
Usage Guidelines
Initialization Strategy:
- Estimate initial pool size based on actual needs
- Choose appropriate expansion strategy
Performance Optimization:
- Preheat frequently used object pools
- Avoid frequent pool capacity adjustments
Resource Management:
- Timely recycle unused objects
- Properly manage asynchronously loaded resources
Important Notes
WARNING
- Ensure objects have the PoolObject component when recycling
- Handle asynchronous operation timing correctly
- Watch for memory leaks, ensure objects are properly recycled
Technical Support
Get Help
- 💬 Join QQ Group Discussion
(ET Framework Group)
: 474643097 - ⭐ Follow the project on GitHub for latest updates