为什么我无法在 unity android 游戏中的 persistentDataPath 中找到字体文件 (.ttf)?

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

有人可以指导我正确的方向吗?我在 Unity 2018.4.9 上使用 C# 在 VS 2017 上制作的 android 游戏无法检测持久数据路径中的文件。

这两个字体文件位于持久数据路径目录中。

这是我找到两个字体文件的代码。

    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 是一个显示三个文件结果的文本框: a) 字体文件 1 b) 字体文件 2 c) 图像文件

文件 1 和 2 的调试跟踪始终为假,而图像文件 returns 在存在时为真。

这些字体文件使用 UnityWebRequest 从流路径下载到持久路径,使用以下代码:

    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);

文件已下载并正确写入永久数据路径 storage/emulated/0/Android/data/com.appname.com/files(交叉检查跟踪。)

为什么找不到这两个字体文件并返回 false,而同一路径中的其他文件 returns 为 true?

拜托,任何人,帮我解决它。我得找到这两个字体文件。

总的来说:

始终使用 Path.Combine 而不是通过字符串连接 + "/" 硬编码系统路径! Path.Combine 而是根据 运行 OS

使用正确的路径分隔符
string fontpath1 = Path.Combine(Application.streamingAssetsPath, "LEOPALMHINDI15K710.TTF");
string fontpath2 = Path.Combine(Application.streamingAssetsPath, "LEOPALMHINDI14K240.TTF");

然后请注意,您检查 streamingAssetsPath 中是否存在字体,而对于图像,您检查 persitentDataPath ...

所以回答问题

Why are these two font files not being located and returning false while other files in same path returns true?

你们不是在看同一条路!

您可能还想检查 persitentDataPath 中的字体是否存在。

Afaik 直接 System.IO 不适用于 Android 上的 streamingAssetsPath,因为它已打包。这就是您使用 UnityWebRequest 阅读它们的原因。所以对于 streamingAssetsPath 这些检查可能总是 false.