- Home /
How to read file on android built from Unity?
This code is work on unity editor, but not android built. I would like to ask for help please !
We cannot access the file, can you pastebin or 0bin it please if it is large? Also it is most likely the location you are reading file from is wrong. You must use Application.persistentDataPath or if you use strea$$anonymous$$g assets then Application.Strea$$anonymous$$gAssetsPath
Thanks for your reply. I have posted my code again. Please help me check ! BEST !
Answer by rh_galaxy · Jun 11, 2020 at 01:32 PM
To use Resources.Load on a custom file you need to place the file in your project path on your PC "/Assets/Resources/test/textfile.txt", and skip the extension in the code like this:
string szFilePath = "test/";
string szFilenameNoExt = "textfile";
TextAsset f = (TextAsset)Resources.Load(szFilePath + szFilenameNoExt);
This will work to read files that are static (exist at compile time).
Thanks so much for your help.
But How to call it to use ? I mean i can not use f.openText right?
https://docs.unity3d.com/ScriptReference/TextAsset.html I google TextAsset this is what I find, literally the first thing Properties it says text: The text contents of the .txt file as a string. (Read Only)
so maybe try f.text
and in the future maybe try use google and common sense
No, because I can not change. The original is FileInfo located the file directory and TextReader will read the line. When you told me to change to Resources.Load, the TextReader is error and I dont know what to change for the fileinfo and textreader.
Don't use TextReader. Continue with this to parse the file text contents as a csv file (comma separated file)
string szFileText = System.Text.Encoding.UTF8.GetString(f.bytes);
string[] szLines = szFileText.Split((char)10);
char[] szSeparator = { (char)',' };
string[] szTokens = szLines[0].Trim('\r', '\n').Split(szSeparator,
StringSplitOptions.RemoveEmptyEntries);
//szTokens[0] is first value in the file, do the parsing with a loop for each line and each
//value. Use Split and Trim however needed to get the values from your text
I have to change everything, and it's error. Could I have your contact please?
Your answer
Follow this Question
Related Questions
How to get Resources filepath on Android device? 2 Answers
Resourse.Load randomly fails on android device 0 Answers
CommandInvokationFailure: Failed to re-package resources. 0 Answers
Android build error. Failed to re-package resources. 3 Answers
Calling Instantiate from a function within a class 2 Answers