(一)内容动态面板
一、答题界面简单搭建
主要为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正确的位置上。