c# - 如何禁用未选择的阵列并启用选择的阵列?
问题描述
有人可以帮我如何禁用未选择的数组并启用选定的数组吗?我在如何做到这一点上有问题..我是统一的新手,我真的在学习它时遇到了问题。我在其他平台上搜索过,但没有得到答案。所以请不要因为我还不知道的事情而抨击我:(
无论如何谢谢你的回答...
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);
}
}
解决方案
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);
}
推荐阅读
- mysql - 如何将 1 个表中的 2 个值加入 1 行?
- django - 如何在 django 中创建自动生成的会员 ID
- twilio - 将变量从 slack webhook 传递到 Autopilot
- url - 使用移动设备查看时,如何使桌面网站上的帖子网址重定向到移动网站上相应的帖子网址,反之亦然
- html - 在 CSS 掩码后面添加图像
- macros - 在 IAR 嵌入式工作台间谍宏中,是否有断点函数名称的变量?
- matlab - 使用有限差分求解 PDE
- python-3.x - 使用sklearn Kmeans时如何避免Kmean局部最优
- javascript - 如何导入远程模态的 javascript
- android - 如何为 google play 控制台创建包名称