温馨提示:本文翻译自stackoverflow.com,查看原文请点击:unity3d - Why am I unable to locate font files(.ttf) in unity android game in persistentDataPath?
android itext itext7 true-type-fonts unity3d

unity3d - 为什么我无法在persistentDataPath的统一Android游戏中找到字体文件(.ttf)?

发布于 2020-05-18 16:58:02

有人可以指引我正确的方向吗?我在VS 2017上使用C#在united 2018.4.9上制作的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的调试跟踪始终为false,而映像文件存在时返回true。

这些字体文件使用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);

文件已下载并正确写入了持久数据路径,即存储/模拟/0/Android/data/com.appname.com/files(在跟踪中进行了交叉检查)。

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

请任何人帮助我修复它。我要找到这两个字体文件。

查看更多

提问者
Divyanshu Agarwal
被浏览
168
derHugo 2020-03-02 14:45

一般来说:

始终Path.Combine通过字符串连接使用而不是对系统路径进行硬编码+ "/"Path.Combine而是根据运行的操作系统使用正确的路径分隔符

string fontpath1 = Path.Combine(Application.streamingAssetsPath, "LEOPALMHINDI15K710.TTF");
string fontpath2 = Path.Combine(Application.streamingAssetsPath, "LEOPALMHINDI14K240.TTF");

然后请注意,您在streamingAssetsPath一段时间内检查图像中是否存在字体,然后persitentDataPath...

所以要回答这个问题

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

您不在同一条路上!

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

由于Afaik direct 已打包,因此System.IO无法streamingAssetsPath在Android上使用。这就是为什么您UnityWebRequest要阅读它们的原因。因此,对于streamingAssetsPath这些检查可能永远都是false