Pass to ComputeShader my define value

I have the Compute Shader:

#pragma kernel CSRed
#pragma kernel CSGreen
#pragma kernel CSBlue

RWTexture2D<float4> _Texture;

//#define SIZE 32

[numthreads(SIZE,SIZE,1)]
void CSRed (uint2 id : SV_DispatchThreadID)
{
    float w, h;
    _Texture.GetDimensions(w, h);
    _Texture[id] = float4(id.x/w, 0, 0, 1);
    #endif
}


[numthreads(SIZE,SIZE,1)]
void CSGreen (uint2 id : SV_DispatchThreadID)
{
    float w, h;
    _Texture.GetDimensions(w, h);
    _Texture[id] = float4(0, id.x/w, 0, 1);
}


[numthreads(SIZE,SIZE,1)]
void CSBlue (uint2 id : SV_DispatchThreadID)
{
    float w, h;
    _Texture.GetDimensions(w, h);
    _Texture[id] = float4(0, 0, id.x/w, 1);
}

And I want to pass #define SIZE 32 from C# code.
How can I do it?

In the compute shader add:

int Size;

And in your C# code (before you call Dispatch() ):

computeShader.SetInt("Size", 32);

There are other functions like SetFloat etc as well…

You should search for structured ComputeBuffers to pass in complex structures (e.g. a number of different int / float types) from c# into the shader.

I really dont think theres a way to do this because of how shaders are compiled.
The numthreads parameters are required to be “Compile-time constants” basically.
Which is why youre not allowed to do anything that would modify them after the shader is built.
And unity builds the shaders as soon as it sees youve made changes to them.
You can do this sort of thing not using unity where you can define your own build macros but that requires manually building the shader yourself at runtime or whenever you want to set the value i guess.
Not sure if theres a way to do that in unity tho because it has its whole own system for building them and i doubt theres any way to expose that to the user for manual building but i would love to be proven wrong on that.

I guess i would recommend just setting the value in the shader and in C# you can use ComputeShader.GetKernelThreadGroupSizes to get the value you set in the shader and handle it that way instead. obviously not optimal but what are ya gonna do. If anything i would just want a function on compute shaders to be able to get defines in the shader from C# so i can handle the change in code based on a define in the shader.