【Unity】問答題實作

如果我們想要使用Unity來建立一個問答題的小測驗,首先要佈置好場景,接著準備好想要問的問題以及這些問題的答案。以下就以是非題作為範例,說明如何在Unity中建立這樣子的應用程式。

請先佈置好所需要的場景,如下所示:

這裡面使用了以下的這些物件:

然後準備兩支程式,分別是StartConversation以及QA,以下是StartConversation的內容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StartConversation : MonoBehaviour
{
    public GameObject player;
    public string[] StartDialog;
    public Text Dialog;
    public Image MainBoard;
    public Image QABoard;
    public Button LeftButton, RightButton;
    public Text QAText;
    private IEnumerator StartChat()
    {
  
        foreach(string sen in StartDialog)
        {
            Dialog.text = sen;
            yield return new WaitForSeconds(3f);
        }
        player.SetActive(true);
        MainBoard.enabled = false;
        Dialog.enabled = false;
        QABoard.gameObject.SetActive(true);
        QAText.gameObject.SetActive(true);
        LeftButton.gameObject.SetActive(true);
        RightButton.gameObject.SetActive(true);

    }
    // Start is called before the first frameupdate
    void Start()
    {
        LeftButton.gameObject.SetActive(false);
        RightButton.gameObject.SetActive(false);
        QAText.gameObject.SetActive(false);
        QABoard.gameObject.SetActive(false);
        player.SetActive(false);
        StartCoroutine(StartChat());
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

以下則是QA的內容:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;

public class QA : MonoBehaviour
{
    public AudioSource SoundEffect;
    public AudioClip[] Soundclips;
    
    public GameObject player;
    public Animator playerAnimator;
    public Text QAItem;
    public Text Score;
    public Image QABoard;
    public string[] Items;
    public bool[] Answers;
    private int index = 0;
    private int score = 0;
    public Button LeftButton, RightButton;

    void Start()
    {
        QAItem.enabled = true;
        LeftButton.enabled = true;
        RightButton.enabled = true;
        index = 0;
        score = 0;
        Score.text = score.ToString();
        QAItem.text = Items[index];
        
    }

    IEnumerator Next()
    {
        index++;
        LeftButton.gameObject.SetActive(false);
        RightButton.gameObject.SetActive(false);
        yield return new WaitForSeconds(3f);
        LeftButton.gameObject.SetActive(true);
        RightButton.gameObject.SetActive(true);
        if (index< Items.Length)
        {
            QAItem.text = Items[index];
        }
        else
        {
            QAItem.gameObject.SetActive(false);
            LeftButton.gameObject.SetActive(false);
            RightButton.gameObject.SetActive(false);
            QAItem.gameObject.SetActive(false);
            QABoard.gameObject.SetActive(false);
        }
    }

    public void ButtonYes()
    {
        if (Answers[index]) {
            score += 1;
            Score.text = score.ToString();
            playerAnimator.SetTrigger("Win");
            PlayWinSound();
        } else
        {
            playerAnimator.SetTrigger("Lose");
            PlayLoseSound();
        }
        StartCoroutine(Next());

    }

    public void ButtonNo()
    {
        if (!Answers[index])
        {
            score += 1;
            Score.text = score.ToString();
            playerAnimator.SetTrigger("Win");
            PlayWinSound();
        } else
        {
            playerAnimator.SetTrigger("Lose");
            PlayLoseSound();
        }
        StartCoroutine(Next());
    }

    void PlayWinSound()
    {
        SoundEffect.clip = Soundclips[0];
        SoundEffect.Play();
    }

    void PlayLoseSound()
    {
        SoundEffect.clip = Soundclips[1];
        SoundEffect.Play();
    }
}

由於在程式中使用了許多的public物件,所以當我們分別把QA放到UnityChan以及把StartConversation放到Main Camera作為組件之後,還要再拖曳一些物作進行連結。以下是Main Camera中的StartConversation要連結的物件:

以下則是在UnityChan中的QA所需要的連結物件:

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *