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. Dismiss Notice

Change Rendering Mode via Script

Discussion in 'Scripting' started by rob179994, Jun 13, 2017.

  1. rob179994

    rob179994

    Joined:
    Aug 4, 2014
    Posts:
    8
    So I have a pack of cards and I spread them out so that the user can pick one. I changed them from Opaque to Transparent so that I change the alpha value (used later in the game).

    Anyway, this change means that the cards display differently and Im not sure how to change this. The cards get slight height as they go right to left so that they appear like they do below (first picture). However, when changed to Transparent, they change to the 2nd picture below.

    upload_2017-6-13_13-29-26.png

    Anyway I thought I could get round this by changing the cards to Transparnet when required, but I cant seem to find out how to access Rendering Mode in script. Also, does anyone have any idea why this is happening?

    Thanks
    Rob
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    You'll want to make two materials, one transparent and one opaque, and use the script to swap between them.
    Code (csharp):
    1. public Material opaqueMat;
    2. public Material transparentMat;
    3. private Renderer rend;
    4.  
    5. void Awake() {
    6. rend = GetComponent<Renderer>();
    7. }
    8.  
    9. void UpdateMaterial(bool transparent) {
    10. if (transparent) {
    11. rend.material = transparentMat;
    12. }
    13. else {
    14. rend.material = opaqueMat;
    15. }
    16. }
    Then just call UpdateMaterial as needed.
     
    Boodums likes this.
  3. berkhulagu

    berkhulagu

    Joined:
    Sep 6, 2017
    Posts:
    11
    @StarManta I also used this approach until I stumbled into this.

    https://github.com/Unity-Technologi...er/Editor/Mono/Inspector/StandardShaderGUI.cs

    The function, SetupMaterialWithBlendMode, describes what actually happens when you pick different render modes.

    Here is a small extension class to help you out.

    Code (CSharp):
    1.  
    2. public static class MaterialExtensions
    3. {
    4.     public static void ToOpaqueMode(this Material material)
    5.     {
    6.         material.SetOverrideTag("RenderType", "");
    7.         material.SetInt("_SrcBlend", (int) UnityEngine.Rendering.BlendMode.One);
    8.         material.SetInt("_DstBlend", (int) UnityEngine.Rendering.BlendMode.Zero);
    9.         material.SetInt("_ZWrite", 1);
    10.         material.DisableKeyword("_ALPHATEST_ON");
    11.         material.DisableKeyword("_ALPHABLEND_ON");
    12.         material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    13.         material.renderQueue = -1;
    14.     }
    15.    
    16.     public static void ToFadeMode(this Material material)
    17.     {
    18.         material.SetOverrideTag("RenderType", "Transparent");
    19.         material.SetInt("_SrcBlend", (int) UnityEngine.Rendering.BlendMode.SrcAlpha);
    20.         material.SetInt("_DstBlend", (int) UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    21.         material.SetInt("_ZWrite", 0);
    22.         material.DisableKeyword("_ALPHATEST_ON");
    23.         material.EnableKeyword("_ALPHABLEND_ON");
    24.         material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    25.         material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.Transparent;
    26.     }
    27. }
    28.  
     
  4. EgoJacky

    EgoJacky

    Joined:
    Aug 15, 2017
    Posts:
    26
    Thank you! Works great.
     
  5. Dragonborn-

    Dragonborn-

    Joined:
    Jun 5, 2017
    Posts:
    2
    Thank you!
     
  6. kidmethuselah

    kidmethuselah

    Joined:
    Jan 14, 2021
    Posts:
    16
    Oh sweet lord thank you. I've been on this a week now. AWESOME!
     
  7. Unifikation

    Unifikation

    Joined:
    Jan 4, 2023
    Posts:
    1,045
    Is there no way to simply "tell" the shader that you want to change blend mode?