【问题标题】:Unity Quiz Game displaying incorrect answersUnity 问答游戏显示错误答案
【发布时间】: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();
    }
}

【问题讨论】:

标签: c# unity3d unity5


【解决方案1】:

首先需要结果屏幕,其中包含信息。您可以创建一个名为 ResultsSren 的游戏对象,它的子对象的结果与此类似

ResultsScreen

  • Result1(ResultsScreen 的子级)
  • Reuslt2(ResultsScreen 的孩子)
  • Result3(ResultsScreen 的子级)
  • 您需要一些东西来存储问题和答案以最终显示结果。

    public List<bool> results = new List<bool>();// you can change this to something better like a Class that contains question ID, result, time used to response....
    public void AnswerButtonClicked(bool isCorrect)
    {
        results.Add(isCorrect); // the position in the array corresponts to the question. For example: Question 1 -> results[0]...Question2 -> results[1]
        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();
        }
    }
    

    当你完成回合/游戏时,你必须得出你的结果,你必须有办法做到这一点:

    • 为 UI 实例化一个预制件并使用该预制件(您需要放置在正确的位置)。
    • 您可以有一个包含位置的“硬编码”屏幕和一个包含对象的数组,如果每场比赛/回合的问题数量始终相同,这可能会很有用。

    脚本结果行为:

    public GameObject[] resultsItem; //Contains the UI GameObjects to print the results
    public GameObject gameObjectWithScriptResults; // Attach the gameobject with the script "AnswerButtonClicked"
    void OnEnable()
    {
       ShowResults();
    }
    void ShowResults(){
       for(int i = 0;i < gameObjectWithScriptResults.getComponent<Script>.results.Count;i++) //this results are from where you have the other script
       {
           //Draw your result[i]
           resultsItem[i].GetComponent<Text>().text = gameObjectWithScriptResults.getComponent<Script>.results[i];
       }
    }
    

    【讨论】:

    • 你能完成编码吗?呵呵相信我,我已经努力了很多次,但因为我还是个初学者,我很难分析这些代码。
    • 您好,先生,您能帮我看看这段代码吗?我真的很难过,你能给我完整的代码,我会分析一下吗?感谢先进
    • 3 个问题先生,我正在研究您之前所说的内容...但是您现在可以帮我吗?这听起来像我是一个新手,但我真的。我只是想让我的游戏可行。
    • @makemove 你现在可以看看我的回复吗?我想它差不多完成了,你需要改变的很少
    • 您必须在 ResultsBehaviour.resultsItem 中放入对将显示结果的 UI 项的 3 个引用。现在将显示真/假。我建议您不要将结果保存在该脚本中,而是对所有结果使用 Singleton。
    猜你喜欢
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多