using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class Answer : MonoBehaviour
{
//读取文档
string[][] ArrayX;//题目数据
string[] lineArray;//读取到题目数据
private int topicMax = 0;//最大题数
private List<bool> isAnserList = new List<bool>();//存放是否答过题的状态
//加载题目
public GameObject tipsbtn;//提示按钮
public Text tipsText;//提示信息
public List<Toggle> toggleList;//答题Toggle
public TMP_Text indexText;//当前第几题
public Text TM_Text;//当前题目
public List<Text> DA_TextList;//选项
private int topicIndex = 0;//第几题
//按钮功能及提示信息
public Transform TF_TopicShowParent;//完成按钮的父物体
public Button BtnBack;//上一题
public Button BtnNext;//下一题
public Button BtnEnd;//完成
public Button BtnTip;//消息提醒
public Button BtnJump;//跳转题目
public Button BtnCompletePrefab;//已答题提示的预设
public InputField jumpInput;//跳转题目
public TMP_Text TextAccuracy;//正确率
private int anserint = 0;//已经答过几题
private int isRightNum = 0;//正确题数
public Canvas canvasStart;
public Canvas canvasGameOver;
public TMP_Text FinishData;//水滴的数量
public GameObject xingxing1;
public GameObject xingxing2;
public GameObject xingxing3;
void Awake()
{
TextCsv();
LoadAnswer();
BtnEnd.gameObject.SetActive(false);
xingxing1.SetActive(false);//消失星星
xingxing2.SetActive(false);//消失星星
xingxing3.SetActive(false);//消失星星
}
public void LoadsGame()
{
SceneManager.LoadScene(0);
}
void Start()
{
toggleList[0].onValueChanged.AddListener((isOn) => AnswerRightRrongJudgment(isOn, 0));
toggleList[1].onValueChanged.AddListener((isOn) => AnswerRightRrongJudgment(isOn, 1));
toggleList[2].onValueChanged.AddListener((isOn) => AnswerRightRrongJudgment(isOn, 2));
toggleList[3].onValueChanged.AddListener((isOn) => AnswerRightRrongJudgment(isOn, 3));
BtnTip.onClick.AddListener(() => Select_Answer(0));
BtnBack.onClick.AddListener(() => Select_Answer(1));
BtnNext.onClick.AddListener(() => Select_Answer(2));
BtnJump.onClick.AddListener(() => Select_Answer(3));
//创建所有题目的Item 并且添加点击事件
for (int i = 0; i < lineArray.Length; i++)
{
Button go = Instantiate(BtnCompletePrefab, TF_TopicShowParent);
int currentIndex = i;
go.name = currentIndex.ToString();
go.transform.GetChild(0).GetComponent<Text>().text=(currentIndex + 1).ToString();
go.onClick.AddListener(()=>
{
topicIndex = currentIndex;
LoadAnswer();
});
}
}
/*****************读取txt数据******************/
void TextCsv()
{
//读取csv二进制文件
TextAsset binAsset = Resources.Load("Data", typeof(TextAsset)) as TextAsset;
//读取每一行的内容
lineArray = binAsset.text.Split('\r');
//创建二维数组 ,长度等于里面的内容
ArrayX = new string[lineArray.Length][];
//把csv中的数据储存在二维数组中
for (int i = 0; i < lineArray.Length; i++)
{
ArrayX[i] = lineArray[i].Split(':');
}
//设置题目状态
topicMax = lineArray.Length;
for (int x = 0; x < topicMax + 1; x++)
{
isAnserList.Add(false);
}
}
/*****************加载题目******************/
void LoadAnswer()
{
for (int i = 0; i < toggleList.Count; i++)
{
toggleList[i].isOn = false;
}
for (int i = 0; i < toggleList.Count; i++)
{
toggleList[i].interactable = true;
}
tipsbtn.SetActive(false);
tipsText.text = "";
indexText.text ="共"+ lineArray .Length+ "题,"+ "当前第" + (topicIndex + 1) + "题:";//第几题
TM_Text.text = "";
TM_Text.text = ArrayX[topicIndex][1].Replace("\n","").Replace("\r","").Replace("\t","");//题目,去掉其他不必要字符
int idx = ArrayX[topicIndex].Length - 3;//有几个选项
for (int x = 0; x < idx; x++)
{
DA_TextList[x].text = ArrayX[topicIndex][x + 2];//选项,题目
}
}
/*****************按钮功能******************/
void Select_Answer(int index)
{
switch (index)
{
case 0://提示
int idx = ArrayX[topicIndex].Length - 1;
int n = int.Parse(ArrayX[topicIndex][idx]);
string nM = "";
switch (n)
{
case 1:
nM = "A";
break;
case 2:
nM = "B";
break;
case 3:
nM = "C";
break;
case 4:
nM = "D";
break;
}
tipsText.text = "<color=#FFAB08FF>" + "正确答案是:" + nM + "</color>";
break;
case 1://上一题
topicIndex--;
topicIndex = topicIndex < 0 ? topicMax - 1 : topicIndex;
LoadAnswer();
return;
//不能循环
if (topicIndex > 0)
{
topicIndex--;
topicIndex = topicIndex < 0 ? lineArray.Length : topicIndex;
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + "前面已经没有题目了!" + "</color>";
}
break;
case 2://下一题
topicIndex++;
topicIndex = topicIndex > topicMax-1 ? 0 : topicIndex;
LoadAnswer();
return;
//不能循环
if (topicIndex < topicMax - 1)
{
topicIndex++;
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + "哎呀!已经是最后一题了。" + "</color>";
}
break;
case 3://跳转
int x = int.Parse(jumpInput.text) - 1;
if (x >= 0 && x < topicMax)
{
topicIndex = x;
jumpInput.text = "";
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + "不在范围内!" + "</color>";
}
break;
}
}
/*****************题目对错判断******************/
void AnswerRightRrongJudgment(bool check, int index)
{
if (check)
{
//判断题目对错
bool isRight;
int idx = ArrayX[topicIndex].Length - 1;
int n = int.Parse(ArrayX[topicIndex][idx]) - 1;
if (n == index)
{
tipsText.text = "<color=#27FF02FF>" + "恭喜你,答对了!" + "</color>";
isRight = true;
TF_TopicShowParent.GetChild(topicIndex).GetChild(0).GetComponent<Text>().color = new Color32(39, 255,2, 255);
tipsbtn.SetActive(true);
}
else
{
tipsText.text = "<color=#FF0020FF>" + "对不起,答错了!" + "</color>";
isRight = false;
TF_TopicShowParent.GetChild(topicIndex).GetChild(0).GetComponent<Text>().color = new Color32(255, 0, 32, 255);
tipsbtn.SetActive(true);
}
//正确率计算
if (isAnserList[topicIndex])
{
tipsText.text = "<color=#FF0020FF>" + "这道题已答过!" + "</color>";
}
else
{
anserint++;
if (isRight)
{
isRightNum++;
}
isAnserList[topicIndex] = true;
TextAccuracy.text = "正确率:" + ((float)isRightNum / anserint * 100).ToString("f2") + "%";
}
Debug.Log(anserint);
if (anserint == 20)/答题题数
{
BtnEnd1();
}
//禁用掉选项
for (int i = 0; i < toggleList.Count; i++)
{
toggleList[i].interactable = false;
}
}
}
public void OnClickEnd()
{
GameOver();
int a = isRightNum;
Debug.Log("答对" + a + "道题");
if (a <= 2)
{
GameOver();//画布显示
FinishData.text = ("很遗憾,你没有获得星星!");
//显示几颗星星
}
if (a >= 3 && a <= 9)
{
GameOver();//画布显示
FinishData.text = ("恭喜你,获得一颗星星!");
xingxing1.SetActive(true); //显示一颗星星
}
if (a >= 10 && a <= 19)
{
GameOver();//画布显示
FinishData.text = ("恭喜你,获得两颗星星!"); //显示两颗星星
xingxing1.SetActive(true);
xingxing2.SetActive(true);
}
if (a == 20)
{
GameOver();//画布显示
FinishData.text = ("恭喜你,获得三颗星星!"); //显示三颗星星
xingxing1.SetActive(true);
xingxing2.SetActive(true);
xingxing3.SetActive(true);
}
}
public void GameOver()
{
//Start画布消失
canvasStart.gameObject.SetActive(false);
//GameOver画布显示
canvasGameOver.gameObject.SetActive(true);
}
public void BtnEnd1()
{
BtnNext.gameObject.SetActive(false);
BtnEnd.gameObject.SetActive(true);
BtnBack.gameObject.SetActive(false);
}
}
步骤
1.建立画布挂代码
2.点击按钮显示正确选项
3.选项+提示语(是否做对)
4.题号+题目
5.导入文件+正确率
6.结束画布
文件
1:以下那位是《本草纲目》的作者?:A、张仲景:B、李时珍:C、扁鹊 :D、华佗:2
2:以下那项是枸杞的功效?:A、清热,行气,止痛,消肿。治感冒,痄腮,喉痛,咳嗽,腹胀,疝气,天疱疮。:B、解肌退热,透疹,生津止渴,升阳止泻,通经活络,解酒毒。:C、温中散寒,理气止痛。:D、滋肾,润肺,补肝,明目。:4
3:哪个脏器主疏泄,与情志活动关系密切?:A、心:B、肝:C、脾:D、肺:2
4:中药“四气”指的是什么?:A、寒热温凉:B、酸甜苦辣 :C、红黄蓝绿 :D、春夏秋冬:1
5:中医的“针灸”疗法,主要通过什么方式来调节人体机能?:A、刺激穴位 :B、服用药物 :C、手术切割 :D、按摩推拿:1
6:中医理论中的“阴阳”是指什么?:A、 两种具体的物质 :B、人体内的两种气体 :C、 对立统一的两个方面 :D、两种不同的能量状态:3
7:下列哪项是中医诊断的基本方法之一?:A、抽血化验 :B、 核磁共振 :C、望闻问切 :D、 CT扫描:3
8:中医四诊病因学的基本理论是什么?:A、九种病因学说 :B、 并病论 :C、五行学说 :D、 三焦学说:1
9:中医理论中,“心主神明”的含义是什么?:A、心主宰人体的生理活动 :B、心主宰人体的心理活动 :C、心主宰人体的生理、心理活动 :D、心主宰人体的精神意识活动:3
10:《黄帝内经》是哪部医学著作的简称?:A、一部古代医学典籍 :B、 中医四大经典之一 :C、秦汉时期的医学总结 :D、以上 都是:4
11:中医的“八纲辨证”包括哪些方面?:A、阴阳、表里、寒热、虚实 :B、 气血、津液、脏腑、经络 :C、病因、病位、病性、病势 :D、望闻问切四诊合参:1
12:哪部书籍论述了外感病与内科杂病的诊治原则?:A、《本草纲目》:B、 《伤寒杂病论》 :C、《千金方》 :D、 《黄帝内经》:2
13:《黄帝内经》分为?:A、《素问》与《灵枢》 :B、《伤寒》与《金匮》 :C、《本草》与《千金》 :D、《本草》与《千金》:1
14:下列哪部书是由明代李时珍所著?:A、《伤寒杂病论》:B、《本草纲目》 :C、《黄帝内经》 :D、《温病条辨》:2
15:“温病学”的代表性著作,由清代吴鞠通所著的是?:A、《温热论》 :B、《温病条辨》 :C、《疫疹一得》 :D、《广瘟疫论》:2
16:哪部书籍详细阐述了阴阳五行、脏腑经络等基础理论?:A、《难经》 :B、 《黄帝内经》 :C、《伤寒杂病论》 :D、《神农本草经》:2
17:被誉为“方书之祖”,由东汉张仲景所著的书籍是?:A、《伤寒杂病论》 :B、 《千金方》 :C、《本草纲目》 :D、《外台秘要》:1
18:哪部书是最早的中医药学典籍之一,载有药物365种?:A、《神农本草经》 :B、 《本草纲目》 :C、《新修本草》 :D、《本草拾遗》:1
19:唐代孙思邈所著,总结唐代以前医学成就的综合性医书是?:A、《千金方》 :B、 《外台秘要》 :C、《伤寒杂病论》 :D、《黄帝内经》:1
20:清代叶天士所著,对温病学说有重大贡献的书籍是?:A、《温热论》 :B、 《温疫论》 :C、《温病条辨》 :D、《广温热论》:1