Search Unity

How to reload shaders after changing Graphics.activeTier?

Discussion in 'General Graphics' started by Arycama, Dec 20, 2017.

  1. Arycama

    Arycama

    Joined:
    May 25, 2014
    Posts:
    184
    Unity's documentation states that "Changing this value affects any subsequently loaded shaders".
    What about when you want the currently loaded shaders to change? I assume all currently loaded shaders need to be reloaded, but I can not find a builtin way to do this.

    My current attempt is to get all currently loaded Renderers through Resources.FindObjectsOfTypeAll<Renderer>, iterate through them and build a hashset of all required shaders, and then unload each shader through Resources.UnloadAsset(Shader). I then re-load each shader using Shader.Find, and assign them to each renderer.

    This works and loads the shader for the correct shader tier if changed. However, it seems to avoid inactive gameobjects, as their material does not get re-assigned and is pink when they are re-enabled.

    tl;dr Is there a way to reload all currently loaded shaders at runtime?
     
  2. melting_pot

    melting_pot

    Joined:
    Mar 2, 2018
    Posts:
    9
    Did you find a way? I would also like to be able to do this!
     
  3. Arycama

    Arycama

    Joined:
    May 25, 2014
    Posts:
    184
    Not really. I think I looped through every renderer in the scene, built a dictionary of material/shader pairs, unloaded each shader using Resources.UnloadAsset, then re-loaded each using Shader.Find, and assigning it to each material.

    In the end, I just ended up using Global Shader Keywords as they get applied instantly.

    Because of this limitation I'm not using shader tiers for anything, as they can't be changed easily at runtime.