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");

Is this what you want?

string path = “Objects/Levels”;

Resources.Load(path + “assetname.txt”) as TextAsset;

using a text asset as an example.

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.

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; }
	}