我想制作一个包含10个输入字段的数组以获取用户输入,然后与另一个int数组元素进行比较。 我不明白如何调用输入字段的子文本元素。 谁能指导我使用c#来实现这一目标的最佳方法?谢谢 我试图显示10个问题,用户必须通过在输入字段中输入文本来回答这些问题。我想使输入字段数组存储用户的答案,另一个数组存储正确的答案。那么我想在用户单击检查按钮时进行比较。如果答案正确,我将用绿色或红色突出显示它。
public class YouTryTables : MonoBehaviour{
int n = 1;
public Text x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;
public int ans1, ans2, ans3, ans4, ans5, ans6, ans7, ans8, ans9, ans10;
public InputField[] allInputFields = new InputField[10]; //array of user Answers entered in input fields
public int[] allAnswers = new int[10];//array of correct answers
public void Start()
{
}
public void GetInput1(string i)
{
}
public void GenerateTable(int n)
{
x1.text = (n + " X " + 1 + " = ").ToString();
x2.text = (n + " X " + 2 + " = ").ToString();
x3.text = (n + " X " + 3 + " = ").ToString();
x4.text = (n + " X " + 4 + " = ").ToString();
x5.text = (n + " X " + 5 + " = ").ToString();
x6.text = (n + " X " + 6 + " = ").ToString();
x7.text = (n + " X " + 7 + " = ").ToString();
x8.text = (n + " X " + 8 + " = ").ToString();
x9.text = (n + " X " + 9 + " = ").ToString();
x10.text = (n + " X " + 10 + " = ").ToString();
for (int i = 0; i < allInputFields.Length; i++)
{
GameObject obj = GameObject.Find("MyObjectWithInputField");
allInputFields[i] = obj.GetComponent<InputField>();
}
for (int j = 0; j< allAnswers.Length; j++)
{
allAnswers[j] = ans1;
}
ans1 = (n * 1);
ans2 = (n * 2);
ans3 = (n * 3);
ans4 = (n * 4);
ans5 = (n * 5);
ans6 = (n * 6);
ans7 = (n * 7);
ans8 = (n * 8);
ans9 = (n * 9);
ans10 = (n * 10);
}
public void ComaprAnswers()
{
if (allInputFields[i] == allAnswers[j])
{
Text text = allInputFields.transform.Find("Text").GetComponent<Text>();
text.color = Color.green;
}
else
{
Text text = allInputFields.transform.Find("Text").GetComponent<Text>();
text.color = Color.red;
}
}
}
谢谢
答案 0 :(得分:0)
要使用InputField,您将需要UI命名空间:using UnityEngine.UI;
:
// Find GameObject with InputField component attached
GameObject obj = GameObject.Find("MyObjectWithInputField");
// Get the InputField component from the object
InputField inputField = obj.GetComponent<InputField>();
// Read the input value of the InputField
string text = inputField.text;
我不确定您要进行哪种比较,但是您只需循环遍历InputFields并读取上面显示的值即可。要获取场景中的所有InputField,可以使用
InputField[] allInputFields = FindObjectsOfType<InputField>();
已在OP中编辑了新代码,这是一个快速注释和少量修改,以解释代码中的一些错误。检查我添加的待办事项。
public class YouTryTables : MonoBehaviour
{
// TODO: This variable isn't used
int n = 1;
public Text x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;
// TODO: These variables aren't used
public int ans1, ans2, ans3, ans4, ans5, ans6, ans7, ans8, ans9, ans10;
public InputField[] allInputFields = new InputField[10]; //array of user Answers entered in input fields
public int[] allAnswers = new int[10];//array of correct answers
public void GenerateTable(int n)
{
// TODO: Doesn't need .ToString();, they're already strings
x1.text = (n + " X " + 1 + " = ").ToString();
x2.text = (n + " X " + 2 + " = ").ToString();
x3.text = (n + " X " + 3 + " = ").ToString();
x4.text = (n + " X " + 4 + " = ").ToString();
x5.text = (n + " X " + 5 + " = ").ToString();
x6.text = (n + " X " + 6 + " = ").ToString();
x7.text = (n + " X " + 7 + " = ").ToString();
x8.text = (n + " X " + 8 + " = ").ToString();
x9.text = (n + " X " + 9 + " = ").ToString();
x10.text = (n + " X " + 10 + " = ").ToString();
for (int i = 0; i < allInputFields.Length; i++)
{
// You loop though all 10 arrays but you assign the same component every time; the one on the GameObject you call MyObjectWithInputField
// You need to get the InputField from the correct GameObjects. Maybe you want to do this in a `public GameObject inputGameObjects` instead?
// TODO: Get InputField from correct GameObject
GameObject obj = GameObject.Find("MyObjectWithInputField");
allInputFields[i] = obj.GetComponent<InputField>();
}
for (int j = 0; j < allAnswers.Length; j++)
{
// TODO: ans1 isn't set yet, but you don't need to save it to a variable first, you can
// simply do allAnswers[j] = n * (j + 1);
allAnswers[j] = ans1;
}
// TODO: You can remove this if you did allAnswers[j] = n * (j + 1); above
ans1 = (n * 1);
ans2 = (n * 2);
ans3 = (n * 3);
ans4 = (n * 4);
ans5 = (n * 5);
ans6 = (n * 6);
ans7 = (n * 7);
ans8 = (n * 8);
ans9 = (n * 9);
ans10 = (n * 10);
}
// TODO: Typo Comapr -> Compare
// I changed this method to return a bool if all answers were correct
public bool ComaprAnswers()
{
bool allAnswersCorrect = true;
// TODO: You want to loop through all questions here
// for (int i = 0; i < allInputFields.Length; i++)
// TODO: You're comparing string to int, allAnswers should be set to string[] and its setters be made .ToString()
if (allInputFields[i] == allAnswers[j])
{
Text text = allInputFields.transform.Find("Text").GetComponent<Text>();
text.color = Color.green;
}
else
{
Text text = allInputFields.transform.Find("Text").GetComponent<Text>();
text.color = Color.red;
allAnswersCorrect = false;
}
return allAnswersCorrect;
}
}
看到截图后,这就是您获得答案值的方式:
var answerParent = GameObject.Find("AnswerPanel");
var answerObject = answerParent.trasnform.GetChild(n); // Where n is 0 - 9
var answerValue = answerObject.GetComponent<InputField>().text;
答案 1 :(得分:0)
var ret = FirstArray.Any(x => SecondArray.Any(y => x == y))