Loading Assets from AssetBundles 5 Issue: Custom Shaders not Loading Properly

All objects loaded from assetbundles that use custom shaders appear black. Anyone else having this issue?

The shaders are definitely there, since I can fix the problem by calling the following script on the gameobject that has just been instantiated from an asset loaded from a bundle. The script simple iterates over all the shaders currently used on the asset, loads them by name, and reassigns it, but this should not be necessary.

    public void RedoShaders(GameObject g)
    {
        Renderer[] allRenderers = g.GetComponentsInChildren<Renderer>();

        for(int c = 0;c < allRenderers.Length;c++)
        {
            for(int i = 0; i < allRenderers[c].sharedMaterials.Length; i++)
            {
                 if(allRenderers[c].sharedMaterials[i].shader.name.ToLower().Contains("custom/"))
                {
                    Shader myshader = Shader.Find(allRenderers[c].sharedMaterials[i].shader.name);
                    allRenderers[c].sharedMaterials[i].shader = myshader;
                }
            }
        }
    }

You’re on Mac? If so, this seems to be a common problem. Not sure how to fix it, I wish I’d know.

Else you should check if the shader maybe is missing properties. This means that the assetbundle build process is stripping away properties/functions where he thinks you don’t need them later. You can disable the stripping in your grafic settings.

What platform are you building in?

The fixup solution is what we use if we are on ios/android as the assetbundles have been built for that platform which is why they don’t work when viewing in the editor.

If you run the same assetbundle on the platform without that fixup, it should work fine.

It is a bit annoying but the only way we found to circumvent it was to have a non-assetbundle mode when in the editor

Thanks KB73. I was noticing the problem in editor, loading from an android target assetbundle, so I suppose I can’t blame Unity in that case :slight_smile:
I’ll test it on the actual device and only bump this if I have an issue, but sounds like I won’t. Much appreciated.