Unity WebGL 外部资源

Unity WebGL External Assets

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

我想我遗漏了一些非常明显的东西,但似乎我需要一些帮助,我一周前才开始学习 unity,我对 C# 或 JavaScript(我正在努力变得更好...)这是否与某些 javascript 安全问题有关?

有人能给我指出正确的方向吗,我应该如何在 Unity WebGL 中读取图像(无需进行写入)?

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

根据 unity3d.com 在 webGL 中构建除支持线程和反射之外的所有内容,因此 IO 应该工作 - 或者我 thought:S

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

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

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

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

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

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

所以除了那些斜杠(我觉得有点奇怪)之外,这似乎没问题

最后创建纹理:

    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 被拒绝

谁能告诉我这是怎么回事?最重要的是,创建一个我可以在运行时从中加载图像的目录的解决方案是什么?

将你的图片放在Resources文件夹中,然后使用Resources.Load打开文件并使用它。

例如:

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

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

基本不支持底层IO操作

我意识到这个问题已经有几年了,但是,由于这似乎仍然是一个常见的问题,这里有一个解决方案(抱歉,代码是 C#,但我猜 javascript实现方式类似)。基本上,您需要使用 UnityWebRequest 和协程来访问 StreamingAssets 文件夹中的文件。

1) 创建一个新的加载场景(除了查询文件外什么都不做;你可以让它显示一些状态文本或进度条让用户知道发生了什么)。

2) 在Loading场景的Main Camera中添加一个名为Loader的脚本

3)在Loader脚本中,添加一个变量,表示是否读取成功:

private bool isAssetRead;

4) 在Loading脚本的Start()方法中:

void Start ()
{
  // if webGL, this will be something like "http://..."
  string assetPath = Application.streamingAssetsPath;

  bool isWebGl = assetPath.Contains("://") || 
                   assetPath.Contains(":///");

  try
  {
    if (isWebGl)
    {
      StartCoroutine(
        SendRequest(
          Path.Combine(
            assetPath, "myAsset")));
    }
    else // desktop app
    {
      // do whatever you need is app is not WebGL
    }
  }
  catch
  {
    // handle failure
  }
}

5) 在Loading脚本的Update()方法中:

void Update ()
{
  // check to see if asset has been successfully read yet
  if (isAssetRead)
  {
    // once asset is successfully read, 
    // load the next screen (e.g. main menu or gameplay)
    SceneManager.LoadScene("NextScene");
  }

  // need to consider what happens if 
  // asset fails to be read for some reason
}

6) 在Loading脚本的SendRequest()方法中:

private IEnumerator SendRequest(string url)
{
  using (UnityWebRequest request = UnityWebRequest.Get(url))
  {
    yield return request.SendWebRequest();

    if (request.isNetworkError || request.isHttpError)
    {
      // handle failure
    }
    else
    {
      try
      {
        // entire file is returned via downloadHandler
        //string fileContents = request.downloadHandler.text;
        // or
        //byte[] fileContents = request.downloadHandler.data;

        // do whatever you need to do with the file contents
        if (loadAsset(fileContents))
          isAssetRead = true;
      }
      catch (Exception x)
      {
        // handle failure
      }
    }
  }
}