导航按钮需要按两次才能在数组中前进

本文关键字:数组 按钮 按两次 导航 | 更新日期: 2023-09-27 18:04:47

我创建了一对导航按钮(下一个和上一个)来浏览图像数组,但是在数组的开始和结束处发生了一些奇怪的事情。我必须点击下一个或上一个按钮两次向前或向后移动。一旦我在数组的开头或结尾这样做,一切都会正常工作。有人能看出哪里出了问题吗?

    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);
        }
    }
}

导航按钮需要按两次才能在数组中前进

你的yield return是问题,这不是它应该如何使用的,如果你想等待异步方法完成,学习如何使用async/await