Unity WebGL 外部资源 [英] Unity WebGL External Assets

查看:51
本文介绍了Unity WebGL 外部资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Unity 中开发一些 webGL 项目,该项目必须从目录加载一些外部图像,它在编辑器中运行良好,但是当我构建它时,它在 Web 控制台中引发目录未找到异常.我将图像放在 Assets/StreamingAssets 文件夹中,该文件夹将成为构建项目中的 StreamingAssets 文件夹(在根目录下,与 index.html 相同).图像位于那里,但浏览器仍然抱怨无法找到该目录.(我是在自己的电脑上打开的,没有运行网络服务器)

I'm developing some webGL project in Unity that has to load some external images from a directory, it runs all fine in the editor, however when I build it, it throws a Directory Not Found exception in web console. I am putting the images in Assets/StreamingAssets folder, that will become StreamingAssets folder in the built project (at root, same as index.html). Images are located there, yet browser still complains about not being able to find that directory. (I'm opening it on my own computer, no running web server)

我想我遗漏了一些非常明显的东西,但似乎我可以使用一些帮助,我一周前才开始学习 unity,而且我对 C# 或 JavaScript 不是那么好(我正在尝试变得更好......)这是否与某些javascript安全问题有关?

I guess I'm missing something very obvious, but it seems like I could use some help, I've just started learning unity a week ago, and I'm not that great with C# or JavaScript (I'm trying to get better...) Is this somehow related to some javascript security issues?

有人可以指出我正确的方向,我应该如何在 Unity WebGL 中读取图像(无需编写)?

Could someone please point me in the right direction, how I should be reading images(no writing need to be done) in Unity WebGL?

string appPath = Application.dataPath;
string[] filePaths = Directory.GetFiles(appPath, "*.jpg");

根据 webGL 中的 unity3d.com 构建除线程和反射之外的所有内容,因此 IO 应该可以正常工作 - 或者我认为:S

According to unity3d.com in webGL builds everything except threading and reflection is supported, so IO should be working - or so I thought:S

我做了一些工作,现在我正在尝试加载一个包含图像路径的文本文件(以;"分隔):

I was working around a bit and now I'm trying to load a text file containing the paths of the images (separated by ';'):

    TextAsset ta = Resources.Load<TextAsset>("texManifest");
    string[] lines = ta.text.Split(';');

然后我将所有行转换为正确的路径,并将它们添加到列表中:

Then I convert all lines to proper path, and add them to a list:

    string temp = Application.streamingAssetsPath + "/textures/" + s;
    filePaths.Add(temp);

Debug.Log 告诉我它看起来像这样:

Debug.Log tells me it looks like this:

file://////Downloads/FurnitureDresser/build/StreamingAssets/textures/79.jpg

file://////Downloads/FurnitureDresser/build/StreamingAssets/textures/79.jpg

所以除了所有那些斜杠之外,这似乎没问题(这对我来说看起来有点奇怪)

So that seems to be allright except for all those slashes (That looks a bit odd to me)

最后创建纹理:

    WWW www = new WWW("file://" + filePaths[i]);
    yield return www;
    Texture2D new_texture = new Texture2D(120, 80);
    www.LoadImageIntoTexture(new_texture);

围绕这最后一部分(不确定:webgl 项目似乎不容易调试)它告诉我:NS_ERROR_DOM_BAD_URI:访问受限 URI 被拒绝

And around this last part (unsure: webgl projects does not seem easily debuggable) it tells me: NS_ERROR_DOM_BAD_URI: Access to restricted URI denied

有人能告诉我发生了什么吗?最重要的是,创建一个目录的解决方案是什么,我可以在运行时从中加载图像?

Can someone please enlighten me what is happening? And most of all, what would be proper to solution to create a directory from where I can load images during runtime?

推荐答案

将您的图像放在 Resources 文件夹中并使用 Resources.Load 打开文件并使用它.

Put your image in the Resources folder and use Resources.Load to open the file and use it.

例如:

Texture2D texture = Resources.Load("images/Texture") as Texture2D;
if (texture != null) 
{
    GetComponent<Renderer>().material.mainTexture = texture;
}

目录列表和文件 API 在 webgl 构建中不可用.

The directory listing and file APIs are not available in webgl builds.

基本上不支持低级 IO 操作.

Basically no low level IO operations are supported.

这篇关于Unity WebGL 外部资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