- Home /
How do I call a File outside Unity without the Location?
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!
Is it possible to have the code find the location and input the location as a string
Answer by Jsyoung43 · Nov 06, 2013 at 08:20 PM
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);
}
}
Answer by tanoshimi · Nov 06, 2013 at 03:48 PM
So you want a file browser?
no I am calling the questions from the xml and want the xml to build in the resource folder so I can edit it without being in Unity to do that I need the exact location of the file to call it in the code but the executable and its data folder are on a Jump Drive so the location is never the same
Answer by drod7425 · Nov 06, 2013 at 04:12 PM
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 don't understand. Are you saying that you don't want them to be able to view the X$$anonymous$$L file? Wouldn't they be able to see the X$$anonymous$$L file even easier if it's in their build's folder? You wouldn't need to expose the url your X$$anonymous$$L file is located at if you used WWW.
Not to be rude but do you not know what STANDALONE NO INTERNET means. The xml is just meant to allow the client to edit the question list themselves so we will not have to recode it everytime. the program will be on a isolated Intranet with shortcuts to the program on every ter$$anonymous$$al.
Despite what I'm sure were your best efforts, that was rude. Your original question made no mention of the fact that you want the client to be able to edit the X$$anonymous$$L. I was going off the information you gave: that you want to be able to edit the X$$anonymous$$L after you built it and you don't want the users to cheat. I'm not a $$anonymous$$dreader.
Good luck with your project.
Yes I did I even said I want the file in the resource folder and that the whole that will be on a Jump Drive and no where does it say that I will be using a WebPlayer. Granted I did not say standalone from the start.
But I am sorry for how rude the comment sounds I type bluntly so it will most like have sounded rude either way.
Still I thank you for your time and help in this matter.
Your answer
Follow this Question
Related Questions
Loading parts of file (Multiplatform) 0 Answers
Read external files from resources 2 Answers
How do I read from a txt file during run time? 0 Answers