0

我正在为 Android 2d 应用程序制作一个菜单,我在多个菜单中有一堆 UI 面板,当我按下下一个right或上一个left按钮时,脚本应该设置下一个面板处于活动状态并停用前一个面板,我尝试过这样做这与带有游戏对象的公共类一起使用,但这不起作用,并且认为列表应该起作用。

在 Unity 编辑器中,我将大小设置为 4 并将 4 个面板拖到脚本中,但出现此错误:

ArgumentOutOfRangeException:参数超出范围。参数名称:索引

这是脚本的相关部分:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MenuControl : MonoBehaviour
{
    //Buttons
    public GameObject ShipUpgradeRight;
    public GameObject ShipUpgradeLeft;

    //Ships
    private int shipUpgradeSelected = 0;
    List<GameObject> ShipList = new List<GameObject>();

    public void Keyword (string keyword)
    {
        if (keyword == "ShipUpgradeLeft") {
            shipUpgradeSelected--;
            ShipList[shipUpgradeSelected].SetActive (true);
            ShipList[shipUpgradeSelected+1].SetActive (false);
        }

        if (keyword == "ShipUpgradeRight") {
            shipUpgradeSelected--;
            CheckShip ();
            ShipList[shipUpgradeSelected].SetActive (true);
            ShipList[shipUpgradeSelected-1].SetActive (false);
        }   
    }
}
4

2 回答 2

2

根据您的评论和实际问题,我看到了一个可能的问题。

的价值shipUpgradeSelected永远不会增加。此外,shipUpgradeSelected具有零作为初始值。

public void Keyword (string keyword)
{
    if (keyword == "ShipUpgradeLeft") {
        shipUpgradeSelected--;
        ShipList[shipUpgradeSelected].SetActive (true);
        ShipList[shipUpgradeSelected+1].SetActive (false);
    }

    if (keyword == "ShipUpgradeRight") {
        shipUpgradeSelected--;
        CheckShip ();
        ShipList[shipUpgradeSelected].SetActive (true);
        ShipList[shipUpgradeSelected-1].SetActive (false);
    }   
}

keyword等于ShipUpgradeRightShipUpgradeLeft的值shipUpgradeSelected减小时(因此它小于零)。然后您尝试访问索引小于零的列表项。

但这是第一个问题。

此外,您不钳制(或不循环)shipUpgradeSelected. 所以例如你有

if (keyword == "ShipUpgradeRight") {
    shipUpgradeSelected++;
    CheckShip ();
    ShipList[shipUpgradeSelected].SetActive (true);
    ShipList[shipUpgradeSelected-1].SetActive (false);
}

如果调用Keyword("ShipUpgradeRight");五次(例如),则值为shipUpgradeSelected5。再次超出范围。所以你需要决定如何限制这个变量的值。

于 2015-01-31T20:39:59.380 回答
0

此代码完美运行:

public void Keyword(string keyword) {
         if (keyword == "ShipUpgradeLeft") {
             shipUpgradeSelected--;
             if (shipUpgradeSelected < 0) {
                 shipUpgradeSelected = 0;
                 return;
             }

             ShipList[shipUpgradeSelected].SetActive(true);
             if (shipUpgradeSelected + 1 < ShipList.Count) ShipList[shipUpgradeSelected + 1].SetActive(false);
             else ShipList[0].SetActive(false);
         }
         if (keyword == "ShipUpgradeRight") {
             shipUpgradeSelected++;
             if (shipUpgradeSelected >= ShipList.Count) {
                 shipUpgradeSelected = ShipList.Count - 1;
                 return;
             }

             ShipList[shipUpgradeSelected].SetActive(true);
             if (shipUpgradeSelected > 0) ShipList[shipUpgradeSelected - 1].SetActive(false);
             else ShipList[ShipList.Count-1].SetActive(false);
         }
     }

但如果我要重新开始,我会这样做:

private void Start()
 {
     ShipUpgradeLeft.GetComponent<Button>().onClick.AddListener(() => { Previous(); });
     ShipUpgradeRight.GetComponent<Button>().onClick.AddListener(() => { Next(); });
 }
 public void Next()
 {
     ShipList[shipUpgradeSelected].SetActive(false);
     shipUpgradeSelected = Mathf.Clamp(shipUpgradeSelected++, 0, ShipList.Count - 1);
     ShipList[shipUpgradeSelected].SetActive(true);
 }
 public void Previous()
 {
     ShipList[shipUpgradeSelected].SetActive(false);
     shipUpgradeSelected = Mathf.Clamp(shipUpgradeSelected--, 0, ShipList.Count - 1);
     ShipList[shipUpgradeSelected].SetActive(true);
 }

感谢 walther 和 d12frosted 帮助我解决问题

于 2015-02-01T14:15:21.123 回答