Symptoms
- My shaders/materials/models loaded from an AssetBundle are pink, missing, or broken
- Shaders that are not loaded from an AssetBundle work correctly
Cause
This could be due to a couple of reasons. For example, the shader code inside the AssetBundle may not be supported by the target platform. Another reason could be that there is a reference to the shader stored in the Bundle, but not included in the built player.
Resolution
The AssetBundle needs to be built for the correct target platform, for example for Bundles intended to be used on iOS devices:
BuildPipeline.BuildAssetBundles("AssetBundles", BuildAssetBundleOptions.None, BuildTarget.iOS);
The Always Included Shaders list in Graphics Settings needs to stay the same between building and loading the AssetBundle. Unity checks the Always Included Shaders list in Graphics Settings. If a shader is in there, a reference to the shader will be stored in the AssetBundle instead of platform specific shader code. If you then remove this shader from the Always Included Shaders list then it might not be included in your built player, resulting in the shader not being found as the AssetBundle still only includes the reference to the shader.
The Graphics APIs list in the target platform's Player Settings should contain all needed APIs when building the AssetBundle. If the list changes between building and loading the AssetBundle then the shader code built into the AssetBundle could be for the wrong API, resulting in the shader being unsupported on the target platform.
More Information
When using one Unity project to build the AssetBundles and another Unity project to load the AssetBundles, the problem could be due to the projects having different Graphics APIs lists, or Always Included Shaders lists.
Comments
7 comments
This didn't helped much. I've fullfilled all the requirements and my material from asset bundle still pink.
Whoever makes it to here:
Add the shaders your prefabs are using to that Always Include Shaders list in Project Settings --> Graphics. Rebuild your bundle assets and it should work.
(Used in 2018.3.12f1)
@Viktoria Hwang That did the trick; thanks for helping!
I'm using bundles I build in one project (a) in another project (b) that acts as the player . If I need to add a shader to the Always Included shader in project (a), do I also need to include it in project (b)?
This article does not help, and how this happens needs to be nailed down better. I've gone through rebuilding the asset project from scratch twice, being careful to change just one thing each time, with no success. Suddenly after a Unity restart on the asset project and a rebuild, everything works. Change back the last setting I changed and restart Unity again, everything still works.
We really need to nail down what settings need to be the same between main and asset projects. It's obvious that it's not just the "Always Included Shaders" list differences, platform differences, or graphic API differences that are discussed in the article.
Ok, I finally nailed down that the Rendering Path in the Universal Renderer Data must match between the main and asset projects (obvious), AND AFTER CHANGING IT TO MATCH, YOU MUST RESTART UNITY FOR THE ASSET PROJECT FOR THE ADDRESSABLE BUILD SYSTEM TO PICK UP THE CHANGE FOR SOME REASON (not at all obvious).
I spent hours arriving at this conclusion, because even after changing all graphics settings to match between projects, things still would not work without a Unity restart on the asset project side. I could change the setting and rebuild Addressables all day, flushing build caches along the way, and the content hash will not change without a Unity restart.
Most people don't pay attention to their unity editor graphics API.
As mentioned in the article. If you build for OpenGL your editor must load the shaders from bundle with OpenGL too. So make sure to uncheck auto graphics API for standalone and only check what you built for.
Article is closed for comments.