- Home /
Why am I unable to locate font files(.ttf) in unity android game in persistentDataPath?
Can somebody guide me in the right direction? My android game that I made on unity 2018.4.9 using C# on VS 2017 is unable to detect files at persistent data path.
The two font files are located in the persistent data path directory
This is my code which locates the two font files.
string fontpath1 = Application.streamingAssetsPath + "/LEOPALMHINDI15K710.TTF";
string fontpath2 = Application.streamingAssetsPath + "/LEOPALMHINDI14K240.TTF";
//Check Font 1 existence
if (File.Exists(fontpath1))
{
Debugtext.text += "true1\r\n";
}
else if (!File.Exists(fontpath1))
{
Debugtext.text += "false1\r\n";
}
//Check Font 2 existence
if (File.Exists(fontpath2))
{
Debugtext.text += "true2\r\n";
}
else if (!File.Exists(fontpath2))
{
Debugtext.text += "false2\r\n";
}
//Third File(Image file existence)
if (File.Exists(Application.persistentDataPath + "/FullShot.jpg"))
{
Debugtext.text += "trueIM\r\n";
}
else if (!File.Exists(Application.persistentDataPath + "/FullShot.jpg"))
{
Debugtext.text += "falseIM\r\n";
}
Debugtext.text += Application.persistentDataPath;
Debugtext.text is a textbox which shows the result of three files: a) Font File 1 b) Font File 2 c) An Image File
The debug traces for files 1 and 2 are always false, whereas the image file returns true when it's present.
These fonts files are downloaded using UnityWebRequest from streaming path to persistent path using the following code:
string fontpath1 = Application.streamingAssetsPath + "/LEOPALMHINDI15K710.TTF";
string fontpath2 = Application.streamingAssetsPath + "/LEOPALMHINDI14K240.TTF";
//Request Font1
UnityWebRequest request = UnityWebRequest.Get(fontpath1);
request.SendWebRequest();
while (!request.isDone)
{
}
System.IO.File.WriteAllBytes(Application.persistentDataPath + "/LEOPALMHINDI15K710.TTF", request.downloadHandler.data);
//Request Font2
UnityWebRequest font2 = UnityWebRequest.Get(fontpath2);
font2.SendWebRequest();
while (!font2.isDone)
{
}
System.IO.File.WriteAllBytes(Application.persistentDataPath + "/LEOPALMHINDI14K240.TTF", font2.downloadHandler.data);
The files are downloaded and written properly to the Persistent data path which is storage/emulated/0/Android/data/com.appname.com/files(cross-checked in traces.)
Why are these two font files not being located and returning false while other files in same path returns true?
Please, anyone, help me fix it. I gotta locate these two font files.
Answer by Kaziata · Mar 02, 2020 at 04:53 AM
It looks like you are using two different methods to find these files. Have you tried:
Application.persistentDataPath +"/LEOPALMHINDI15K710.TTF";
Instead of: Application.streamingAssetsPath + "/LEOPALMHINDI15K710.TTF";
Right. The code checking the existance of the files still uses the Strea$$anonymous$$gAssetsPath which of course won't work.
ps: Like always I'd like to mention that I would highly recommend to use System.IO Path.Combine(Application.strea$$anonymous$$gAssetsPath, "LEOPAL$$anonymous$$HINDI15$$anonymous$$710.TTF")
to combine several path fragments. Note that I removed the slash from the string constant. The Combine method ensures that the right slash is used depending on the platform. Also it handles potential trailing slashes in the strea$$anonymous$$gAssetsPath / persistentDataPath correctly.
Your answer
Follow this Question
Related Questions
Getting Localization data in Android with streamingAssets and UnityWebRequest 0 Answers
Where to store files in Android that can be accessed later via path? 1 Answer
Loading an image from streamingassets fails every time on Android 1 Answer
Get Contents of Folder Using UnityWebRequest 0 Answers
Fatal Exception when loading multiple image files on Android from persistent data path 0 Answers