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.
In general:
Allways use Path.Combine
instead of hardcoding system paths via string concatenation + "/"
! Path.Combine
rather uses the correct path separators according to the running OS
string fontpath1 = Path.Combine(Application.streamingAssetsPath, "LEOPALMHINDI15K710.TTF");
string fontpath2 = Path.Combine(Application.streamingAssetsPath, "LEOPALMHINDI14K240.TTF");
Then note that you check for existence of the fonts in the streamingAssetsPath
while for the image you check in the persitentDataPath
...
So to answer the question
Why are these two font files not being located and returning false while other files in same path returns true?
You are not looking in the same path!
You probably wanted to check the existence for the fonts also in the persitentDataPath
.
Afaik direct System.IO
don't work for the streamingAssetsPath
on Android since it is packed. That's why you are using UnityWebRequest
to read them. So for streamingAssetsPath
these checks will probably always be false
.
Update: Download is done and webrequest is disposed. It is now returning true for both files existence. But pdf is not registering them still.
I will update the pdf registering codes as soon as I reach office.
Your question wasn't about anything regarding PDF. It was why
false
is returned when you check for the existence of the two font files ...The whole point reading fonts in persistent data path is to register it using PdfFontFactory for localization of the pdf that my game generates. Instead of reading directly from streaming assets path app is downloading fonts fro streamingAssets to PersistentDatPath using UnityWebRequest. It return true for file's existence now but Pdf should register them as well which is not the case. I thought pdf is not registering the fonts because existence is not returning true, but it returns true for both files now. Do you want me to create another question to ask that?
@DivyanshuAgarwal yes that would be the way to go ;)