- Home /
Rendermode: Cutout not working on gameobject from external asset bundle
Hi, I have a piece of clothing that uses transparency to 'cut out' bits of unwanted mesh.
This works when I load the object directly in my Unity main project. It also works when I create a prefab in a separate AssetBuilder project However, when I load the generated asset file and instantiate the prefab object, all the settings look fine in the debug inspector (all setting the same as a copy of the object that was directly instantiated by unity), but the cutout isn't actually happening on the object that came from the external asset file.
[1]: /storage/temp/155009-untitled.png
Answer by unitylepi · Mar 25, 2020 at 12:54 PM
decided to reapply shaders to all objects after instantiation.. not sure if there's a more optimal way to apply them directly to the loaded asset bundle so you only have to do it once for each prefab, rather than once for each prefab instantiation. Maybe someone can weight in here? Also, there seems to be a z-buffering change that takes place when transparent shaders are refreshed.
var prefab = ab.LoadAsset(assetName);
if (null == prefab) return false;
GameObject go = Instantiate(prefab) as GameObject;
if (go == null) return false;
RefreshShaders(go);
private void RefreshShaders(GameObject go)
{
// find all renderers for this GO (parent + Children)
Renderer[] rens = go.GetComponentsInChildren<Renderer>();
// for each material in each renderer, reattach the same shader if you can find it.
for (int i = 0; i < rens.Length; i++)
{
foreach (Material mat in rens[i].materials)
{
var shd = Shader.Find(mat.shader.name);
if (null == shd)
{
Debug.Log ("Cannot refresh the shader on GameObject:" + go.name + " shader name: "+mat.shader.name+". Applying standard shader. If not okay, add this shader to Resources folder.");
shd = Shader.Find("Standard");
}
mat.shader = shd;
}
}
}
Answer by eyalfx · Nov 18, 2021 at 09:33 PM
I've been having similar shader issues with assetbundle and resorted to similar solutions as the one you posted above. I do know that it's related to that but I don't know how to fix it so it works out of the box. I hate that I have to loop over objects one by one to fix them.