Ejemplo n.º 1
0
    /// <summary>
    /// fetches a new instance from the recycle bin. Returns null if we reached the hardLimit.
    /// </summary>
    public GameObject Spawn()
    {
        var go = Pop();

        if (go != null)
        {
            OnSpawnedEvent?.Invoke(go);

            if (reset)
            {
                go.GetComponent <PooledGameObject>()?.Reset();
            }

            if (automaticallyRecycleParticleSystems)
            {
                var system = go.GetComponent <ParticleSystem>();
                if (system)
                {
                    // we add the startLifetime to the system's duration to avoid it getting recycled while emitting.
                    // note that curves can extend the startLifetime so this isn't perfect
                    TrashMan.DespawnAfterDelay(go, system.duration + system.startLifetime);
                }
                else
                {
                    Debug.LogError("automaticallyRecycleParticleSystems is true but there is no ParticleSystem on this GameObject!");
                }
            }
        }

        return(go);
    }