Unity VR 应用程序 - 无法导入文件



现在我正在为我的Oculus Go开发一个VR应用程序。在此应用程序中,我需要在运行时导入图像,视频,音频等文件。我现在的问题是这些文件的导入确实可以在 Unity 中工作,但在我的 Oculus Go 上不起作用。

在我的项目中,我使用资源商店中的文件浏览器预制件。 这个文件浏览器可以在我的应用程序运行时打开,但是当我想打开一个文件(例如图像(时,它只会变成灰色。 图像应加载到 RawImage-Object。

运行时的文件浏览器

RawImage-Object 变为灰色

团结工作

我不明白为什么会这样。我不得不说我是团结的新手,希望得到任何帮助!这是图像加载脚本的代码。

public class FileManager : MonoBehaviour {
public RawImage image;
public void OpenExplorer()
{
SimpleFileBrowser.FileBrowser.SetFilters(true, ".txt", ".jpg", ".png", ".mp4");
SimpleFileBrowser.FileBrowser.ShowLoadDialog( (path) => { UpdateImage(path); }, null, false, null, "Select File", "Select");
}
void UpdateImage(string pfad)
{
if (pfad != null)
{
WWW www = new WWW(pfad);
image.texture = www.texture;
var texWidth = www.texture.width;
var texHeight = www.texture.height;
if (texWidth > texHeight)
{
GetComponent<RectTransform>().sizeDelta = new Vector2(1920/2, 1080/2);
}
if (texWidth < texHeight)
{
GetComponent<RectTransform>().sizeDelta = new Vector2(1080/2, 1920/2);
}
}
}
我现在的

问题是这些文件的导入确实可以在 团结,但不适用于我的Oculus Go。

大多数有缺陷的代码都可以在编辑器上运行,但是在部署或生成项目时,您会发现代码存在问题。

您的问题出在UpdateImage功能上:

WWW www = new WWW(pfad);
image.texture = www.texture;
...

在加载图像之前,您应该屈服或等待WWW请求完成。异步请求。如果不产生它,您将尝试访问不完整的图像,从而导致损坏的图像或灰色图像,具体取决于平台。

进行以下更改:

1.将UpdateImage更改为协程函数(void更改为IEnumerator(,然后在从中访问纹理之前生成带有yield return www;WWW请求:

2.使用StartCoroutineUpdateImage函数作为协程函数调用:

取代:

SimpleFileBrowser.FileBrowser.ShowLoadDialog( (path) => { UpdateImage(path); }, null, false, null, "Select File", "Select");

SimpleFileBrowser.FileBrowser.ShowLoadDialog( (path) => { StartCoroutine(UpdateImage(path)); }, null, false, null, "Select File", "Select");

3.在访问纹理之前检查错误。当WWW请求完成后,可能会出现错误。在使用返回的纹理或项目之前检查这一点。

最终代码应如下所示:

public RawImage image;
public void OpenExplorer()
{
SimpleFileBrowser.FileBrowser.SetFilters(true, ".txt", ".jpg", ".png", ".mp4");
SimpleFileBrowser.FileBrowser.ShowLoadDialog((path) => { StartCoroutine(UpdateImage(path)); }, null, false, null, "Select File", "Select");
}
IEnumerator UpdateImage(string pfad)
{
if (pfad != null)
{
WWW www = new WWW(pfad);
//WAIT UNTIL REQUEST IS DONE!
yield return www;
//Check for error
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
}
else
{
image.texture = www.texture;
var texWidth = www.texture.width;
var texHeight = www.texture.height;
if (texWidth > texHeight)
{
GetComponent<RectTransform>().sizeDelta = new Vector2(1920 / 2, 1080 / 2);
}
if (texWidth < texHeight)
{
GetComponent<RectTransform>().sizeDelta = new Vector2(1080 / 2, 1920 / 2);
}
}
}
}

最新更新