unity-game-engineshaderdecal

Unity Legacy Shader how to change decal from script


This is what I am using but I can't find anything online regarding how to go about changing out a decal.

Let's consider that I have multiple Textures ex.(Tex1, Tex2, .. etc).


Solution

  • If you look at the source code of LegacyShaders/Decal(state Unity 2017.2) you see that the property you want to change is called _DecalText

    Shader "Legacy Shaders/Decal" 
    {
        Properties 
        {
            _Color ("Main Color", Color) = (1,1,1,1)
            _MainTex ("Base (RGB)", 2D) = "white" {}
            _DecalTex ("Decal (RGBA)", 2D) = "black" {}
        }
    
        ...
    }
    

    You can simply set it using material.SetTexture

    material.SetTexture("_DecalTex", texture);
    

    e.g. like

    public class Example : MonoBehaviour
    {
        // reference in the Inspector
        public Texture texture;
    
        private void Start()
        {
            var renderer = GetComponent<MeshRenderer>();
            var material = renderer.material;
    
            material.SetTexture("_DecalTex", texture);
        }
    }
    

    enter image description here