- Home /
Store the path with Resources.Load in a string
Hi I need to store the path in a string using resources.Load.
for example ;
string path = Resources.Load("Objects/Levels");
Could you be more specific about what you are going to do with the string? Is this for a script running in the editor or in game? I don't believe what you are trying to do is possible but with more information maybe someone could help you find an alternate solution.
I'm going to parse an X$$anonymous$$L file, and this string will be as the path to that Xml File.
TextReader reader = new StreamReader (path);
Answer by uanmanarmy · Jul 22, 2014 at 09:07 AM
I will put the solution that I made.
Put your XML file in the resources folder in your project (that is, Assets/Resources/).
Make a variable of type class that you made.
LevelsDirectory XmlData;
In your code, use this to load the file as a TextAsset:
TextAsset textAsset= Resources.Load ("xml_path") as TextAsset;
From here, you can get it into XMLDocument, if that is what you want.
XmlSerializer deserializer = new XmlSerializer (typeof(LevelsDirectory));
TextReader reader = new StringReader (textAsset.text);
XmlData = (LevelsDirectory)deserializer.Deserialize (reader);
reader.Close ();
LevelsDirectory is a public class with information from your Xml
public class LevelsDirectory
{
[XmlElement("Level")]
public List<Level> levelList = new List<Level> ();
}
public class Level
{
public int LevelNumber{ get; set; }
public int ItemsNumber{ get; set; }
public string ItemsColor { get; set; }
}
Answer by WillTM · Jul 21, 2014 at 03:01 PM
Is this what you want?
string path = "Objects/Levels";
Resources.Load(path + "assetname.txt") as TextAsset;
using a text asset as an example.
not really but good one.
Why in a string, cuz I need this string for an build in Function that accept only strings ;D
Answer by calebheale · Jul 22, 2014 at 08:39 AM
I don't think you want Resources for this. You probably want StreamingAssets. Place the xml file in a folder called StreamingAssets (similar to what you do with resources) and then you can access the file using Application.streamingAssetsPath like:
StreamReader sr = new StreamReader(Application.streamingAssetsPath + "/myXml.xml");
You will want to take a look at the documentation to see the additional notes on android and web platforms if those are intended targets.
Your answer
Follow this Question
Related Questions
Loading a ScriptableObject at Runtime. 1 Answer
Access path of stored asset 0 Answers
A node in a childnode? 1 Answer
Writing on text file in an Android Build 1 Answer
Loading resources into a ScriptableObject instead of Resources.Load? 1 Answer