首页 文章

Unity VR-Application - 无法导入文件

提问于
浏览
0

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

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

Filebrowser during runtime

RawImage-Object turns grey

Unity-does work

我不明白为什么会这样 . 我不得不说我是团结的新手,并会感激任何帮助!这是image-load-script的代码 .

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

}

1 回答

  • 0

    我现在的问题是这些文件的导入确实可以统一使用,但在我的眼睛上不起作用 .

    大多数错误代码都在编辑器上运行,但在部署或构建项目时,您将发现代码问题 .

    您的问题出在 UpdateImage 函数中:

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

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

    进行以下更改:

    1 . 将 UpdateImage 更改为协同程序函数( voidIEnumerator ),然后在从中访问纹理之前,使用 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);
                }
            }
        }
    }
    

相关问题