下面是全部的腳本代碼,可能比較長,主要實作的就是記錄用戶,題號以及其所選的ABCD選項的情況,其中update中包含了選擇選項和確認選項的內容(與寫入xml相關的只有標紅的內容。)報錯的地方是在黃色的字的位置。
using UnityEngine;
using System.Collections;
using System.Xml;
using System.IO;
using System;
public class queswitch : MonoBehaviour {
public GameObject[] quechoice;
private int no1=1, no2=1, quesno=0,stuno=0;
private bool input1, input2;
public AudioClip sound;
private AudioSource Sound;
// Use this for initialization
void Start () {
Sound = GameObject.Find ("wall003").GetComponent<AudioSource> ();
CreateXML();
}
// Update is called once per frame
void Update () {
//quechoice[]是0~5的的6個gameobject,quechoice[1]=A.quechoice[2]=B.quechoice[3]=C.quechoice[4]=D
if (Input.GetKeyDown(KeyCode.X)) {//當按下“X”鍵時向下選擇
no1 ++;
input1 = true;
if (input1) {
if (no1 > 5) {
no1 = 1;
}
//no2 = 6 - no1;
input1 = false;
}
//把當前選項的顏色變成紅色,上一個的顏色還原。no1為選項
quechoice [no1 - 1].GetComponent<MeshRenderer> ().material.color=new Color32(44,109,118,0);
quechoice [no1].GetComponent<MeshRenderer> ().material.color = Color.red;
quechoice [(no1+1)%5].GetComponent<MeshRenderer> ().material.color=new Color32(44,109,118,0);
quechoice [(no1+2)%5].GetComponent<MeshRenderer> ().material.color=new Color32(44,109,118,0);
quechoice [(no1+3)%5].GetComponent<MeshRenderer> ().material.color=new Color32(44,109,118,0);
quechoice [(no1+4)%5].GetComponent<MeshRenderer> ().material.color=new Color32(44,109,118,0);
}
if (Input.GetKeyDown(KeyCode.Z)) {//當按下“Z”鍵時向上選擇
no1 --;
input2 = true;
if (input2) {
if (no1 <1) {
no1 = 5;
}
//no1 = 6 - no2;
input2 = false;
}
quechoice [(no1+1)%5].GetComponent<MeshRenderer> ().material.color=new Color32(44,109,118,0);
quechoice [(no1+2)%5].GetComponent<MeshRenderer> ().material.color=new Color32(44,109,118,0);
quechoice [(no1+3)%5].GetComponent<MeshRenderer> ().material.color=new Color32(44,109,118,0);
quechoice [(no1+4)%5].GetComponent<MeshRenderer> ().material.color=new Color32(44,109,118,0);
quechoice [no1].GetComponent<MeshRenderer> ().material.color = Color.red;
}
if (Input.GetKeyDown (KeyCode.Q)) {
Sound.PlayOneShot(sound,1);
quesno++;
switch(quesno)
{
case 1:
GameObject.Find ("questext").GetComponent<TextMesh>().text="第一題";
addxmldata(stuno ,quesno ,no1);
break;
case 2:
GameObject.Find ("questext").GetComponent<TextMesh>().text="第二題";
addxmldata(stuno ,quesno ,no1);
break;
case 3:
GameObject.Find ("questext").GetComponent<TextMesh>().text="第三題";
addxmldata(stuno ,quesno ,no1);
break;
case 4:
GameObject.Find ("questext").GetComponent<TextMesh>().text="第四題";
addxmldata(stuno ,quesno ,no1);
break;
case 5:
GameObject.Find ("questext").GetComponent<TextMesh>().text="第五題";
addxmldata(stuno ,quesno ,no1);
break;
case 6:
GameObject.Find ("questext").GetComponent<TextMesh>().text="第六題";
addxmldata(stuno ,quesno ,no1);
break;
case 7:
GameObject.Find ("questext").GetComponent<TextMesh>().text="第七題";
addxmldata(stuno ,quesno ,no1);
break;
case 8:
GameObject.Find ("questext").GetComponent<TextMesh>().text="第八題";
addxmldata(stuno ,quesno ,no1);
break;
case 9:
GameObject.Find ("questext").GetComponent<TextMesh>().text="第九題";
addxmldata(stuno ,quesno ,no1);
break;
case 10:
GameObject.Find ("questext").GetComponent<TextMesh>().text="第十題";
addxmldata(stuno ,quesno ,no1);
break;
default:
GameObject.Find ("questext").GetComponent<TextMesh>().text="實驗結束感謝您的配合";
stuno++;//需要修改只能記錄一個人的,之后的人物資訊將被覆寫
break;
}
}
}
void CreateXML()
{
string path = Application.dataPath + "/results.xml";
if(!File.Exists(path))
{
//創建最上一層的節點。
XmlDocument xml = new XmlDocument();
//創建最上一層的節點。
XmlElement root = xml.CreateElement("objects");
//最后保存檔案
xml.Save(path);
}
}
void addxmldata(int stuno,int quesno,int queschoice){//傳入回答問題的學生編號,所回答問題的編號,所選選項
string path = Application.dataPath + "/results.xml";
string a=System.Convert.ToString(stuno);
string b=System.Convert.ToString(quesno);
string c=System.Convert.ToString(queschoice);
if(File.Exists(path))
{
XmlDocument xml = new XmlDocument();
xml.Load(path);
if(b=="1"){//新的一個同學
XmlNode root = xml.SelectSingleNode("objects");
XmlElement element=xml.CreateElement("answer");//學生編號
element.SetAttribute("id",a);
XmlElement elementChild1=xml.CreateElement("tihao");//題號
elementChild1.SetAttribute("tiid",b);
elementChild1.InnerText=c;
element.AppendChild(elementChild1);
root.AppendChild(element);
xml.AppendChild(root);
//最后保存檔案
xml.Save(path);
}
else{
XmlNodeList xmlNodeList = xml.SelectSingleNode("objects").ChildNodes;
foreach(XmlElement xl1 in xmlNodeList)
{
if (xl1.GetAttribute("id") ==a)
{
XmlElement elementChild1=xml.CreateElement("tihao");//題號
elementChild1.SetAttribute("tiid",b);
elementChild1.InnerText=c;
xl1.AppendChild(elementChild1);
}
}
xml.Save(path);
}
}
}
}
uj5u.com熱心網友回復:
忘了貼出報錯資訊了~XmlException: Document element did not appear. file:///D:/Unity Projects/psychology_program/Assets/results.xml Line 1, position 1.
Mono.Xml2.XmlTextReader.Read ()
uj5u.com熱心網友回復:
xml檔案錯誤轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/61358.html
標籤:Unity3D
下一篇:求助,關于小球平拋運動
