0
votes

I am trying to set the texture of a shader graph material and am having no luck. After following some tutorials and some googling I can't seem to get it working.

So far I have set my scene up with a plane i generated via code and a pre-built one and have applied the material to both of these. I then created a script to set the texture of the MeshRenderer to "_MainTex" as I saw mention several times while googling. I also set the material texture property to "_MainTex".

I also tried the Reference name as well but that also didn't work.

So how do you set the texture of a shader graph material with Universal Render Pipeline via code?

The two planes ingame:

The two planes

Prebuilt plane's inspector:

The prebuilt plane's inspector

Shader Graph:

The shader graph

The script:

void Start()
{
    GetComponent<MeshRenderer>().material.SetTexture("_MainTex", new TextureGenerator().GenerateNoiseTexture(256, 256));
}
1

1 Answers

2
votes

You need to fill out the "Reference" section for this property.

It is found in Graph Inspector -> Node Settings. (usually in the top right of the ShaderGraph)

Be sure to change "Texture2D_234E865" to "_MainTex" there. Verify the asset saved by clicking on the shader and looking in the inspector. Under Properties it should list _MainTex. Then you can use your SetTexture code or simply call material.mainTexture, which does the same thing.