Search Unity

How to change the float value inside the shader file with a c# script?

Discussion in 'Scripting' started by michaeltung, Sep 17, 2020.

  1. michaeltung

    michaeltung

    Joined:
    Aug 9, 2017
    Posts:
    24
    I know that to change the value of the UI text is
    textName.text = "A number of value"

    But in this situation I want to change the value of the float inside the shader file instead of the UI text.
    But I don't know what code can do this function.
    For example, I want to change this float value. What should I write in the c# script?

    Please answer me the CODE instead of concept! As I really know the concept.
    Tutorial videos are also welcome.
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    If you're using ShaderGraph, you create a property in the blackboard and give it a name. Then you set the value through a Material using that shader:

    Code (CSharp):
    1. Material myMaterial;
    2. float someValue;
    3.  
    4. myMaterial.SetFloat("MyPropertyName", someValue);
    I'm not sure what framework you're using from your screenshot, but most likely the principle is the same. You set a named property value through the material using the SetXXX methods, depending on the type of the property.
     
  3. michaeltung

    michaeltung

    Joined:
    Aug 9, 2017
    Posts:
    24
    Thanks, I'll try it.