Unity实现答题系统的示例代码

作者:代码骑士 时间:2022-05-09 18:31:00 

一、作品展示

1、菜单界面

Unity实现答题系统的示例代码

(注:由于特殊原因,原图无法展示,请谅解)

2、答题界面

Unity实现答题系统的示例代码

(注:由于特殊原因,原图无法展示,请谅解)

3、学习模式界面

Unity实现答题系统的示例代码

(注:由于特殊原因,原图无法展示,请谅解)

二、代码展示

1、菜单页面

三个场景跳转按钮

学习党史按钮 

using UnityEngine.SceneManagement;
using UnityEngine;

public class EnterStudy : MonoBehaviour
{
   public void EnterStudy_()
   {
       SceneManager.LoadScene("学习界面");
   }
}

答题测试按钮

using UnityEngine.SceneManagement;
using UnityEngine;

public class EnterTest : MonoBehaviour
{
   public void EnterTest_()
   {
       SceneManager.LoadScene("example");
   }
}

退出系统按钮

using UnityEngine;

public class ExitSystem : MonoBehaviour
{
   public void Quit()
   {
       Application.Quit();
   }
}

2、退出按钮

using UnityEngine;

public class ExitSystem : MonoBehaviour
{
   public void Quit()
   {
       Application.Quit();
   }
}

3、学习界面代码

代码

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

public class TXT_OPRATION : MonoBehaviour
{
   //public TextAsset textTxt;
   //读取文档
   string[][] ArrayX;//题目数据
   string[] lineArray;//读取到题目数据

public Text stuText;
   string[] items = new string[7]{"题号:", "题目:", "A:", "B:", "C:", "D:", "答案:" };

void Start()
   {
       LoadTxt();
       //Debug.Log(textTxt.text);
   }

private void LoadTxt()
   {
       string UnityPath1 = Application.dataPath + "/StreamingAssets/题目5.txt";
       string[] allLineText = File.ReadAllLines(UnityPath1);
       for (int i = 0; i < allLineText.Length; i++)
       {
           Debug.Log(allLineText.Length);
       }
       ArrayX = new string[allLineText.Length][];
       //把csv中的数据储存在二维数组中  
       for (int i = 0; i < allLineText.Length; i++)
       {
           ArrayX[i] = allLineText[i].Split(':');
       }
       //查看保存的题目数据
       int k = 0;
       string texts = "";
       for (int i = 0; i < ArrayX.Length; i++)
       {
           //texts += "题号:";
           for (int j = 0; j < ArrayX[i].Length; j++)
           {
               //Debug.Log(ArrayX[i][j]);
               texts += items[k];
               texts += ArrayX[i][j];
               texts += '\n';
               k++;
               if (k == 7)
               {
                   k = 0;
               }
           }
       }

stuText.text = texts;

}
}

4、答题界面代码

返回开始菜单按钮

using UnityEngine.SceneManagement;
using UnityEngine;

public class returnMenu : MonoBehaviour
{
   public void ReturnMenu()
   {
       SceneManager.LoadScene("登录界面");
   }
}

答题处理主代码

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

public class testAnswer : 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 Text indexText;//当前第几题
   public Text TM_Text;//当前题目
   public List<Text> DA_TextList;//选项
   private int topicIndex = 0;//第几题

//按钮功能及提示信息
   public Button BtnBack;//上一题
   public Button BtnNext;//下一题
   public Button BtnTip;//消息提醒
   public Button BtnJump;//跳转题目
   public InputField jumpInput;//跳转题目
   public Text TextAccuracy;//正确率
   private int anserint = 0;//已经答过几题
   private int isRightNum = 0;//正确题数

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));
   }

