- Home /
It will not show correctly using custom shader while loading assetbundle built in iPhone
Just like title. this is the code pack the prefab
Packcode
private static void PackSelect()
{
string path = Application.streamingAssetsPath + "/bundle.pack";
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(Selection.activeObject, null, path,
BuildAssetBundleOptions.CollectDependencies |
BuildAssetBundleOptions.CompleteAssets
, BuildTarget.iPhone);
BuildPipeline.PopAssetDependencies();
}
the prefab can be shown on iPhone correctly, but it turned black(may be execute "Fallback" in shader) while loading assetbundle in UNITY_EDITOR and other platforms.
Shader code
Shader "Mobile/Cutoff2Side"
{
Properties
{
_Color("_Color", Color) = (0.4980392,0.4980392,0.4980392,1)
_Colorpower("_Colorpower", Float) = 2
_Diffusemap("_Diffusemap", 2D) = "gray" {}
_Clipbias("_Clipbias", Range(0,1) ) = 0.466
_Alphabias("_Alphabias", Range(0,1) ) = 0
}
SubShader
{
LOD 200
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Cull Back
Lighting Off
ZWrite Off
AlphaTest Off
Fog { Mode Off }
Offset -1, -1
ColorMask RGB
Blend One OneMinusSrcAlpha
ColorMaterial AmbientAndDiffuse
CGPROGRAM
#pragma surface surf Lambert
#pragma target 2.0
float4 _Color;
float _Colorpower;
sampler2D _Diffusemap;
float _Clipbias;
float _Alphabias;
struct Input
{
float2 uv_Diffusemap;
};
void surf(Input IN, inout SurfaceOutput o)
{
float4 color = tex2D(_Diffusemap, IN.uv_Diffusemap);
o.Emission = (color * _Colorpower.xxxx * _Color).xyz;
o.Alpha = (color.a - _Alphabias).x;
float c = (color.a - _Clipbias).x;
clip(c);
}
ENDCG
}
Fallback "Diffuse"
}
The left is the prefab loaded from assetbundle, the right is the same asset from hierarchy, it could be shown the same on the mobile, whose material is the same one.
Answer by foobuggy · Dec 09, 2013 at 08:03 PM
This is a bit of an older post, but I thought I'd add an alternate approach. A semi-hacky fix I used is to basically place a script along these lines on the root object of your assetbundle/prefab.
private void Awake()
{
#if UNITY_EDITOR
foreach(Transform t in gameObject.GetComponentsInChildren<Transform>())
{
if(t.gameObject.renderer != null)
{
t.gameObject.renderer.material.shader = Shader.Find(t.gameObject.renderer.material.shader.name);
}
}
#endif
}
It isn't the most efficient approach, but it worked for me. Plus it is better than not being able to see your assets while trying to test the app in editor. Using compiler arguments to restrict this so it only runs in-editor will also avoid any performance hits in final builds.
This worked for me in the Editor, as well as on Android. Not the cleanest solution of course, but it works. Strangely, shaders don't get loaded with the AssetBundle only if they are one of the transparent ones.
Genius. Thank you. $$anonymous$$uch easier than messing around building a separate desktop bundle.
Answer by whydoidoit · Nov 08, 2013 at 04:36 AM
Yeah shaders are compiled for a platform when they are packed into the bundle, just the one platform (iPhone not the editor). I suggest you stick you shaders in a resources folder while you are developing so that they load correctly and switch to packing them in the bundle at the last minute.
i'm wordering how to configurate shaders before it is compiled.
Your answer
Follow this Question
Related Questions
Streamed textures appearing as default white only on iOS 0 Answers
It will not show correctly using custom shader while loading assetbundle built in iPhone 0 Answers
Multiple tex2D commands in fragment shader needed, is there a workaround? 0 Answers
MovieTexture on iOS 0 Answers
Automatically load asset bundles on start from cache 0 Answers