But MainTex is a 2DArray Texture and I’m calling this on the c# side m_mesh.SetUVs(0, uvs); where uvs is declared as List uvs. I actually need to pass the Z index in the UV.
Understand in Surface Shaders the “uv_” prefix must be a float2. Internally this is letting the Surface Shader code know you want to use the first UV set and apply the offset and scale from the _MainTex. When it generates the real shader code (Surface Shaders are vertex fragment shader generators) it is adding a line like this.
But, Unity doesn’t know that’s what you want, and using an Input variable with the “uv_” prefix will always add the first bit of code. The solution is to use a different variable name and calculate the value on your own with a custom vertex function.
#pragma surface ... vertex:vert
...
Input {
float3 texcoord_MainTex; // not using uv_ prefix
}
float4 _MainTex_ST; // the scale offset data, the uv_ prefix adds this for you too, gets used by that TRANSFORM_TEX macro
...
void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input,o);
o.texcoord_MainTex = float3(TRANSFORM_TEX(v.texcoord.xy, _MainTex), v.texcoord.z);
}
Then use IN.texcoord_MainTex when sampling your texture array. You could also just skip the TRANSFORM_TEX part entirely if you don’t need or want the scale and offset. Just use:
o.texcoord_MainTex = v.texcoord.xyz;
You can name that Input variable anything you want really, btw, just as long as it doesn’t start with “uv” and isn’t one of the other special variable names.