Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rustyruss1985 · Aug 27, 2018 at 12:00 AM · transparencydevice

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

alt text

device-transparency.png (272.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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; 
     }
 }
 


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rustyruss1985 · Aug 28, 2018 at 12:03 AM 0
Share

i'm confused, how does this allow transparency to work when i build to my device?

avatar image dan_wipf rustyruss1985 · Aug 28, 2018 at 04:22 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges