Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Unity Make Object Transparent In Runtime Not Working On Mobile

Discussion in 'Shaders' started by cihatozdemir, Jun 7, 2023.

  1. cihatozdemir

    cihatozdemir

    Joined:
    Oct 23, 2020
    Posts:
    5
    Hi guys, I make a fade object system. System works perfectly on editor, but if I build on IOS or Android, it seems like this;
    upload_2023-6-7_18-3-24.jpeg

    But on editor, seems like this;
    upload_2023-6-7_18-3-12.png


    I use this code to make transparent

    Code (CSharp):
    1.             material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
    2.             material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    3.             material.SetInt("_ZWrite", 0);
    4.             material.SetInt("_Surface", 1);
    5.  
    6.             material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
    7.            
    8.             material.SetShaderPassEnabled("DepthOnly", false);
    9.             material.SetShaderPassEnabled("SHADOWCASTER", retainShadows);
    10.            
    11.             material.SetOverrideTag("RenderType", "Transparent");
    12.            
    13.             material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
    14.             material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
    How can ı solve this?
     
  2. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    This is due to shader stripping. Because you only enable those keywords through code, Unity's build system doesn't know those keywords will be enabled, so it strips them from the shader. To fix this, you need a material at build time with those keywords enabled.

    Easiest solution is to put a dummy material in Resources (any folder named "Resources" anywhere in your project) with all the right stuff set up and never actually use it. Then when Unity goes to build your project, it sees a transparent material with premultiplied alpha, and so will include those shader variants.
     
    cihatozdemir likes this.
  3. cihatozdemir

    cihatozdemir

    Joined:
    Oct 23, 2020
    Posts:
    5
    Thank you, this works! I believe a separate dummy object needs to be created for each shader. In the end, it worked. Thank you!
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,255
    The other option is to use shader variant collection. A list of all used shaders variants is automatically generated by the editor, and you can save that list as a collection and use that to inform the build system what variants need to be included. I would clear the list, then play through the game in the editor, and then save the collection.

    You can also manual add or remove variants if need be.

    https://docs.unity3d.com/Manual/shader-variant-collections.html