I want to use a 2D Color array in a Texture2D.SetPixels function, so i need to convert the 2D array into a 1D array. How do i do this in C#?
Check the scripting reference.
All the information is there.
The code in c# would be something like this:
Color[,] 2dArray;
Texture2D texture;
//texture initialization
//array initialization
int width = 2dArray.GetLength(0);
int height = 2dArray.GetLength(1);
Color[] 1dArray = new Color[width*height];
Color color;
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
color = 2dArray[j,i];
1dArray[i*height+j] = color;
}
}
texture.SetPixels(1dArray);
texture.Apply();
I’m not sure how you would do it exactly in c#, but I can give the concept. Just use a nested forloop to grab each of the elements from the 2D array, and put them in a 1D array.
This will only work though if the 2D array has fixed number of elements in each spot