- Home /
Question by
Panthesilea · Apr 25, 2014 at 05:41 PM ·
shadertreetransparent
Unity Tree transparency
Hello! I made an application with trees, created by Unity Tree Creator. I want filter some trees, so I replace shader and color to make transparent trees. It works in debug and standalone version too. Here's the code:
private void SetTransparent(GameObject g)
{
for (int i = 0; i < g.renderer.materials.Length; i++)
{
g.renderer.materials[i].shader = Shader.Find("Transparent/Diffuse");
g.renderer.materials[i].SetColor("_Color", new Color(1, 1, 1, 0.1f));
}
for (int i = 0; i < g.transform.childCount; i++)
SetTransparent(g.transform.GetChild(i).gameObject);
}
And the result:
I have an invert method to restore original shader and material color:
private void RevertTransparent(GameObject g)
{
for (int i = 0; i < g.renderer.materials.Length; i++)
{
if (g.renderer.materials[i].name == "Optimized Bark Material (Instance)")
g.renderer.materials[i].shader =
Shader.Find("Nature/Tree Creator Bark");
else if (g.renderer.materials[i].name ==
"Optimized Leaf Material (Instance)")
g.renderer.materials[i].shader =
Shader.Find("Nature/Tree Creator Leaves Fast");
else
g.renderer.materials[i].shader = Shader.Find("Default/Diffuse");
g.renderer.materials[i].SetColor("_Color", new Color(1,1,1));
}
for (int i = 0; i < g.transform.childCount; i++)
SetTransparent(g.transform.GetChild(i).gameObject);
}
And the restoring works only in debug mode in unity editor, in builded version this happens:
Any idea how to properly change textures in standalone version too?
transp.jpg
(88.8 kB)
revtransp.jpg
(152.7 kB)
Comment
Best Answer
Answer by Panthesilea · Apr 25, 2014 at 07:04 PM
I have to create a Resources folder, then put in it the used shader.