在列表中设置活动游戏对象

Set active gameobjects in a list

我正在为 Android 二维应用程序制作菜单,我在多个菜单中有一堆 UI 面板,当我按下下一个 right 或前一个 left 按钮脚本应该设置下一个面板活动和停用前一个面板,我试过用 public class 和 gameobjects但这没有用,我认为列表应该有用。

在 Unity 编辑器中,我将大小设置为 4,并将 4 个面板拖到脚本中 我收到了这个错误:

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index

这是脚本的相关部分:

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

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

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 等于 ShipUpgradeRightShipUpgradeLeftshipUpgradeSelected 的值减少(因此小于零)。然后您尝试访问索引小于零的列表项。

但这是第一个问题。

此外,您不限制(或不循环)shipUpgradeSelected 的值。例如你有

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

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

此代码完美运行:

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 帮我解决问题