User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Using an external pooling system

Since version 1.5.9 Easy Decal has the ability to inject a custom instantiating method to delegate the instantiating to an external pooling system.

The following example is using the PoolManager from Path-o-logical Games. But you could of course roll your own or use any other pooling system, as they all are similar in terms of functionality and API.

Somewhere in your code (e.g. in your game manager) register your custom instantiation method like this:

public void Awake()
{
    EasyDecal.Instantiation = PoolInstantiation;
}
 
private static EasyDecal PoolInstantiation(GameObject decalPrefab, GameObject parent, Vector3 position, Quaternion rotation)
{
    EasyDecal clone = PoolManager.Pools[PoolName].Spawn(decalPrefab, position, rotation).GetComponent<EasyDecal>();
    clone.Reset(true);// Version >= 1.6.0
    clone.Reset();// Version < 1.6.0

    return clone;
}

 

On your decal prefab add the following script to recycle the game object after it has been faded out:

using ch.sycoforge.Decal;
using UnityEngine;
 
[RequireComponent(typeof(EasyDecal))]
public class DecalPooling : MonoBehaviour
{
    //-------------------------------
    // Static Fields
    //-------------------------------
    public static string PoolName = "Decals";
 
    //-------------------------------
    // Fields
    //-------------------------------
    private Vector3 prefabScale;
    private EasyDecal decal;
 
    //-------------------------------
    // Methods
    //-------------------------------
    public void Awake()
    {
        decal = GetComponent<EasyDecal>();
        decal.OnFadedOut += decal_OnFadedOut;
 
        //Cache prefab scale
        prefabScale = gameObject.transform.localScale;
    }
 
    private void decal_OnFadedOut(EasyDecal obj)
    {
        // Set parent to scene root
        obj.gameObject.transform.parent = null;
 
        //Reset to prefab scale for the case that your pooling system doesn't take care about scale inheritance
        obj.gameObject.transform.localScale = prefabScale;
 
        PoolManager.Pools[PoolName].Despawn(obj.transform);
    }
}