Unity WebGL外部资产

Unity WebGL External Assets

本文关键字:外部 WebGL Unity      更新时间:2024-03-31

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

我想我错过了一些非常明显的东西,但我似乎需要一些帮助,我一周前刚刚开始学习团结,我对C#或JavaScript不是很好(我正在努力变得更好…)这在某种程度上与一些JavaScript安全问题有关吗?

有人能告诉我正确的方向吗?我应该如何在Unity WebGL中阅读图像(无需写作)?

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

根据webGL中的unity3d.com,构建除线程和反射之外的所有内容都是支持的,所以IO应该可以工作——或者我认为是这样:S

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

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

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

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

调试日志告诉我它看起来像这样:

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的访问被拒绝

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

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

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

2) 将名为Loader的脚本添加到Loading场景中的Main Camera。

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

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

例如:

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

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

基本上不支持低级别IO操作。