我和我的朋友正在做一个VR实验。我们目前有代码生成16个随机颜色的按钮,我想有一些代码重置按钮并再次随机颜色约20次。我不知道该如何处理这种情况。下面是我们当前的代码:
public class ButtonManager : MonoBehaviour
{
Text[] labels;
Image[] images;
// Start is called before the first frame update
Color[] colors = new Color[]{
Color.red,Color.green,Color.yellow,Color.magenta,Color.gray,Color.black
};
void Start()
{
int chosenOne = Random.Range(0, 16);
Debug.Log(chosenOne.ToString());
labels = gameObject.GetComponentsInChildren<Text>();
int elemNum = 0;
foreach (Text txt in labels)
{
txt.text = elemNum.ToString();
elemNum++;
}
images = gameObject.GetComponentsInChildren<Image>();
elemNum = 0;
foreach (Image btn in images)
{
if (elemNum == chosenOne + 1)
{
btn.color = Color.blue;
}
else
{
btn.color = colors[Random.Range(0, 6)];
}
elemNum++;
}
}
// Update is called once per frame
void Update()
{
}
}
试试这个:
public class ButtonManager : MonoBehaviour
{
Text[] labels;
Image[] images;
// Start is called before the first frame update
Color[] colors = new Color[]{
Color.red,Color.green,Color.yellow,Color.magenta,Color.gray,Color.black
};
void Start()
{
labels = gameObject.GetComponentsInChildren<Text>();
foreach (Text txt in labels)
{
txt.text = elemNum.ToString();
elemNum++;
}
images = gameObject.GetComponentsInChildren<Image>();
foreach (Image btn in images)
{
btn.color = colors[Random.Range(0, colors.Length)];
}
}
// Update is called once per frame
void Update()
{
}
}