首页 > 解决方案 > 如何禁用未选择的阵列并启用选择的阵列?

问题描述

有人可以帮我如何禁用未选择的数组并启用选定的数组吗?我在如何做到这一点上有问题..我是统一的新手,我真的在学习它时遇到了问题。我在其他平台上搜索过,但没有得到答案。所以请不要因为我还不知道的事情而抨击我:(

无论如何谢谢你的回答...

using System.Collections.Generic;
using UnityEngine;

public class ArrayObject : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject[] CustomPanel;
    private int counter = 0;
    public GameObject nextBtn;
    public GameObject prevBtn;
    void Start()
    {

    }
    private void checkBtns()
    {
        if (counter < 1)
        {
            prevBtn.SetActive(false);
        }
        else
        {
            prevBtn.SetActive(true);
        }
        if (counter > CustomPanel.Length - 2)
        {
            nextBtn.SetActive(false);
        }
        else
        {
            nextBtn.SetActive(true);
        }
    }

    public void next()
    {
        counter++;
        checkBtns();
        CustomPanel[counter].SetActive(true);
    }
    public void prev()
    {
        counter--;
        checkBtns();
        CustomPanel[counter].SetActive(true);
    }

}

标签: c#unity3d

解决方案


public void next()
{
    counter++;
    checkBtns();
    for (int i = 0; i < CustomPanel.Length; i++)
    {
        if (i == counter)
            CustomPanel[i].SetActive(true);
        else
            CustomPanel[i].SetActive(false);
    }
}

或者

public void next()
{
    counter++;
    checkBtns();
    for (int i = 0; i < CustomPanel.Length; i++)
    {
        CustomPanel[i].SetActive(i == counter);
    }
}

另一种选择:不遍历数组中的所有对象:

public void next()
{
    // Disable the button that has been selected
    // before the click.
    // The counter is currently = that button index.
    CustomPanel[counter].SetActive(false);
    counter++;
    checkBtns();
    // Now the counter = the new button index,
    // enable the newly selected button:
    CustomPanel[counter].SetActive(true);
}

推荐阅读