写了一个选择题Demo,电脑包和编辑器内无问题,但是打包安卓手机之后题目无法正常使用,想到的是安卓环境中正确地读取文件的问题
改进方案:
1.由于 XmlDocument.Load
方法在 Android 上的路径问题(由于文件位于 APK 内部,无法像在文件系统中那样直接访问),需要先使用 UnityWebRequest
来异步加载文件内容,然后再解析 XML。
2.异步处理:修改你的代码,以支持异步文件加载和处理,这对于避免在加载大文件时造成的界面冻结也很有帮助。
代码对比:
初版:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Xml;
using System.Text;
public class xmlres : MonoBehaviour
{
[SerializeField]
private List<Toggle> toggleList;//答案ABC
[SerializeField]
private Text TM_Text;//题目
[SerializeField]
private List<Text> DA_TextList;//选项
private int topNumber = 0;
private string selectAnswer = " ";
private bool isSelect = false;//是否选中
/* 确定五道题 */
List<xmlData> xmls = new List<xmlData>();
List<xmlData> useArray = new List<xmlData>();
HashSet<xmlData> xmlDatas = new HashSet<xmlData>();
//public GameObject selectPanel;
//public GameObject wrongPanel;
public static xmlres instance;
private void Awake()
{
instance = this;
}
void Start()
{
ReadQuestion();
toggleList[0].onValueChanged.AddListener(OnValChanged0);
toggleList[1].onValueChanged.AddListener(OnValChanged1);
toggleList[2].onValueChanged.AddListener(OnValChanged2);
}
void OnValChanged0(bool check)
{
//Debug.Log(check);
if (check)
{
selectAnswer = "A";
isSelect = true;
}
}
void OnValChanged1(bool check)
{
//Debug.Log(check);
if (check)
{
selectAnswer = "B";
isSelect = true;
}
}
void OnValChanged2(bool check)
{
//Debug.Log(check);
if (