- The recycling and reuse of objects, reducing the overhead of creating and destroying objects repeatedly.
- Easy to use
- It's also possible to use with other game engines with minor modifications.
- Open your project.
- Navigate to Tools -> Plugins -> Clone Plugin Project.
- Enter the following URL: https://github.com/DevHrytsan/FlaxObjectPool.git.
- It will ask you to reload the editor. Do it.
- Profit!
- DON`T open your project in Editor.
- Add FlaxObjectPool folder to the Plugin folder in your existing project
[!WARNING] If you do not already have a Plugin folder in your project, create one. Ensure that the FlaxObjectPool folder is named correctly as "FlaxObjectPool".
- Next, add a reference from your game project to the added plugin project. Open <project_name>.flaxproj with a text editor and add a reference to the plugin project. Like this:
"References": [
....,
{
"Name": "$(ProjectPath)/Plugins/FlaxObjectPool/FlaxObjectPool.flaxproj"
}
],
- Open your project.
- Enjoy!
At the beginning of your C# script, you should include the necessary namespace:
using FlaxObjectPool;
To create an object pool, instantiate the BaseObjectPool class with the desired type , where should be a reference type (class). You can specify several parameters during initialization, including functions for creating, getting, releasing, and destroying objects, as well as default capacity, maximum size, and a flag for limiting releases. For example, I will use Actor class:
var objectPool = new BaseObjectPool<Actor>(
() => OnCreateFuncion(), // Function to create objects
obj => OnGetObject(obj), // When getting an object
obj => OnReleaseObject(obj), // When releasing an object
obj => OnDestroyObject(obj) // When destroying an object
);
Call the Get() method to retrieve an object from the pool. When you're done with the object, use the Release(T item) method to return it to the pool. For example:
Actor myActor = objectPool.Get(); //Getting from pool
objectPool.Release(myActor); //Releasing to pool
You can clean and dispose of objects in the pool when necessary. The Dispose() method disposes of the entire pool, while the Clean() method releases all active objects back into the pool. Use them as needed.
objectPool.Dispose(); // Dispose the entire pool
objectPool.Clean(); // Release all active objects back to the pool
You can retrieve pool-related information, including its default capacity, maximum size, the total object count in the pool, and the count of active objects, by accessing properties like DefaultCapacity, MaxSize, CountAll, and ActiveCount.
You'll find it in the plugin's root folder under "Content -> Demo" and open the "DemoScenePool" scene. It showcases the usage of pooling with custom projectiles. To remove the demo content, delete all the "Demo" folders in FlaxObjectPool.
FlaxEditor_T9sPvSqcnT.mp4
Feel free to contribute to this project and suggest some ideas for it. Don`t forget about Flax Engine discord server
Under the MIT license. Feel free to use :)