- Home /
iOS, Assentbundles and Custom Shaders
There's something special about shaders in assent bundles in iOS? When I do the process below and use Windows as target, everything run smoothly. When it do it in iOS, the materials contained in the asset bundle are all OK except that ones that uses custom shaders. Am I doing something wrong?
PS: The asset bundle do contains the custom shaders (Im using BuildAssetBundleOptions.CollectDependencies) and this process do works in windows bundles.
PushAssetDependencies
BuildAssetBundle // Materials and Shaders
PushAssetDependencies
BuildPlayer
PopAssetDependencies
PopAssetDependencies
In both platforms. If the bundle was generated with iOS as target it causes the problem even on editor mode.
Answer by equalsequals · Jul 26, 2013 at 03:17 PM
My team recently experienced this as well.
We found that simply re-assigning the material's shader at run-time with Shader.Find actually resolved the issue.
[RequireComponent(typeof(Renderer))]
public class ShaderFix : MonoBehaviour
{
//This should be attached to any Game Object with a Renderer that has a Material with a custom shader.
#if UNITY_IPHONE
void Awake()
{
Material[] newMats = new Material[renderer.materials.Length];
for(int i = 0; i < newMats.Length; i++)
{
Material ogMat = renderer.materials[i];
Material mat = new Material(ogMat);
//Note: the material maintains a link to its shader's name, so pass that across dynamically.
mat.shader = Shader.Find(ogMat.shader.name);
newMats[i] = mat;
}
renderer.materials = newMats;
}
#endif
}
Another thing that may also provide some help is to have empty materials somewhere in a built scene that use the shaders.
The problem seems to be that since the shaders get bundled, they don't come across in the build because they weren't used anywhere that the build process could see. Creating a simple empty MonoBehaviour with a Material[] will suffice here, and they will not add any significant memory as they don't need textures, but just a way to ground the custom shader in your project so that it is collected by the build processor.
Hope this helps.
==
Thanks! I still don't know why it happens (an Unity Bug, maybe) but at least now I have an workaround.
$$anonymous$$aybe you also have an answer to my other Asset Bundle question: http://answers.unity3d.com/questions/498393/assetbundle-content.html
Thanks again.
Your answer
Follow this Question
Related Questions
Asset Bundles with In-App Payment in Unity Mobile 1 Answer
Animator in assetbundle from iphone app not play 1 Answer
Using AssetBundles as IAP Crashing 0 Answers
AssetBundles on iOS: Memory always increases, causing crash 0 Answers
Does Apple allow you to download assetbundles with scriptableobjects, from an external server? 0 Answers