【发布时间】:2019-07-15 00:51:30
【问题描述】:
我正在显示 4 个问题,用户必须从底部给定的答案按钮数组中选择答案。要回答第一个问题 (22 + 6),用户必须选择旁边给出的黄色按钮,然后单击底部列表中的任何按钮。单击任何答案按钮时,我想将该值分配给提供的黄色按钮。然后对于下一个问题(16 + 7),用户必须再次选择另一个黄色按钮并从列表中单击答案按钮。我无法将单击的答案按钮的值分配给选定的黄色按钮。谁能建议我如何实现这一目标?
public class WithButtons : MonoBehaviour
{
Text[] userInputs;
public Text aValue = null, bValue = null, cValue = null, dValue = null;
int a, b, c, d;
public GameObject[] ansButtons;
private GameObject tempGO;
Text[] answers;
private Button myButton;
//-----------------------------------------------------------------------//
void Start()
{
PuzzleMaker();
}
public void PuzzleMaker()
{
a = Random.Range(0, 25);
b = Random.Range(0, 25);
c = Random.Range(0, 25);
d = Random.Range(0, 25);
aValue.text = a.ToString();
bValue.text = b.ToString();
cValue.text = c.ToString();
dValue.text = d.ToString();
ansButtons[0].GetComponentInChildren<Text>().text = (a + b).ToString();
ansButtons[1].GetComponentInChildren<Text>().text = (c + d).ToString();
ansButtons[2].GetComponentInChildren<Text>().text = (a + c).ToString();
ansButtons[3].GetComponentInChildren<Text>().text = (b + d).ToString();
ansButtons[4].GetComponentInChildren<Text>().text = (a + a).ToString();
ansButtons[5].GetComponentInChildren<Text>().text = (b + b).ToString();
ansButtons[6].GetComponentInChildren<Text>().text = (c + c).ToString();
ansButtons[7].GetComponentInChildren<Text>().text = (d + d).ToString();
}
//check which button is clicked
public void WhichButtonIsClicked(string buttonName)
{
Debug.Log(buttonName);
if (buttonName == "AB")
{
//assign value of clicked button to ? button Q1
//userInput1.text =
}
else if (buttonName == "CD")
{
//assign value of clicked button to ? button Q2
}
else if (buttonName == "AC")
{
//assign value of clicked button to ? button Q3
}
else if (buttonName == "BD")
{
//assign value of clicked button to ? button Q4
}
}
public void OnEnable()
{
myButton = GetComponent<Button>();
myButton.onClick.AddListener(() => { ChangeTarget(); });
}
public void ChangeTarget()
{
int i;
for (i = 0; i < ansButtons.Length; i++)
{
userInputs[i].text =
UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.GetComponentInChildren<Text>().text.ToString();
}
}
}
【问题讨论】:
-
@Jannis 我已经分配了 4 个答案和 4 个随机值。他们有更好的方法吗?我是统一的新手,所以不确定。
-
我先分配了 8 个值,然后我将这些值改组,所以我每次都不会在同一个位置得到答案
-
哦,好的。然后我将删除循环并简单地分配值并保持 shuffle.wil 可以吗?
-
删除了谢谢。但是你能指导我将点击的按钮文本分配给选定的按钮吗
标签: c# arrays unity3d button text