2

I want to access two properties _Color and _Color2 and want to change the values using script as we can see the value is in the format (1,1,1,1) so how can I change it its basically color values.

Shader "Custom/SpriteGradient" 
{
    Properties
    {
        [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
        _Color("LeftColor", Color) = (1,1,1,1)
        _Color2("RightColor", Color) = (1,1,1,1)
        _Scale("Scale", Float) = 1

        _StencilComp("Stencil Comparison", Float) = 8
        _Stencil("Stencil ID", Float) = 0
        _StencilOp("Stencil Operation", Float) = 0
        _StencilWriteMask("Stencil Write Mask", Float) = 255
        _StencilReadMask("Stencil Read Mask", Float) = 255
        _ColorMask("Color Mask", Float) = 15
        // see for example
        // http://answers.unity3d.com/questions/980924/ui-mask-with-shader.html
    }

    ...
}
1

1 Answer 1

2

See Material.SetColor

Modified example from the API

void Start()
{
    //Get the Renderer component from this object
    var renderer = GetComponent<Renderer>();

    var material = renderer.material;

    //Call SetColor using the shader property name "_Color" and "_Color2" and setting their color to red
    material.SetColor("_Color", Color.red);
    material.SetColor("_Color2", new Color(1, 0, 0, 1));  
}

The name means the actual property name, not the display name. You also can get the actual property names e.g. by selecting a shader asset and check the inspector

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.