The use of the pooling system requires
C# coding, so you have total control on where and when the
system has to work. Basically, a single Pooler is a collection of objects (Prefabs and Audio Clips) used by
another object (for instance, the collection of bullets and sounds used by a gun). This means you have to
start linking the Pooler to the object that needs to spawn objects with the pooling technique.
[t[LINKING TO A POOLER]]
The Pooler is a Unity
ScriptableObject, so you have just to define an
EasyPoolingPlus public
variable in your object MonoBehaviour class.
Example
using UnityEngine;
// Remember to import TigerForge namespace.
using TigerForge;
public class Gun : MonoBehaviour
{
-// Create a public variable of EasyPoolingPlus class so as to have a link for a Pooler.
-public EasyPoolingPlus bulletsPool;
-// Default Start() Unity function
-void Start() {
-}
}
[t[START THE POOLING SYSTEM]]
The pooling system engine of [SP]Easy Pooling PLUS[S] must be started before using it. In this way, you have
control of where and when the pooling system has to start working. To start the Pooler engine you have just
to call the
Start() Pooler method:
variable_name.Start()
Example
using UnityEngine;
using TigerForge;
public class Gun : MonoBehaviour
{
-public EasyPoolingPlus bulletsPool;
-void Start() {
--// The pooling system for the bulletsPool Pooler will start when the game starts.
--bulletsPool.XXStarXX();
-}
}
[t[SPAWNING A PREFAB]]
Normally, when you need to spawn a Prefab, you use Unity
Instantiate() method. Now, the instantiation
of Prefabs is the job of the Pooler. It will search for an existing instance and, if it's found, it will
return
it, in order to reuse that object. If an instance doesn't exist or there aren't reusable instances, the
Pooler will create a new one. The new one will be included in the pooling system logic.
To get a Prefab
you have just to call the
GetPrefab() Pooler method, specifyng the ID of the Prefab. Keep in mind
that the released Prefab is activated and working:
variable_name.GetPrefab(ID, position)
{{Parameter{}Type{}Description}}
{variable_name!!The name of the EasyPoolingPlus instance.}
{ID!string!The unique ID that identifies the Prefab.}
{position!Vector3 (optional)!The position where the Prefab has to be placed.}
{{Returned value{}Description}}
{GameObject!The Prefab instantiated or reused by the Pooler.}
Example
using UnityEngine;
using TigerForge;
public class Gun : MonoBehaviour
{
-public EasyPoolingPlus bulletsPool;
-void Start() {
--bulletsPool.XXStarXX();
-}
-// Default Update() Unity function.
-void Update() {
--if (Input.GetKeyUp(KeyCode.A))
--{
---// When the user press [A] key, the "BULLET_SPHERE" Prefab is released by the Pooler (in the same Gun position; it's optional).
---// If it's a new instantiated Prefab or a reused Prefab is a decision of the Pooler.
---GameObject bullet = bulletsPool.GetPrefab("BULLET_SPHERE", gameObject.transform.position);
---// 'bullet' variable contains the released GameObject. From here, you can continue working with this object as usual.
---// ...
--}
-}
}
[t[SPAWNING AN AUDIO CLIP]]
In general, the spawning of Audio Clips works with the same logic of Prefabs spawning. The difference is
that the Pooler looks for
not playing Audio Clips. If a sound is not playing, the Pooler plays it. If
there aren't Audio Clips or all the Audio Clips are playing, the Pooler will create a new one. The new one
will be included in the pooling system logic.
To get an Audio Clip
you have just to call the
GetAudioClip() Pooler method, specifyng the ID of the Audio Clip. The
Pooler will play it, according with the Pooler settings, and will release a Unity AudioSource item:
variable_name.GetAudioClip(ID, position, play)
{{Parameter{}Type{}Description}}
{variable_name!!The name of the EasyPoolingPlus instance.}
{ID!string!The unique ID that identifies the Prefab.}
{position!Vector3 (optional)!The position where the Prefab has to be placed.}
{play!Bool (optional)!It determines if the Audio Clip has to be played or it has to be
released without playing it. It's true by default. You can set it to false if you need to have
total control of the released AudioSource.}
{{Returned value{}Description}}
{AudioSource!The AudioSource instantiated or reused by the Pooler.}
Example
using UnityEngine;
using TigerForge;
public class Gun : MonoBehaviour
{
-public EasyPoolingPlus bulletsPool;
-void Start() {
--bulletsPool.XXStarXX();
-}
-void Update() {
--if (Input.GetKeyUp(KeyCode.A))
--{
---GameObject bullet = bulletsPool.GetPrefab("BULLET_SPHERE", gameObject.transform.position);
---// The "SHOOT" Audio Clip is played as the Gun fires a bullet.
---AudioSource shoot = bulletsPool.GetAudioClip("SHOOT", gameObject.transform.position);
--}
-}
}
[t[PREFAB DESTROYING]]
While an Audio Clip is intended unused when it's simply not playing, a Prefab can be considered unused in
two different situations, as selected by the developer in the 'Destroy Type' parameter in the Pooler
settings.
Both situations can be manually managed by the developer, using SetActive(false) method for
the 'Set Inactive' feature or transform.position with the specified Z coordinate value for the "Set
Position" feature.
However, [SP]Easy Pooling PLUS[S] comes with an
EasyPoolingPlusSystem class that makes the Prefab
destroying procedure pretty easier and according to the Pooler settings. To destroy a Prefab you have just
to call the
Destroy() method of the EasyPoolingPlusSystem class:
EasyPoolingPlusSystem.Destroy(gameObject)
Example
using UnityEngine;
using TigerForge;
public class Bullet : MonoBehaviour
{
// It's supposed that this DestroyMe() function is called when this Bullet has to me destroyed for the provided reasons.
-void DestroyMe() {
--// Just pass gameObject to the Destoy() method.
--EasyPoolingPlusSystem.XXDesXX(gameObject);
-}
}