【问题标题】:In Unity, how can I open a file (i.e. PDF) in an external app?在 Unity 中,如何在外部应用程序中打开文件(即 PDF)?
【发布时间】:2018-07-27 17:30:07
【问题描述】:

我现在正在开发 Unity 应用程序,客户希望能够允许用户单击按钮并在手机(iOS 和 Android)上打开 PDF,但在他们使用的任何应用程序中阅读 PDF,而不是在 Unity 应用程序内部。

我已经尝试过Application.OpenURL("file:///[...]"),它在我的 Windows 桌面上的编辑器中工作(在 Edge 中打开 PDF)...但它不能在 Android 上打开文件(打开文件没有任何反应)。

这就是我正在做的事情:

public string FileName = string.Empty;
public string FileExtension = string.Empty;
private string FilePath = string.Empty;
void Start()
{
    FilePath = string.Format("{0}/{1}.{2}", Application.persistentDataPath, FileName, FileExtension);
}

public void OpenFile()
{
    if (!File.Exists(FilePath))
    {
        var pathRoot = Application.streamingAssetsPath;
        #if UNITY_ANDROID && !UNITY_EDITOR
            pathRoot = Application.dataPath + "!/assets";
        #endif
        StartCoroutine(FetchFile(string.Format("{0}/{1}.{2}", pathRoot, FileName, FileExtension)));
        return;
    }
    Application.OpenURL(FilePath);
}

private IEnumerator FetchFile(string path)
{
    path = "file://" + path;
    #if UNITY_ANDROID && !UNITY_EDITOR
        path = "jar:" +  path;
    #endif
    var fetcher = new WWW(path);
    yield return fetcher;
    File.WriteAllBytes(FilePath, fetcher.bytes);

    Application.OpenURL("file:///" + FilePath);
}

所以我正在检查该文件是否存在于设备的存储中,如果不存在,我将从应用程序中“提取”它并将其写入存储。然后我尝试使用file:/// 标准打开本地“URL”(这个Application.OpenURL("https://www.google.com"); 可以成功地在我的移动浏览器中打开该URL)。

Unity 中是否有办法创建Intent 或触发此事件的方法? FilePath 在 Windows 上工作,与我写入字节的路径相同,所以应该是正确的......还是我错过了什么?

注意: 这在 iOS 中也不起作用,但我想我在 Unity 论坛(现在找不到链接)上读到 Android 是唯一的路径/URL 的例外情况......有人也可以帮我解决这个问题吗?

【问题讨论】:

    标签: c# android ios unity3d


    【解决方案1】:

    在 Nougat (v.7) 中更改了 Android 权限

    您将无法仅通过路径打开,系统已阻止它。

    只需将目标 API 级别降低到 23(Android 6.0 'Marshmallow')。

    您可以按照此参考 URL 使用此代码: How to Open a PDF in an Android-Unity3D App?

    void openPDF(){
      string namePDF = "test";
      TextAsset pdfTem = Resources.Load("PDFs/"+namePDF, typeof(TextAsset)) as TextAsset;
      System.IO.File.WriteAllBytes(Application.persistentDataPath + "/"+namePDF+".pdf", pdfTem.bytes);
      Application.OpenURL(Application.persistentDataPath+"/"+namePDF+".pdf");
        }
    

    【讨论】:

      【解决方案2】:

      如果您尝试使用Application.OpenURL 打开任何文档,那么它将无法正常工作。 Unity 在最新版本中删除了该支持。您可以使用AndroidOpenUrl.OpenUrl(documentUrl, dataType) 来解决此问题。

      public void Example()
       {   
           string dataType = "application/pdf";
           string documentUrl = "/storage/emulated/0/Downloads/template.pdf";
           AndroidOpenUrl.OpenUrl(documentUrl, dataType); // you can specify any MIME type when opening a file by explicitly specifying the dataType parameter
       }
      

      您将从以下存储库中获得该软件包的演示和安装步骤。

      https://github.com/codemaker2015/Unity-Android-Files-Opener

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多