Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jsyoung43 · Nov 05, 2013 at 04:06 PM · assetsxml

Calling an XML outside of unity

OK I know how to call and display xml file in unity from the asset list, but what do I need to change so that I can call from an xml that I just dump into the build folder so that I can edit it outside of unity without have to rebuild the project every time

Below is the ENTIRE project code it uses only one script at the moment I want to call from an xml outside of unity

UPDATE--I have used the first answer to call the xml from outside the program by declaring the file path. But this is not enough as I need to call the file without knowing the exact location and if I could force the XML to build in the resource folder than part of the problem would be done

Is it possible to use a Application.datapath to call the XML without declaring an exact location?


 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 = "C:/Users/MymIntern2/Desktop/Builds/testbuild_Data/gamexmldata.xml";
     
     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
         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(path);
         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);
     
     }
     
     
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by PProductions · Nov 05, 2013 at 05:37 PM

Try using the StreamReader class from the System.IO namespace. Here is the MSDN link

You would read the xml file from the path then use the text from the stream to load your xml For example

     string path = "C:/myfolder/myproject/data.xml"
     
     StreamReader reader = new StreamReader(path)
     xmlDoc.LoadXml(reader.ReadToEnd())
     reader.Close()


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jsyoung43 · Nov 05, 2013 at 07:24 PM 0
Share

thanks but is there a way to make it find the file without having to type the location of the file

avatar image PProductions · Nov 08, 2013 at 03:25 PM 0
Share

As you mentioned, Application.datapath would be ideal for this. Just add the relative folder and file after this.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Is there a central place that people wish for assets? 1 Answer

Unity 3.x Game Development by Example Beginner's 0 Answers

XML parsing c# in unity 1 Answer

Start a game by reading the data from the xml 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges