Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

lag when instantiating prefab particle effect for first time.

Discussion in 'WebGL' started by jdeuce, Nov 18, 2017.

  1. jdeuce

    jdeuce

    Joined:
    Dec 15, 2012
    Posts:
    22
    Shipping a basic game to WebGL from Unity 2017.2. Having hiccup where the game lags for a few frames when I instantiate a prefab into the scene for the first time, such as the first time an item is picked up and I Instantiate() the item pickup particle effect prefab into the scene. Instantiating the same effect for future item pickups has no lag.

    I found one suggestion to just spawn all your prefabs at the initialization of the scene, but to an off screen camera coordinate. And then a slightly refined suggestion to use a render texture instead of off screen coordinates.

    It seems like this should be handled by the engine so just wondering if this is the recommended solution or if there is a better approach you guys could recommend.

    The Android version of the game does not have this lag.

    Will I run into a similar problem with sounds once I start using them?
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,438
    Most likely, yes. In order to workaround this issue, I create prefab instances during the loading screen and store them in a GameObjectPool for later use.

    In my project, every GameObject knows its dependencies. For example, a "door" knows what sound to play when it gets opened. In Door.Awake(), it tells the GameObjectPool: I need this "open sound" prefab later, please instantiate if it's not available yet.

    Every Component with dependencies is doing this in my project. Once the scene has finished loading, I have a GameObjectPool that contains instances of prefabs that are later needed during game-play.

    If a door gets opened, the Door Component asks the GameObjectPool: I have this "open sound" prefab, please give me the corresponding instance I asked you to create earlier. The Door then gets the prefab instance from the GameObjectPool, enables it, plays the sound and once it's finished, the instance puts itself back into the GameObjectPool.

    Unity has a tutorial on game object pooling:
    https://unity3d.com/learn/tutorials/topics/scripting/object-pooling


    PS: For ParticleSystem's specifically, they have an option called "Prewarm". This option causes the particle system to run the simulation for a bit and can be very expensive.
     
  3. SunnyChow

    SunnyChow

    Joined:
    Jun 6, 2013
    Posts:
    360
    get similar problem