【发布时间】:2018-02-26 20:30:34
【问题描述】:
我按照 Unity 中关于制作测验游戏的官方教程制作了一个测验游戏多项选择。我正在尝试显示游戏中所有不正确/错误的答案,我该怎么做?这是代码。
用于显示问题
void ShowQuestion()
{
RemoveAnswerButtons();
bool questionChosen = false;
while (questionChosen != true) // While question chosen does not equal true
{
int random = Random.Range(0, questionPool.Length); // Choose a random number between 0 and the questionPool length
if (!questionIndexesChosen.Contains(random)) // If the new list doesn't contain the number
{
questionIndexesChosen.Add(random); // Add the number to the list
questionIndex = random; // Set the questionIndex to the number
questionChosen = true; // Set questionChosen to true to end the while loop
}
}
QuestionData questionData = questionPool[questionIndex]; // Get the QuestionData for the current question
questionText.text = questionData.questionText; // Update questionText with the correct text
问题----->按钮
for (int i = 0; i < questionData.answers.Length; i ++) // For every AnswerData in the current QuestionData...
{
GameObject answerButtonGameObject = answerButtonObjectPool.GetObject(); // Spawn an AnswerButton from the object pool
answerButtonGameObjects.Add(answerButtonGameObject);
answerButtonGameObject.transform.SetParent(answerButtonParent);
answerButtonGameObject.transform.localScale = Vector3.one;
AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton>();
answerButton.SetUp(questionData.answers[i]); // Pass the AnswerData to the AnswerButton so the AnswerButton knows what text to display and whether it is the correct answer
}
}
void RemoveAnswerButtons()
{
while (answerButtonGameObjects.Count > 0) // Return all spawned AnswerButtons to the object pool
{
answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
answerButtonGameObjects.RemoveAt(0);
}
}
点击回答按钮并显示正确和错误的结果。
public void AnswerButtonClicked(bool isCorrect)
{
if (isCorrect)
{
Debug.Log("Your Answer is Correct");
playerScore += currentRoundData.pointsAddedForCorrectAnswer; // If the AnswerButton that was clicked was the correct answer, add points
scoreDisplay.text = playerScore.ToString();
}
if (qNumber < questionPool.Length - 1) // If there are more questions, show the next question
{
qNumber++;
ShowQuestion();
}
else // If there are no more questions, the round ends
{
EndRound();
}
}
【问题讨论】:
-
@Chopi 我的意思是像 Merriam Webster。您将单击“查看结果”,然后将弹出所有带有答案的问题。
-
在这种情况下,您需要保存玩家回答的值和问题。