为 Unity 问答游戏创建问题数据库

问题描述 投票:0回答:1

我是一名初级开发者,目前正在使用 Unity 开发一款问答游戏。这都是关于日语书写脚本——平假名和片假名。玩家获得一个日语字符和 4 种可能的发音。他们的工作是选择正确的发音。到目前为止,我的所有问题(字符)和可能的答案(发音)都在检查器中。然而,这意味着有 2 个类别,有 71 个问题,每个类别有 71 个可能的答案(超过 10k 个字符串),因此添加新字符会导致极其缓慢且低效。我该如何改进?

我主要使用了 Brackey 的测验教程和 Youtube 甚至 ChatGPT 上的一些其他教程。

我尝试将问题与答案分开,以便将它们分开,但我真的不知道该怎么做。

这是我的主要代码:

public class GameManager : MonoBehaviour
{
    public Animator playerAnimator;
    public Animator enemyAnimator;

    public GameObject healthBar;
    public GameObject enemyHealthBar;

    public Question[] katakanaQuestions;
    public Question[] hiraganaQuestions;

    [SerializeField] private float timeBetweenQuestions;

    private static List<Question> unansweredQuestions;

    private Question currentQuestion;

    [SerializeField] private TMP_Text factText;
    [SerializeField] private Button[] answerButtons;
  
    private int correctAnswerIndex;

    private bool isAnsweringQuestion = false;

    void Start()
    {
        LoadSelectedCategory();
        SetCurrentQuestion();
    }

    private void LoadSelectedCategory()
    {
        bool isHiraganaSelected = PlayerPrefs.GetInt("IsHiraganaSelected", 1) == 1;

        Question[] selectedQuestions = isHiraganaSelected ? hiraganaQuestions : katakanaQuestions;
        
        unansweredQuestions = selectedQuestions.ToList();
    }

    void SetCurrentQuestion()
    {
        int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
        currentQuestion = unansweredQuestions[randomQuestionIndex];

        factText.text = currentQuestion.question;

        
        List<string> answerChoices = new List<string>();    // Tworzę listę możliwych odpowiedzi z poprawną odpowiedzią i trzema losowymi błędnymi odpowiedziami

        answerChoices.Add(currentQuestion.answers[currentQuestion.correctAnswerIndex]);  // Dodaję poprawną odpowiedź
        List<string> incorrectAnswers = currentQuestion.answers.ToList();
        
        incorrectAnswers.RemoveAt(currentQuestion.correctAnswerIndex);       // Usuwam poprawną odpowiedź
        for (int i = 0; i < 3; i++)      // Dodaję resztę trzech przypadkowych odpowiedzi
        {
            int randomIndex = Random.Range(0, incorrectAnswers.Count);
            answerChoices.Add(incorrectAnswers[randomIndex]);
            incorrectAnswers.RemoveAt(randomIndex);
        }

        // Mieszanie
        int n = answerChoices.Count;
        while (n > 1)
        {
            n--;
            int k = Random.Range(0, n + 1);
            string temp = answerChoices[k];
            answerChoices[k] = answerChoices[n];
            answerChoices[n] = temp;
        }

        // Tekst dla przycisków
        for (int i = 0; i < answerButtons.Length; i++)
        {
            answerButtons[i].GetComponentInChildren<TMP_Text>().text = answerChoices[i];
        }

        // Poprawny indeks przycisku
        correctAnswerIndex = answerChoices.IndexOf(currentQuestion.answers[currentQuestion.correctAnswerIndex]);

    }

    public void CheckAnswer(int answerIndex)
    {
        if (isAnsweringQuestion)
        {
            Debug.Log("Wait for the next question!");
            return;
        }

        isAnsweringQuestion = true; // Gracz aktualnie odpowiada

        StartCoroutine(CheckAnswerCoroutine(answerIndex));
    }

    private IEnumerator CheckAnswerCoroutine(int answerIndex)
    {
        //yield return new WaitForSeconds(0.3f);

        // Store the original colors of the buttons
        Color[] originalColors = new Color[answerButtons.Length];
        for (int i = 0; i < answerButtons.Length; i++)
        {
            originalColors[i] = answerButtons[i].GetComponent<Image>().color;
        }

        for (int i = 0; i < answerButtons.Length; i++)
        {
            if (i == correctAnswerIndex)
            {
                // Change the color of the correct answer button to a specific green color (0.1f, 0.8f, 0.1f)
                answerButtons[i].GetComponent<Image>().color = new Color(0.1f, 0.8f, 0.1f);
            }
            else if (i == answerIndex)
            {
                // Change the color of the selected (incorrect) answer button to a specific red color (0.8f, 0.1f, 0.1f)
                answerButtons[i].GetComponent<Image>().color = new Color(0.8f, 0.1f, 0.1f);
            }
        }

        yield return new WaitForSeconds(1f); // Wait for a short time to show the button colors

        // Reset button colors to their original state
        for (int i = 0; i < answerButtons.Length; i++)
        {
            answerButtons[i].GetComponent<Image>().color = originalColors[i];
        }

        if (answerIndex == correctAnswerIndex)
        {
            Debug.Log("Correct!");

            Attack();
            enemyHealthBar.GetComponent<EnemyHealthBar>().TakeDamage(1);
        }
        else
        {
            Debug.Log("Wrong answer");

            GetHit();
            healthBar.GetComponent<HealthBar>().TakeDamage(1);
        }

        yield return new WaitForSeconds(timeBetweenQuestions); // Wait for the next question

        isAnsweringQuestion = false;

        SetCurrentQuestion();
    }
[System.Serializable]
public class Question
{

    public string question;
    public int correctAnswerIndex;
    public string[] answers;

}

[System.Serializable]
public class KatakanaQuestion
{
    public string question;
    public string[] answers;
    public int correctAnswerIndex;
}
c# unity-game-engine game-development
1个回答
0
投票

简化事情的一种方法是只拥有一个片假名字符列表、一个平假名字符列表和一个发音列表,所有这些都按相同的顺序排列。然后您可以选择一个随机索引并使用该索引抓取字符和发音,然后为不正确的选项选择其他 3 个随机索引。

© www.soinside.com 2019 - 2024. All rights reserved.