- Home /
Problem with path in FileStream
try {
FileStream fsr = new FileStream(Application.dataPath + @"\Resources\converted.uvtddir ", FileMode.Open, FileAccess.Read);
BinaryFormatter formatter = new BinaryFormatter();
Chapters = (IList<Chapter>)formatter.Deserialize(fsr);
fsr.Close();
}catch (Exception e){
Debug.Log(e.Message);
}
Hello, i have problem with path to file in resource. Everythink WORK excellent in unity editor, but when i build this on windows or android, throw error : Could not find a part of the path "C:...". I'm confused, please help.
Answer by Tarlius · Feb 26, 2013 at 11:17 AM
You need to store the file as a text asset (with file extension ".bytes") and load it with Resources.Load().
Anything in a /Resources folder will get pre-built into an Asset Bundle when you deploy (as opposed to a normal folder you can navigate to)
You can access the raw bytes by using textAsset.bytes, and with the byte[] you can make a memory stream that can be used in place of the file stream.
(Edit: Updated to include extra info from conversation with OP)
so do you have some idea how to get access to this file in built application?
Change the file extension to .txt Then use something like:
TextAsset textAsset = (TextAsset) Resources.Load("converted");
byte[] bytes = textAsset.bytes;
0
I forgot mention that is nested lists with bools, strings and others values, not only text.
You can use TextAsset for binary data (as odd as that sounds); .bytes will get you the raw bytes.
I think it will also work if you change the extension to .bytes too, which might be a little less confusing. If I recall correctly, unity won't add it if its not an asset-type it recognises though.
I believe you can then turn the byte[] into a $$anonymous$$emoryStream and use it with your above code in place of FileStream.
Ok,work ;) thx for help, if you want edit your answer i used .bytes extension and code should look like this:
try {
TextAsset ta = Resources.Load("converted") as TextAsset;
Stream s = new $$anonymous$$emoryStream(ta.bytes);
BinaryFormatter formatter = new BinaryFormatter();
Chapters = (IList<Chapter>)formatter.Deserialize(s);
s.Close();
}catch (Exception e){
Debug.Log(e.$$anonymous$$essage);
}
Answer by DaveA · Feb 26, 2013 at 10:30 AM
When built, does that path exist? Is the file there? If not, make that path, and put that file there.
yes, file exist in Assets/Resource. When i run my game in editor, i have access to file and everythink work good, but not in built application