从题库加载题目并作答,选择正确答案
直接上代码:
[Serializable]
public class Tmda//题目名称和答案
{
public string name;
public string A;
public string B;
}
public class kztimu : MonoBehaviour
{
private string[] tmh;//存储读取的行数
public Text tmname, tma, tmb;//题目 ,答案a,b
private void Awake()
{
StartCoroutine(LoadFile());
}
public List<Tmda> tmdas=new List<Tmda>();//题目库
void Start()
{
}
void Update()
{
}
Tmda tmda;
IEnumerator LoadFile( )//加载文档
{
string path = Application.streamingAssetsPath + "/" + "timu.txt";
UnityWebRequest request = UnityWebRequest.Get(path);
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
Debug.Log(request.error+"没有获取到");
}
else
{ //把文档除去空格换行
tmh= request.downloadHandler.text.Split(new string[] { "\r\n" }, StringSplitOptions.None);
Jeremy_newtm();
}
}
public void Jeremy_newtm()//创建题目
{
for (int i = 0; i < tmh.Length; i++)
{
if (i% 3 == 0)
{
tmda = new Tmda();
tmda.name = tmh[i];
}
if (i % 3 == 1)
{
tmda.A = tmh[i];
}
if (i % 3 == 2)
{
tmda.B = tmh[i];
tmdas.Add(tmda);
}
}
//Debug.Log(tmdas.Count);
Jeremy_Range();
}
int sj;
public void Jeremy_Range()//随机
{
sj = UnityEngine.Random.Range(0,tmdas.Count);
tmname.text = tmdas[sj].name;
tma.text = tmdas[sj].A;
tmb.text = tmdas[sj].B;
tmdas.Remove(tmdas[sj]);
}
public void Jeremy_cancel() { tmdas.Clear(); Jeremy_newtm(); }//重新加载
}