In my shader, I declare a non-serialized array of float4s and execute a block of code on the elements:
uniform float4 _HandPos[5];
v2f vert(appdata v)
{
float distH;
for (float i = 0; i < _HandPos.Length; i++)
{
/*if element has not been assigned, assuming an empty element is NaN,
check distance between vertex and element*/
if (!isnan(_HandPos[i]))
{
distH = distance(_HandPos[i], v.vertex);
}
//do something with distH here
}
}
Every frame, an array in a C# script is sent to _HandPos. It doesn't always send values for all 5 elements, so I have to filter the empty ones out. Here, I assumed that an empty element is NaN, but that doesn't seem to work.
If you don't write a value each frame, whatever value was set last will be preserved. Alternatively, you could use an array of int values that correspond to which _HandPos elements need to be reprocessed.
Keep in mind that a branching statement (such as if()) on the GPU is generally going to cause the GPU to execute both branches then discard one value -- so if this is meant to be an optimization it's probably not going to have the desired effect.