我有很多图片存储在screenshots文件夹中。因此,这个脚本将从文件夹中调用图像而不应用。但问题是,图像只显示一张。我的意思是它们没有像幻灯片或淡入淡出那样进入下一张图像。
然后我不停地收到这个错误消息:
IndexOutOfRangeException: Index was outside the bounds of the array.
它指向这段代码:
ImageHolder[i].GetComponent<RawImage>().texture = thisTexture;
完整代码如下:
Texture2D thisTexture;
byte[] bytes;
string fileName;
public GameObject[] ImageHolder = new GameObject[1];
void Start()
{
var imagesToLoad = Directory.GetFiles(Application.dataPath + "/screenshots", "*.png");
for (int i = 0; i < imagesToLoad.Length; i++)
{
thisTexture = new Texture2D(100, 100); //NOW INSIDE THE FOR LOOP
fileName = imagesToLoad[i];
bytes = File.ReadAllBytes(fileName);
thisTexture.LoadImage(bytes);
thisTexture.name = fileName;
// This line
ImageHolder[i].GetComponent<RawImage>().texture = thisTexture;
}
}
}
我在这里附上输出结果。我有4个
RawImage
。该图片只是显示并卡住了。