Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate and destroying particle system

Discussion in 'Scripting' started by LaurensiusTony, Jul 5, 2014.

  1. LaurensiusTony

    LaurensiusTony

    Joined:
    May 22, 2013
    Posts:
    78
    Hi, i have this kind of particle system manager code to control when and where my particle system instantiate and destroyed, but after learning about gameobject pool manager and know that instantiate and destroying gameobject can slow down the game i wondering is it apply to particle effect to? since my game is shooter and there will be so many enemies and explosions(particle system) so maybe that the thing that keep my 2d shooter game having low framerate or fps drop after playing for a while

    please take a look and maybe if you guys have more better way to achive the same experience like this but with better performance you can share it here :)

    Code (CSharp):
    1.  
    2.     public static SpecialEffectHelper Instance;
    3.  
    4.     public ParticleSystem fireEffect;
    5.     public ParticleSystem pauseEffect;
    6.     public ParticleSystem hitEffect;
    7.    
    8.     void Awake () {
    9.         if (Instance != null)
    10.         {
    11.             Debug.LogError("Multiple instance of SpecialEffectHelper!");
    12.         }
    13.  
    14.         Instance = this;
    15.     }
    16.  
    17.     /// <summary>
    18.     /// Create an explosion at the given location
    19.     /// </summary>
    20.     /// <param name="position"></param>
    21.     public void Explosion(Vector3 position)
    22.     {
    23.         //smoke on the water
    24.  
    25.         //fire in the sky
    26.         instantiate(fireEffect, position);
    27.     }
    28.  
    29.     public void PauseEffect(Vector3 position)
    30.     {
    31.         instantiate(pauseEffect, position);
    32.     }
    33.  
    34.     public void HitEffect(Vector3 position)
    35.     {
    36.         instantiate(hitEffect, position);
    37.     }
    38.  
    39.     /// <summary>
    40.     /// instantiate a particle system from prefab
    41.     /// </summary>
    42.     /// <param name="prefab"></param>
    43.     /// <param name="position"></param>
    44.     /// <returns></returns>
    45.     private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
    46.     {
    47.  
    48.         ParticleSystem newParticleSystem = Instantiate(
    49.             prefab,
    50.             position,
    51.             Quaternion.identity
    52.             ) as ParticleSystem;
    53.  
    54.         //make sure it will be destroyed
    55.         Destroy(
    56.             newParticleSystem.gameObject,
    57.             newParticleSystem.startLifetime
    58.             );
    59.  
    60.         return newParticleSystem;
    61.     }
    and i just need to put this script into emptygameobject, fill the particle effect in the script and to call it just need to

    Code (CSharp):
    1. SpecialEffectHelper.Instance.Explosion(transform.position);
    and that's it.... i found this is very easy to use but maybe not the best in performance and performance is what i looking for several days... (already doing a lot to improve the performance but no luck)
     
  2. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    Unless you develop for older devices and have a lot of instantiations in same frame you won't see much performance difference, but having so many particle systems on the screen at the same time is probably what is decreasing performance more than instantiation.
    Here is a guide for object pooling if you still want to implement it (use Play and Stop for particle systems instead of renderer.enabled): https://docs.unity3d.com/351/Documentation/Manual/UnderstandingAutomaticMemoryManagement.html
     
    Last edited: Jul 5, 2014
  3. LaurensiusTony

    LaurensiusTony

    Joined:
    May 22, 2013
    Posts:
    78
    my game do a lot of particle explosion and because of that i think making pooling is a good idea but i don't know how to impelement it.... and your doc is good but not helping me in understanding how to create poolmanager for particle system