Search Unity

LoadedAsset does not respect renderingmode: cutout

Discussion in 'Editor & General Support' started by unitylepi, Mar 25, 2020.

  1. unitylepi

    unitylepi

    Joined:
    May 21, 2018
    Posts:
    34
    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 hat was directly instantiated by unity), but the cutout isn't actually happening on the object that came from the external asset file.

    https://imgur.com/a/aeQDBIz
    Untitled.png
     
  2. unitylepi

    unitylepi

    Joined:
    May 21, 2018
    Posts:
    34
    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?

    Code (CSharp):
    1.    
    2. var prefab = ab.LoadAsset(assetName);
    3.     if (null == prefab) return false;
    4.     GameObject go = Instantiate(prefab) as GameObject;
    5.     if (go == null) return false;
    6.  
    7.     RefreshShaders(go);
    8.  
    9.  
    10.     private void RefreshShaders(GameObject go)
    11.     {
    12.         // find all renderers for this GO (parent + Children)
    13.         Renderer[] rens = go.GetComponentsInChildren<Renderer>();
    14.  
    15.         // for each material in each renderer, reattached the same shader if you can find it.
    16.         for (int i = 0; i < rens.Length; i++)
    17.         {
    18.             foreach (Material mat in rens[i].materials)
    19.             {
    20.                 var shd = Shader.Find(mat.shader.name);
    21.                 if (null == shd)
    22.                 {
    23.                     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.");
    24.                     shd = Shader.Find("Standard");
    25.                 }
    26.                 mat.shader = shd;
    27.             }
    28.         }
    29.     }
    30.  
     
unityunity