Unity答题面板的制作方式

文章详细介绍了如何在Unity中使用AnswerPanel组件构建答题界面,包括单选或多选题的实现,以及固定答题面板的代码展示,涉及组件拖放和音频反馈功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)内容动态面板 

一、答题界面简单搭建

主要为5个组件,一个Text作为题目,Scroll View作为装载选择题的容器,Toggle作为选择模板,两个Button,一个作为关闭面板按钮,一个作为提交按钮。

二、代码展示 

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

public class AnswerPanel : MonoBehaviour
{
    public Text topicText;
    public Button closePanelBtn;
    public GameObject choiceContentExample;
    public Button confirmBtn;
    [Header("是否是多选题")]
    public bool isMultipleChoice;
    /// <summary>
    /// 2的0次=1  2的1次=2
    /// </summary>
    public int currentResult;

    
    private int selectResult;
    private List<Toggle> choiceContentToggles = new List<Toggle>();
    private void Awake()
    {
        choiceContentExample.SetActive(false);
        closePanelBtn.onClick.AddListener(() =>
        {
            this.gameObject.SetActive(false);
            topicText.text = string.Empty;
            selectResult = 0;
            currentResult = 0;
            foreach (var choiceContentToggle in choiceContentToggles)
                Destroy(choiceContentToggle.gameObject);
            choiceContentToggles.Clear();
        });
        confirmBtn.onClick.AddListener(() =>
        {
            Debug.Log(selectResult == currentResult ? "作答正确" : "作答错误");
        });
    }


    /// <summary>
    /// 设置答题内容
    /// </summary>
    /// <param name="topic">题目</param>
    /// <param name="choiceContents">选择内容</param>
    /// <param name="result">A:2的0次=1、B:2的1次=2、C:2的2次=4、D:2的3次=8
    /// 例如:答案为BD时result=2+8=10</param>
    public void SetAnswerPanel(string topic, List<string> choiceContents, int result)
    {
        this.gameObject.SetActive(true);
        topicText.text = topic;
        foreach (var choiceContent in choiceContents)
        {
            var choiceContentObj = Instantiate(choiceContentExample, choiceContentExample.transform.parent);
            choiceContentObj.SetActive(true);

            var choiceContentToggle = choiceContentObj.GetComponentInChildren<Toggle>();
            if (!choiceContentToggles.Contains(choiceContentToggle))
                choiceContentToggles.Add(choiceContentToggle);
            choiceContentToggle.isOn = false;
            choiceContentToggle.onValueChanged.AddListener((value) =>
            {
                if (value)
                {
                    if (!isMultipleChoice)
                    {
                        foreach (var _choiceContentToggle in choiceContentToggles)
                        {
                            if (_choiceContentToggle != choiceContentToggle && _choiceContentToggle.isOn)
                                _choiceContentToggle.isOn = false;
                        }
                    }
                    
                    selectResult += (int)Math.Pow(2, choiceContentObj.transform.GetSiblingIndex() - 1);
                }
                else
                    selectResult -= (int)Math.Pow(2, choiceContentObj.transform.GetSiblingIndex() - 1);
            });

            var choiceContentText = choiceContentObj.GetComponentInChildren<Text>();
            choiceContentText.text = choiceContent;
        }
        currentResult = result;
    }
}

 三、使用方式:

将组件拖入正确位置,用如下代码调用:

public class AnswerPanelTest : MonoBehaviour
{
    public AnswerPanel answerPanel;
    private void Awake()
    {
        answerPanel.SetAnswerPanel("这是我的题目答案是BD", new List<string> { "选项A", "选项B", "选项C", "选项D"}, 10);
    }
}

 四、结果展示:

 

(二、固定答题面板)

一、代码展示:

public class AnswerPanel : MonoBehaviour
{
    [Header("答题面板")]
    public GameObject answerPanel;
    [Header("正确答案")]
    public Toggle resultToggle;
    [Header("提交按钮")]
    public Button confirmBtn;

    [Header("播放答题音效源")]
    public AudioSource _audioSource;
    [Header("正确音效")]
    public AudioClip correctAudio;
    [Header("错误音效")]
    public AudioClip errorAudio;

    private void Awake()
    {
        answerPanel.SetActive(false);
        confirmBtn.onClick.AddListener(() =>
        {
            if (resultToggle.isOn)
            {
                Debug.Log("作答正确!");
                _audioSource.clip = correctAudio;
                _audioSource.Play();
            }
            else
            {
                _audioSource.clip = errorAudio;
                _audioSource.Play();
                Debug.Log("作答错误!");
            }
            answerPanel.SetActive(false);
        });
    }
}

 二、使用方法

每一个答题面板都需要挂载该脚本

将正确的组件拖入到unity正确的位置上。

(三)内容动态面板升级

在一的基础上,去掉了多选选项勾选[Header("是否是多选题")]public bool isMultipleChoice;同时增加了下一题按钮和结果框。

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


