我正在尝试在 Unity (C#) 中绘制一个大而宽的自定义纹理(大约 2000x200 像素),其中每列中的所有连续垂直像素都在两个块中。第一个颜色相同的块,然后是第二个透明像素块。每列都有不同数量的彩色像素,并且彼此颜色不同。
实际上,这是一个垂直条形图,有几千个 1 像素宽的条。
这个纹理需要大约每秒更新一次。
我的初始实施速度很慢,并且在设置像素颜色时会导致帧速率峰值。因此,我正在寻找以最快的方式实现这一目标的想法。
我现在的代码大致如下:
一天的开始:
Color32[] m_pixelData = new Color32[width * height];
Texture2D m_texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
Color32 m_transparent = new Color32(0,0,0,0);
每次更新:
for (int x = 0; x < width; x++)
{
Color32 barColor = CalculateBarColorForThisColumn(x);
int barHeight = CalculateBarHeightForThisColumn(x);
for (int y = 0; y < height; y++)
{
int pixelIndex = y * width + x;
if (y <= barHeight)
m_pixelData[pixelIndex] = barColor;
else
m_pixelData[pixelIndex] = m_transparent;
}
}
m_texture.SetPixels32(m_pixelData);
是否有更快的方法来做到这一点,或者这是最好的方法并提高整个程序的性能,是否需要将这项工作推到后台线程?