【发布时间】: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 的例外情况......有人也可以帮我解决这个问题吗?
【问题讨论】: