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

Question How to programmatically change opacity on URP decal projector?

Discussion in 'Scripting' started by kadbry, Feb 21, 2023.

  1. kadbry

    kadbry

    Joined:
    May 30, 2022
    Posts:
    4
    The documentation for the URP projector is below.
    Class DecalProjector | Universal RP | 12.1.10 (unity3d.com)

    It says the variable fadeFactor controls the opacity, but I can't seem to access this variable programmatically.

    Code (CSharp):
    1. public DecalProjector decalProjector;
    2.  
    3. private void OnTriggerEnter(Collider other)
    4. {
    5.     //unrelated code
    6.     decalProjector.fadeFactor = 1; // ----> returns error (Decal projector does not contain a definition for fadeFactor)
    7. }
     
  2. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    You can programmatically change the opacity of a URP (Universal Render Pipeline) decal projector by accessing its material and modifying the alpha value of its color property.

    Here's an example code snippet in C# that demonstrates how to set the opacity of a decal projector to a specified value:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering.Universal;
    3.  
    4. public class DecalOpacityChanger : MonoBehaviour
    5. {
    6.     public float opacity = 0.5f; // the desired opacity value between 0 and 1
    7.  
    8.     private DecalProjector decalProjector;
    9.     private Material decalMaterial;
    10.  
    11.     void Start()
    12.     {
    13.         decalProjector = GetComponent<DecalProjector>();
    14.         decalMaterial = decalProjector.material;
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         Color decalColor = decalMaterial.color;
    20.         decalColor.a = opacity;
    21.         decalMaterial.color = decalColor;
    22.     }
    23. }
    24.  
     
  3. kadbry

    kadbry

    Joined:
    May 30, 2022
    Posts:
    4
    This is still giving me an error. It says DecalProjector does not contain a definition for material.
     
  4. kadbry

    kadbry

    Joined:
    May 30, 2022
    Posts:
    4
    It turns out whatever DecalProjector I was using wasn't the one in the Universal Rendering Pipeline. When I specified
    Code (CSharp):
    1. UnityEngine.Rendering.Universal.DecalProjector
    it worked as it should.