Alright so I am trying to call a XML file but I won’t always know the location of it on the computer (as in it will be on a JUMP Drive) I don’t know yet how to make the XML build Directly into the Resource Folder so if that could be explained as well thanks.
The Reason for this is so that I can edit the content of the Quiz XML outside of unity after I BUILD the project
If you would like to see the code Please Comment
Thank You for your time
UPDATE:
THE PROGRAM IS STANDALONE. THERE IS NO WEB USE FOR THIS PROGRAM IN ANYWAY!
So you want a file browser?
Why not host it on a web server and import it using WWW? Then you always know where it is and can just upload a new XML when you want to change the questions.
EDIT: added bonus of this method is that the questions will change for every build instance, not just for the one who’s XML file you edited.
I found how to do it Thank All
Application.dataPath is what I need
The only way to test this was to build and run because it won’t find the XML otherwise
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
public class TestGUI : MonoBehaviour {
public GUISkin testSkin;
public TextAsset QuestionList;
public Rect textAreaSize;
public Rect buttonSizeA;
public Rect buttonSizeB;
public Rect buttonSizeC;
public Rect buttonSizeD;
public Rect AnswerSizeA;
public Rect AnswerSizeB;
public Rect AnswerSizeC;
public Rect AnswerSizeD;
List<Dictionary<string,string>> levels = new List<Dictionary<string,string>>();
Dictionary<string,string> obj;
float width = 1024;
float height = 768;
static int actualLevel = 1;
static int LevelMaxNumber;
static int WaipointCounter = 0;
static string QuestionString = "";
static string AnswerStringA = "";
static string AnswerStringB = "";
static string AnswerStringC = "";
static string AnswerStringD = "";
string path = @"/gamexmldata.xml";
string locate = Application.dataPath;
void OnGUI() {
GUI.skin = testSkin;
float resolutionX = Screen.width / width;
float resolutionY = Screen.height / height;
GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(resolutionX, resolutionY, 1));
GUI.TextArea (textAreaSize, QuestionString);
if(GUI.Button(buttonSizeA, "A")) // make a button
{
print ("Button A");
WaipointCounter = 4;
}
GUI.Label (AnswerSizeA, AnswerStringA);
if(GUI.Button(buttonSizeB, "B")) // make a button
{
print ("Button B");
}
GUI.Label (AnswerSizeB, AnswerStringB);
if(GUI.Button(buttonSizeC, "C")) // make a button
{
print ("Button C");
}
GUI.Label (AnswerSizeC, AnswerStringC);
if(GUI.Button(buttonSizeD, "D")) // make a button
{
print ("Button D");
}
GUI.Label (AnswerSizeD, AnswerStringD);
}
void Start()
{ //Timeline of the Level creator
locate+=(@"/Resources"+path);
GetLevel();
StartCoroutine(LevelStartInfo(1.0F));
LevelMaxNumber = levels.Count;
}
public void GetLevel()
{
XmlDocument xmlDoc = new XmlDocument(); // xmlDoc is the new xml document.
StreamReader reader = new StreamReader(locate);
xmlDoc.LoadXml(reader.ReadToEnd()); // load the file.
reader.Close();
XmlNodeList levelsList = xmlDoc.GetElementsByTagName("level"); // array of the level nodes.
foreach (XmlNode levelInfo in levelsList)
{
XmlNodeList levelcontent = levelInfo.ChildNodes;
obj = new Dictionary<string,string>(); // Create a object(Dictionary) to colect the both nodes inside the level node and then put into levels[] array.
foreach (XmlNode levelsItens in levelcontent) // levels itens nodes.
{
//if(levelsItens.Name == "name")
//{
// obj.Add("name",levelsItens.InnerText); // put this in the dictionary.
//}
if(levelsItens.Name == "tutorial")
{
obj.Add("tutorial",levelsItens.InnerText); // put this in the dictionary.
}
if(levelsItens.Name == "object")
{
switch(levelsItens.Attributes["name"].Value)
{
case "Right": obj.Add("Right" ,levelsItens.InnerText); break; // put this in the dictionary.
case "Wrong1": obj.Add("Wrong1",levelsItens.InnerText); break; // put this in the dictionary.
case "Wrong2": obj.Add("Wrong2",levelsItens.InnerText); break; // put this in the dictionary.
case "Wrong3": obj.Add("Wrong3",levelsItens.InnerText); break; // put this in the dictionary.
}
}
//if(levelsItens.Name == "finaltext")
//{
// obj.Add("finaltext",levelsItens.InnerText); // put this in the dictionary.
//}
}
levels.Add(obj); // add whole obj dictionary in the levels[].
}
}
IEnumerator LevelStartInfo(float Wait)
{
//string lvlName = "";
//levels[actualLevel-1].TryGetValue("name",out lvlName);
string tutorial = "";
levels[actualLevel-1].TryGetValue("tutorial",out QuestionString);
levels[actualLevel-1].TryGetValue("Right" ,out AnswerStringA);
levels[actualLevel-1].TryGetValue("Wrong1",out AnswerStringB);
levels[actualLevel-1].TryGetValue("Wrong2",out AnswerStringC);
levels[actualLevel-1].TryGetValue("Wrong3",out AnswerStringD);
//levels[actualLevel-1].TryGetValue("finaltext",out finaltext);
yield return new WaitForSeconds(Wait);
}
void Update()
{
if(WaipointCounter == 4)
{
if(actualLevel<LevelMaxNumber)
{
actualLevel+=1;
WaipointCounter = 0;
StartCoroutine(FinishLevel(1.0F));
}
else
{
actualLevel=1;
WaipointCounter = 0;
StartCoroutine(FinishLevel(1.0F));
}
}
}
IEnumerator FinishLevel(float Wait)
{
//yield return new WaitForSeconds(Wait);
//GameObject FinalText_GUI = Instantiate(FinalText) as GameObject;
//FinalText_GUI.guiText.text = finaltext;
yield return new WaitForSeconds(3.0f);
Application.LoadLevel(0);
}
}