- Home /
Unity5 v Unity4 Shader array errors
in a CG shader with 4.6 the following methods for declaring arrays work, but in Unity 5.0 I am now getting either errors, or a black shader, and "upgrades" preventing compilation on various platforms. All the following variations previously worked, but now produce bad results.
float2[2] array;
float2[] array;
float2[2] array =
{
float2(1, 0),
float2(0, 1),
};
float2[] array =
{
float2(1, 0),
float2(0, 1),
};
Answer by sovalopivia · Apr 11, 2015 at 11:45 AM
It appears that array syntax is slightly different now. This is likely because D3D9 shader compiler was switched from Cg 2.2 to HLSL.
After a brief look at HLSL Syntax arrays are declared and initialised slightly differently. In CG in 4.6 you would declare an array in this format.
float[2] array;
float2[] array;
float2[2] array =
{
float2(1, 0),
float2(0, 1),
};
float2[] array =
{
float2(1, 0),
float2(0, 1),
};
in Unity 5.0 it now has to be declared like so.
float array[2];
// NOT ALLOWED! Array needs a length
float2 array[];
float2 array[2] =
{
float2(1, 0),
float2(0, 1),
};
float2 array[] =
{
float2(1, 0),
float2(0, 1),
};
For anyone in the future if you're getting errors try declaring the array without the comma after the last float in the array:
float2 _array[2] =
{
float2(0, 0),
float2(.5, .5) //no comma
};