Reputation: 88
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:
Prebuilt plane's inspector:
Shader Graph:
The script:
void Start()
{
GetComponent<MeshRenderer>().material.SetTexture("_MainTex", new TextureGenerator().GenerateNoiseTexture(256, 256));
}
Upvotes: 2
Views: 9890
Reputation: 361
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.
Upvotes: 2