Unity accessing HLSL shader parameters with C# Input Controller

Heran Zhou
2 min readMar 22, 2020

--

Since a shader is made of different parameters, we can make interesting gameplay elements by adjusting them in game. Here is a documentation of how I control my shader in real-time with keyboard controls.

Unity engine uses HLSL/Cg for shaders, and C# for most other things.

In the shader HLSL script, inside Properties{} type in the parameters that you want to expose to the inspector, so that you can see what the values are in the game editor. Below in the blue box I indicated, declare those variables so that the script can access them later.

As you can see in the screenshot below, the exposed parameters show up.

Then make a controller C# script and attach it to the same game object that has the shader script, so that you can get the Renderer component. I highlighted the codes that I used to access and change the shader parameter in the screenshot below.

Because we declared “_Offset” and other variables earlier in the shader script, we can now access it and change its value.

https://hzhou17.itch.io/color-game This link is a demo to my project, in which you can use UI buttons and press and hold D, R, G, and B keys to adjust the color of the shader.

--

--

Heran Zhou
Heran Zhou

No responses yet