【问题标题】:Unity VR-Application - Can't import filesUnity VR-Application - 无法导入文件
【发布时间】:2018-09-13 14:39:23
【问题描述】:

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

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

Filebrowser during runtime

RawImage-Object turns grey

Unity-does work

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

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

}

【问题讨论】:

  • 你使用的路径是什么?
  • 路径是我通过文件浏览器选择的文件路径。
  • 能贴出路径的调试输出吗?如果它在资源文件夹中,您可能需要使用 Resources.Load ("Images/SampleImage") as Texture2D; 而不是 www
  • 输出为:D:\Dokumente\IMG_20180912_0002.jpg 它确实显示了正确的路径
  • 而且图片不在资源文件夹中。我想将之前下载的图片与我的 oculus 一起使用。

标签: c# unity3d virtual-reality


【解决方案1】:

我现在的问题是这些文件的导入确实有效 统一,但在我的 oculus 上不起作用。

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

您的问题出在UpdateImage 函数中:

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

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

进行以下更改:

1. 将 UpdateImage 更改为协程函数(voidIEnumerator),然后在访问纹理之前使用 yield return www; 生成 WWW 请求: p>

2.将UpdateImage函数作为协程函数调用StartCoroutine

替换:

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

【讨论】:

  • 我认为只调用 image.texture.width 和同样的高度比调用 www.texture 更好,这每次都会生成一个新的纹理 2d docs.unity3d.com/ScriptReference/WWW-texture.html
  • @palebone 是的,没错。 OP 应该在image.texture = www.texture 之后使用image.texture.width,尽管这与问题无关。
猜你喜欢
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 2020-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多