FX Loading Management (Unity Plugin)

From PopcornFX
(Redirected from Unity/Managing FX Loading)
Jump to: navigation, search

The default behaviour regarding FX' resources is to be loaded whenever the PKFxFX component's Start() event is called, which can cause a slight lag the first time an FX is instanced.
To remedy this, one may want to control when effects' resources are actually loaded and unloaded.

Unity-logo-g.png

Loading effects

You can use PKFxManager.PreLoadFxIFN() to force the preload (including shaders and textures) when you see fit.

The method takes the path to the pkfx file you want to preload, relative to the PackFX' root.

So let's say you need to preload an effect "StreamingAssets/PackFx/Particles/Whatever/BlingBling.pkfx" in a loading screen scene. You need to have an active PKFxRenderingPlugin component in your scene and call

 PKFxManager.PreLoadFxIFN("Particles/Whatever/BlingBling.pkfx");

at some point during the loading screen.

Since loading needs to synchronize with the rendering thread, every effects loaded with PreLoadFxIFN in a frame will be loaded after the next OnPostRender event received by the PKFxRenderingPlugin component. So it's advised to use coroutines to load one effect a frame to avoid visible lag. So let's say you have a custom component containing an array of paths to effects, doing something like :

public list<string> arrayPath;

IEnumerator Start()
{
    foreach (string path in arrayPath)
    {
      PKFxManager.PreLoadFxIFN(path);
      yield return null; // will yield until next frame, after the effect is actually preloaded.
    }
}

Unloading Effects

PopcornFX won't unload any resource on its own by default.
To explicitly unload an effect and its dependencies, simply call

PKFxManager.UnloadEffect()

As opposed to loading effects, this doesn't require sync'ing with the rendering thread and can be done on-the-spot, the unloading is done immediately.

Resetting the scene

As of v2.8, PopcornFX won't assume the state of the current scene when loading levels/deleting PKFxRenderingPlugin components.
To properly cleanup the particles in scene when changing level, a call to PKFxManager.Reset() is required.
However, this doesn't unload the internal resources associated with particles, do fully unload the effects and cleanup everything associated with particles, a call to PKFxManager.DeepReset() is appropriate.