[Serializable]
public class AnswerContent
{
    public string topic;
    public List<string> choiceContents;
    [Header("答案:第一个正确就是2的0次=1,第二个正确就是2的1次=2,多选一二正确就是1+2=3")]
    public int result;
}


public class AnswerPanel : MonoBehaviour
{
    public Text topicText;
    public Button closePanelBtn;
    public GameObject choiceContentExample;
    public Button confirmBtn;

    public Text resultText;
    public Button nextAnswerBtn;
    
    /// <summary>
    /// 2的0次=1  2的1次=2
    /// </summary>
    public int currentResult;
 
    
    private int selectResult;
    private List<Toggle> choiceContentToggles = new List<Toggle>();
    private void Start()
    {
        this.gameObject.SetActive(false);
        choiceContentExample.SetActive(false);
        closePanelBtn.onClick.AddListener(() =>
        {
            this.gameObject.SetActive(false);
        });
        confirmBtn.onClick.AddListener(() =>
        {
            nextAnswerBtn.gameObject.SetActive(true);
            resultText.text = selectResult == currentResult ? "作答正确" : "作答错误";
        });
        
        nextAnswerBtn.onClick.AddListener(() =>
        {
            currentAnswerIndex += 1;
            SetAnswerPanel();
        });
    }
 
 
    /// <summary>
    /// 设置答题内容
    /// </summary>
    /// <param name="topic">题目</param>
    /// <param name="choiceContents">选择内容</param>
    /// <param name="result">A:2的0次=1、B:2的1次=2、C:2的2次=4、D:2的3次=8
    /// 例如:答案为BD时result=2+8=10</param>
    public void SetAnswerPanel(string topic, List<string> choiceContents, int result)
    {
        topicText.text = string.Empty;
        selectResult = 0;
        currentResult = 0;
        confirmBtn.gameObject.SetActive(true);
        topicText.gameObject.SetActive(true);
        nextAnswerBtn.gameObject.SetActive(false);
        resultText.text = "";
        
        foreach (var choiceContentToggle in choiceContentToggles)
            Destroy(choiceContentToggle.gameObject);
        choiceContentToggles.Clear();
        
        this.gameObject.SetActive(true);
        topicText.text = topic;
        foreach (var choiceContent in choiceContents)
        {
            var choiceContentObj = Instantiate(choiceContentExample, choiceContentExample.transform.parent);
            choiceContentObj.SetActive(true);
 
            var choiceContentToggle = choiceContentObj.GetComponentInChildren<Toggle>();
            if (!choiceContentToggles.Contains(choiceContentToggle))
                choiceContentToggles.Add(choiceContentToggle);
            choiceContentToggle.isOn = false;
            choiceContentToggle.onValueChanged.AddListener((value) =>
            {
                if (value)
                {
                    if ((result & (result - 1)) == 0)
                    {
                        foreach (var _choiceContentToggle in choiceContentToggles)
                        {
                            if (_choiceContentToggle != choiceContentToggle && _choiceContentToggle.isOn)
                                _choiceContentToggle.isOn = false;
                        }
                    }
                    selectResult += (int)Math.Pow(2, choiceContentObj.transform.GetSiblingIndex() - 1);
                }
                else
                    selectResult -= (int)Math.Pow(2, choiceContentObj.transform.GetSiblingIndex() - 1);
            });
 
            var choiceContentText = choiceContentObj.GetComponentInChildren<Text>();
            choiceContentText.text = choiceContent;
        }
        currentResult = result;
    }



    public int currentAnswerIndex = 0;
    public List<AnswerContent> answerContents = new List<AnswerContent>();
    /// <summary>
    /// 设置答题内容
    /// </summary>
    /// <param name="topic">题目</param>
    /// <param name="choiceContents">选择内容</param>
    /// <param name="result">A:2的0次=1、B:2的1次=2、C:2的2次=4、D:2的3次=8
    /// 例如:答案为BD时result=2+8=10</param>
    public void SetAnswerPanel(List<AnswerContent> answerContents)
    {
        this.answerContents = answerContents;
        currentAnswerIndex = 0;
        SetAnswerPanel();
    }

    private void SetAnswerPanel()
    {
        if (currentAnswerIndex < answerContents.Count)
        {
            SetAnswerPanel(answerContents[currentAnswerIndex].topic,
                answerContents[currentAnswerIndex].choiceContents,
                answerContents[currentAnswerIndex].result);
        }
        else
        {
            foreach (var choiceContentToggle in choiceContentToggles)
                Destroy(choiceContentToggle.gameObject);
            choiceContentToggles.Clear();
            topicText.gameObject.SetActive(false);
            confirmBtn.gameObject.SetActive(false);
            nextAnswerBtn.gameObject.SetActive(false);
            resultText.text = "答题结束";
        }
    }
}

 调用方式:

下载链接:

Unity一个答题系统资源-CSDN文库 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

平杨猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值