- Home /
surface transparency works in editor, but not when i build to device
Hi, i am changing my surface transparency using the following function:
public void SetTransparent(string objectString, byte alpha)
{
GameObject pial = GameObject.Find(objectString);
MeshRenderer[] rendererArray = pial.GetComponentsInChildren<MeshRenderer>();
MeshRenderer renderer = rendererArray[0];
Material material = renderer.material;
material.SetFloat("_Mode", 4f);
Color32 col = renderer.material.GetColor("_Color");
col.a = alpha;
col.r = 195;
col.g = 127;
col.b = 80;
renderer.material.SetColor("_Color", col);
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.EnableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
}
this works well when i test in the editor (see below left) but when i build to device, the transparency does not work (below right)
does anyone know why this is/how to fix it? thanks
Answer by dan_wipf · Aug 27, 2018 at 05:55 AM
well if you have set your materials shader to StanderShader you could simply do this: If possible and no changes have to be made to your Material at Runtime, except changing the Transparent Value, you're better off with predefine everything(Color,RenderQueue and Stuff). if not I'd recommend writing your own Shader and modify that, or create for each Preset a new Material..(might be a bit Performance costly)..
let me know if this is what you'd like to achieve.
Dan
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class YourClass : MonoBehaviour {
public GameObject pial;
public void SetTransparent(string objectString, float alpha)
{
// i'd recomend to set the pial GameObject manualy over the Inspector.
if(pial == null){
GameObject pial = GameObject.Find(objectString);
}
MeshRenderer[] rendererArray = pial.GetComponentsInChildren<MeshRenderer>();
MeshRenderer renderer = rendererArray[0];
Material material = renderer.material;
Color col = new Color(material.color.r,material.color.g,material.color.b,alpha);
//if really necessary you add your Lines here, but if you have predefinde all
//settings in your Material settings in Inspector it's not necessary anymore
//material.SetInt("_SrcBlend",(int)UnityEngine.Rendering.BlendMode.SrcAlpha);
//material.SetInt("_DstBlend",(int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
//material.SetInt("_ZWrite", 0);
//material.DisableKeyword("_ALPHATEST_ON");
//material.EnableKeyword("_ALPHABLEND_ON");
//material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
//material.renderQueue = 3000;
}
}
i'm confused, how does this allow transparency to work when i build to my device?
hm you change the alpha value from 100 to maybe 50 and then you get the tramsparent effect, if it’s 100 back again you cant see throu
for mobile device might this shader works:
Shader "Transparent/Diffuse ZWrite" {
Properties {
_Color ("$$anonymous$$ain Color", Color) = (1,1,1,1)
_$$anonymous$$ainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
// extra pass that renders to depth buffer only
Pass {
ZWrite On
Color$$anonymous$$ask 0
}
// paste in forward rendering passes from Transparent/Diffuse
UsePass "Transparent/Diffuse/FORWARD"
}
Fallback "Transparent/VertexLit"
}
Your answer
Follow this Question
Related Questions
Is there a shader in Unity that prevents transparent parts from overlapping? 1 Answer
How to write Diffuse shader with Pass{} instead of surface? 1 Answer
Global Transparency for sprite shader 1 Answer
transparent RGBA PNG textures worked in 4.6, how can I get them working in Unity5? 1 Answer
use Alpha from image in Fragment shader 0 Answers