Warm tip: This article is reproduced from stackoverflow.com, please click
android itext itext7 true-type-fonts unity3d

Why am I unable to locate font files(.ttf) in unity android game in persistentDataPath?

发布于 2020-05-14 12:23:58

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 a screenshot of app persistent data folder.

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.

Questioner
Divyanshu Agarwal
Viewed
36
derHugo 2020-03-02 14:45

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.