/*****************读取txt数据******************/
   void TextCsv()
   {
       string UnityPath1 = Application.dataPath + "/StreamingAssets/题目5.txt";
       string[] allLineText = File.ReadAllLines(UnityPath1);
       for (int i = 0; i < allLineText.Length; i++)
       {
           Debug.Log(allLineText.Length);
       }
       ArrayX = new string[allLineText.Length][];
       //把csv中的数据储存在二维数组中  
       for (int i = 0; i < allLineText.Length; i++)
       {
           ArrayX[i] = allLineText[i].Split(':');
       }
       /*
       //查看保存的题目数据
       for (int i = 0; i < ArrayX.Length; i++)
       {
           for (int j = 0; j < ArrayX[i].Length; j++)
           {
               Debug.Log(ArrayX[i][j]);
           }
       }
       */
       //设置题目状态
       topicMax = allLineText.Length;
       for (int x = 0; x < topicMax; 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 = "第" + (topicIndex + 1) + "题:";//第几题
       TM_Text.text = ArrayX[topicIndex][1];//题目
       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://上一题
               if (topicIndex > 0)
               {
                   topicIndex--;
                   LoadAnswer();
               }
               else
               {
                   tipsText.text = "<color=#27FF02FF>" + "前面已经没有题目了!" + "</color>";
               }
               break;
           case 2://下一题
               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;
               tipsbtn.SetActive(true);
           }
           else
           {
               tipsText.text = "<color=#FF0020FF>" + "对不起,答错了!" + "</color>";
               isRight = false;
               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") + "%";
           }

//禁用掉选项
           for (int i = 0; i < toggleList.Count; i++)
           {
               toggleList[i].interactable = false;
           }
       }
   }
}

三、相应资源

1、txt文件格式

题号:题目:A选项:B选项:C选项:D选项:答案的序号
题号:题目:A选项:B选项:C选项:D选项:答案的序号
题号:题目:A选项:B选项:C选项:D选项:答案的序号
题号:题目:A选项:B选项:C选项:D选项:答案的序号
题号:题目:A选项:B选项:C选项:D选项:答案的序号
……
(注:最后一题的末尾不加回车,所有冒号都是英文格式)

2、如何修改题目内容

这是本系统的一大亮点,也是Unity开发的一个神奇的特性。往往我们做的程序想要修改信息都要连接云服务器、数据库什么的,很少有能在本地直接修改的,并且修改完内置文件之后往往都会使程序崩溃,本系统使用的数据修改方法是将题目写在外部文档,使程序能够动态读取和修改题目信息。这种方式支持的文档格式可以是txt、xml、json,本系统用的是最简便的txt文本。

这是因为我们在Unity中新建一个StreamingAssets文件夹,这个文件夹中的内容可以在应用发布时原封不动地打包进去(不会被加密和压缩),一般用来存放二进制文件。

有了这个文件夹,我们就可以在打包完之后也可以进行文件中的修改(只限制于StreamingAssets文件夹)从而实现增删题目的功能。

唯一的限制就是添加修改题目要满足固定格式,不然读取会出现错误。

(按此路径找到题目文件)

E:\xx学习系统5.0plus\xx学习与答题系统_Data\StreamingAssets

来源:https://blog.csdn.net/qq_51701007/article/details/124640011

标签:Unity,答题,系统
0
投稿

猜你喜欢

  • C#几种截取字符串的方法小结

    2023-07-16 09:55:10
  • Android简单实现圆盘抽奖界面

    2022-07-25 08:35:27
  • Android Path绘制贝塞尔曲线实现QQ拖拽泡泡

    2023-03-16 07:57:24
  • Springboot如何利用拦截器拦截请求信息收集到日志详解

    2023-03-09 02:30:57
  • spring boot starter actuator(健康监控)配置和使用教程

    2021-07-29 06:24:18
  • java 动态 代理的方法总结

    2023-08-25 04:00:33
  • C#用户定义类型转换详解

    2022-06-07 11:44:32
  • java注解的类型知识点总结

    2022-11-04 00:26:02
  • Android实现双层ViewPager嵌套

    2021-12-23 02:35:52
  • Java组件commons fileupload实现文件上传功能

    2022-05-03 15:03:07
  • c# wpf如何使用Blend工具绘制Control样式

    2022-10-26 09:35:25
  • springboot如何通过@PropertySource加载自定义yml文件

    2022-08-06 19:42:56
  • C# Aspose.Words 删除word中的图片操作

    2023-07-29 12:01:43
  • Android中去掉标题栏的几种方法(三种)

    2023-04-01 10:57:37
  • Spring Security中的Servlet过滤器体系代码分析

    2023-03-23 19:05:34
  • Java中String、StringBuffer、StringBuilder的区别介绍

    2023-11-20 18:31:32
  • Java数据结构之数组(动力节点之Java学院整理)

    2023-09-23 21:20:12
  • 结合mybatis-plus实现简单不需要写sql的多表查询

    2021-06-25 12:54:22
  • 使用itextpdf操作pdf的实例讲解

    2022-11-16 00:22:43
  • 安卓(Android)开发之自定义饼状图

    2022-04-06 11:13:11
  • asp之家 软件编程 m.aspxhome.com