当前位置:网站首页>Still developing games with unity? Then you're out. Try unity to build an answer system
Still developing games with unity? Then you're out. Try unity to build an answer system
2022-07-24 16:54:00 【InfoQ】
One 、 Preface
Two 、 Renderings and project downloads

3、 ... and 、 Realization
3-1 Interface building

3-2 Read document

using System.Collections.Generic;
using UnityEngine;
public class Answer : MonoBehaviour
{
// Read document
string[][] ArrayX;
string[] lineArray;
private int topicMax = 0;// Maximum number of questions
private List<bool> isAnserList = new List<bool>();// Store the status of whether you have answered the questions
void Start()
{
TextCsv();
}
/***************** Read txt data ******************/
void TextCsv()
{
// Read csv Binary
TextAsset binAsset = Resources.Load("YW", typeof(TextAsset)) as TextAsset;
// Read the contents of each line
lineArray = binAsset.text.Split('\r');
// Create a 2D array
ArrayX = new string[lineArray.Length][];
// hold csv The data in is stored in a two-dimensional array
for (int i = 0; i < lineArray.Length; i++)
{
ArrayX[i] = lineArray[i].Split(':');
}
// View the saved title data
for (int i = 0; i < ArrayX.Length; i++)
{
for (int j = 0; j < ArrayX[i].Length; j++)
{
Debug.Log(ArrayX[i][j]);
}
}
// Set topic status
topicMax = lineArray.Length;
for (int x = 0; x < topicMax + 1; x++)
{
isAnserList.Add(false);
}
}
}

3-3 Load title
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Answer : MonoBehaviour
{
// Read document
string[][] ArrayX;// Subject data
string[] lineArray;// Read the title data
private int topicMax = 0;// Maximum number of questions
private List<bool> isAnserList = new List<bool>();// Store the status of whether you have answered the questions
// Load title
public GameObject tipsbtn;// Prompt button
public Text tipsText;// Prompt information
public List<Toggle> toggleList;// Answer the questions Toggle
public Text indexText;// What is the current question
public Text TM_Text;// Current topic
public List<Text> DA_TextList;// Options
private int topicIndex = 0;// How many questions
void Start()
{
TextCsv();
LoadAnswer();
}
/***************** Read txt data ******************/
void TextCsv()
{
// Read csv Binary
TextAsset binAsset = Resources.Load("YW", typeof(TextAsset)) as TextAsset;
// Read the contents of each line
lineArray = binAsset.text.Split('\r');
// Create a 2D array
ArrayX = new string[lineArray.Length][];
// hold csv The data in is stored in a two-dimensional array
for (int i = 0; i < lineArray.Length; i++)
{
ArrayX[i] = lineArray[i].Split(':');
}
// Set topic status
topicMax = lineArray.Length;
for (int x = 0; x < topicMax + 1; x++)
{
isAnserList.Add(false);
}
}
/***************** Load title ******************/
void LoadAnswer()
{
tipsbtn.SetActive(false);
tipsText.text = "";
for (int x = 0; x < 4; x++)
{
toggleList[x].isOn = false;
}
indexText.text = " The first " + (topicIndex + 1) + " topic :";// How many questions
TM_Text.text = ArrayX[topicIndex][1];// subject
int idx = ArrayX[topicIndex].Length - 3;// There are several options
for (int x = 0; x < idx; x++)
{
DA_TextList[x].text = ArrayX[topicIndex][x + 2];// Options
}
}
}


