Search Unity

Loading Shaders from Asset Bundles

Discussion in 'Shaders' started by Digika, Feb 23, 2021.

  1. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    Does anyone know why these aren't searchable via Shader.Find() and is there a way to make them work properly?
    Sure, I can load them directly as an Asset via Bundle.LoadAsset, but there is plenty of Unity code that uses Shader.Find() so I need them to be available for the native API.
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    Shader.Find()
    searches only the shaders that have been loaded. When a shader is in an asset bundle, you have control over when the asset bundle and its contents get loaded.
     
  3. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    Yes, and that is my issue, for example, with UIToolkit that rigidly bound to global Shader.Find to work.
    Is there way to somehow populate global shader cache/resource from bundle? Even if via some hack?
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,026
    You just need to load the bundle before something uses Shader.Find(). There's no way around it.
     
  5. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    To make it clear - before ANYTHING uses Shader.Find() for the first time? Because if so, I do it very early at startup/boottime before (during) first splashscreen scene is loaded.

    UPD: just tested, I indeed load my AsetBundle before first call to Shader.Find()
     
    Last edited: Feb 24, 2021