我创建了一对导航按钮(下一个和上一个)来浏览图像数组,但是在数组的开头和结尾发生了一些stange。我必须两次点击下一个或上一个按钮才能前进或后退。一旦我在数组的开头或结尾处这样做,一切正常。有人能看出出了什么问题吗?
public Texture2D tex;
public string[] galleryImages;
public Button nextBtn;
public Button prevBtn;
int randomIndex;
int currentIndex = 0;
public Color inactiveColor = new Color(0.2F, 0.3F, 0.4F, 0.5F);
string[] arctopithecusImages;
string[] arctopithecusPNGImages;
string[] gulonImages;
string[] scythianWolfImages;
string[] simivulpaImages;
string[] succorathImages;
string[] tatusImages;
// System.Random rand = new System.Random();
// Create a master Array of all image files located in all Image locations
void Start()
{
// Build Gallery Arrays
arctopithecusImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.jpg");
arctopithecusPNGImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/ARCTOPITHECUS/", "*.png");
gulonImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/GULON/", "*.jpg");
scythianWolfImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/SCYTHIAN-WOLF/", "*.png");
simivulpaImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/SIMIVULPA/", "*.png");
succorathImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/SUCCORATH/", "*.png");
tatusImages = Directory.GetFiles(@"/Users/kenmarold/Screenshots/TATUS/", "*.png");
// Concatenate all Folder Array into single Array
galleryImages =
arctopithecusImages.Concat(arctopithecusPNGImages)
.Concat(gulonImages)
.Concat(scythianWolfImages)
.Concat(simivulpaImages)
.Concat(succorathImages)
.Concat(tatusImages)
.ToArray();
Debug.Log(galleryImages.Length);
}
IEnumerator loader(int indexNum)
{
WWW www = new WWW("file://" + galleryImages[indexNum]); // get the first file from disk
yield return www; // Wait unill its loaded
tex = new Texture2D(512,512); // create a new Texture2D
www.LoadImageIntoTexture(tex); // put the image file into the new Texture2D
Rect rct = new Rect(0, 0, tex.width, tex.height);
Vector2 pvt = new Vector2(0.5f, 0.5f);
GameObject screenShotImg = GameObject.FindGameObjectWithTag("GalleryImgHolder");
Image img = screenShotImg.GetComponent<Image>();
img.sprite = Sprite.Create(tex, rct, pvt);
}
public void LoadImg()
{
if(tex == null)
{
StartCoroutine("loader", currentIndex);
Debug.Log(currentIndex);
} else {
currentIndex = Random.Range(0,galleryImages.Length);
StartCoroutine("loader", currentIndex);
Debug.Log(currentIndex);
}
}
public void nextImage()
{
Debug.Log(currentIndex);
// Increment through gallery
if(currentIndex != galleryImages.Length - 1)
{
StartCoroutine("loader", currentIndex++);
prevBtn.enabled = true;
prevBtn.interactable = true;
nextBtn.enabled = true;
nextBtn.interactable = true;
// nextBtn.image.color = new Color(255f,255f,255f,1f);
} else if (currentIndex == galleryImages.Length - 1){
nextBtn.enabled = false;
nextBtn.interactable = false;
// nextBtn.image.color = new Color(255f,255f,255f,.5f);
}
}
public void prevImage()
{
Debug.Log(currentIndex);
// Decrement through gallery
if(currentIndex != 0)
{
StartCoroutine("loader", currentIndex--);
nextBtn.enabled = true;
nextBtn.interactable = true;
prevBtn.enabled = true;
prevBtn.interactable = true;
// prevBtn.image.color = new Color(255f,255f,255f,1f);
} else if (currentIndex == 0){
prevBtn.enabled = false;
prevBtn.interactable = false;
// prevBtn.image.color = new Color(255f,255f,255f,.5f);
}
}
}
答案 0 :(得分:2)
如果你想等待异步方法完成,学习如何使用async / await,那么你的产量回报就是问题,不应该如何使用它。