3-4 Button function
/***************** Button function ******************/
void Select_Answer(int index)
{
switch (index)
{
case 0:// Tips
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>" +" The right answer is :"+ nM + "</color>";
break;
case 1:// Last question
if (topicIndex > 0)
{
topicIndex--;
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + " There are no questions ahead !" + "</color>";
}
break;
case 2:// Next question
if (topicIndex < topicMax-1)
{
topicIndex++;
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + " Oh dear ! It's the last question ." + "</color>";
}
break;
case 3:// Jump
int x = int.Parse(jumpInput.text) - 1;
if (x >= 0 && x < topicMax)
{
topicIndex = x;
jumpInput.text = "";
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + " Out of range !" + "</color>";
}
break;
}
}
3-5 Question right and wrong judgment
/***************** Question right and wrong judgment ******************/
void AnswerRightRrongJudgment(bool check,int index)
{
if (check)
{
// Judge whether the question is right or wrong
bool isRight;
int idx = ArrayX[topicIndex].Length - 1;
int n = int.Parse(ArrayX[topicIndex][idx]) - 1;
if (n == index)
{
tipsText.text = "<color=#27FF02FF>" + " congratulations , bingo !" + "</color>";
isRight = true;
tipsbtn.SetActive(true);
}
else
{
tipsText.text = "<color=#FF0020FF>" + " I'm sorry , Wrong answer !" + "</color>";
isRight = false;
tipsbtn.SetActive(true);
}
// Accuracy calculation
if (isAnserList[topicIndex])
{
tipsText.text = "<color=#FF0020FF>" + " This question has been answered !" + "</color>";
}
else
{
anserint++;
if (isRight)
{
isRightNum++;
}
isAnserList[topicIndex] = true;
TextAccuracy.text = " Accuracy rate :" + ((float)isRightNum / anserint * 100).ToString("f2") + "%";
}
// Disable the option
for (int i = 0; i < toggleList.Count; i++)
{
toggleList[i].interactable = false;
}
}
}

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Answer : MonoBehaviour
{
// Read document
string[][] ArrayX;// Subject data
string[] lineArray;// Read the title data
private int topicMax = 0;// Maximum number of questions
private List<bool> isAnserList = new List<bool>();// Store the status of whether you have answered the questions
// Load title
public GameObject tipsbtn;// Prompt button
public Text tipsText;// Prompt information
public List<Toggle> toggleList;// Answer the questions Toggle
public Text indexText;// What is the current question
public Text TM_Text;// Current topic
public List<Text> DA_TextList;// Options
private int topicIndex = 0;// How many questions
// Button function and prompt information
public Button BtnBack;// Last question
public Button BtnNext;// Next question
public Button BtnTip;// Message alert
public Button BtnJump;// Jump topic
public InputField jumpInput;// Jump topic
public Text TextAccuracy;// Accuracy rate
private int anserint = 0;// I have answered several questions
private int isRightNum = 0;// Correct number of questions
void Awake()
{
TextCsv();
LoadAnswer();
}
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));
}
/***************** Read txt data ******************/
void TextCsv()
{
// Read csv Binary
TextAsset binAsset = Resources.Load("YW", typeof(TextAsset)) as TextAsset;
// Read the contents of each line
lineArray = binAsset.text.Split('\r');
// Create a 2D array
ArrayX = new string[lineArray.Length][];
// hold csv The data in is stored in a two-dimensional array
for (int i = 0; i < lineArray.Length; i++)
{
ArrayX[i] = lineArray[i].Split(':');
}
// Set topic status
topicMax = lineArray.Length;
for (int x = 0; x < topicMax + 1; x++)
{
isAnserList.Add(false);
}
}
/***************** Load title ******************/
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 = " The first " + (topicIndex + 1) + " topic :";// How many questions
TM_Text.text = ArrayX[topicIndex][1];// subject
int idx = ArrayX[topicIndex].Length - 3;// There are several options
for (int x = 0; x < idx; x++)
{
DA_TextList[x].text = ArrayX[topicIndex][x + 2];// Options
}
}
/***************** Button function ******************/
void Select_Answer(int index)
{
switch (index)
{
case 0:// Tips
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>" +" The right answer is :"+ nM + "</color>";
break;
case 1:// Last question
if (topicIndex > 0)
{
topicIndex--;
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + " There are no questions ahead !" + "</color>";
}
break;
case 2:// Next question
if (topicIndex < topicMax-1)
{
topicIndex++;
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + " Oh dear ! It's the last question ." + "</color>";
}
break;
case 3:// Jump
int x = int.Parse(jumpInput.text) - 1;
if (x >= 0 && x < topicMax)
{
topicIndex = x;
jumpInput.text = "";
LoadAnswer();
}
else
{
tipsText.text = "<color=#27FF02FF>" + " Out of range !" + "</color>";
}
break;
}
}
/***************** Question right and wrong judgment ******************/
void AnswerRightRrongJudgment(bool check,int index)
{
if (check)
{
// Judge whether the question is right or wrong
bool isRight;
int idx = ArrayX[topicIndex].Length - 1;
int n = int.Parse(ArrayX[topicIndex][idx]) - 1;
if (n == index)
{
tipsText.text = "<color=#27FF02FF>" + " congratulations , bingo !" + "</color>";
isRight = true;
tipsbtn.SetActive(true);
}
else
{
tipsText.text = "<color=#FF0020FF>" + " I'm sorry , Wrong answer !" + "</color>";
isRight = false;
tipsbtn.SetActive(true);
}
// Accuracy calculation
if (isAnserList[topicIndex])
{
tipsText.text = "<color=#FF0020FF>" + " This question has been answered !" + "</color>";
}
else
{
anserint++;
if (isRight)
{
isRightNum++;
}
isAnserList[topicIndex] = true;
TextAccuracy.text = " Accuracy rate :" + ((float)isRightNum / anserint * 100).ToString("f2") + "%";
}
// Disable the option
for (int i = 0; i < toggleList.Count; i++)
{
toggleList[i].interactable = false;
}
}
}
}
Four 、 an account of happenings after the event being told
边栏推荐
猜你喜欢
随机推荐
OS Experiment 5 process switching based on kernel stack switching
ArcGIS layer annotation display
thinkphp3.2.5无法跳转到外部链接
Envi5.3 open GF-1 WFV data
Jia Yueting's Faraday will receive another financing of US $225million in the future, and ff91 will be mass produced soon!
Still shocked by the explosion in the movie? Then you must not miss this explosive plug-in of unity
工信安全中心牵头搭建数据流通平台 蚂蚁集团等厂商提供技术支持
Analysis of double pointer sliding window method and solution of leetcode related problems
CPU comparison
Codeforces round 690 (Div. 3) C. unique number conventional solution
804. Unique Morse code word
Parental delegation mechanism
1024 happy holidays
Getting started with arcpy
Matlab writes excel string and number merging
Simply use MySQL index
安全的证券公司有哪些?我想在手机上买股票
How to effectively avoid memory leakage when customizing the handler?
[machine learning basics] - another perspective to explain SVM
随笔记:同步、异步和微任务、宏任务的打印顺序









