Application.streamingAssetsPath和WebGL构建 [英] Application.streamingAssetsPath and WebGL build

查看:1052
本文介绍了Application.streamingAssetsPath和WebGL构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在处理的项目中,StreamingAssets目录中有两个json文件.处理这些脚本的脚本在Standalone PC版本中可以完美运行,而在WebGL版本中则完全不起作用.

In a project I'm working on I have two json files in the StreamingAssets directory. The script that handles them works perfectly in a Standalone PC build but doesn't work at all in a WebGL one.

我收到找不到文件!"消息根据脚本:

I'm getting the "Cannot find file!" message according to the script:

    else if (!File.Exists (filePath))
    {
        Debug.LogError ("Cannot find file!"); 
    }

我得到了使用Unity网站上的脚本API所描述的WWW类的答案,该地址位于以下地址: https://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html

I was given the answer to use the WWW class as described in the scripting API on Unity Technologies site at this address: https://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "MyFile");
    public string result = "";
    IEnumerator Example() {
        if (filePath.Contains("://")) {
            WWW www = new WWW(filePath);
            yield return www;
            result = www.text;
        } else
            result = System.IO.File.ReadAllText(filePath);
    }
}

我很乐意这样做,但是我在编码方面太新了,我需要一些解释.我现在的第一个问题是:这行中的我的文件" 字符串是什么

I would gladly do that but I'm too new at coding and I need some explanations. The first question I have for now is: what is this "my file" string in the line

    public string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "MyFile");

我应该在那写什么?是网址吗?如果是网址,该网址是什么?

What am I supposed to write there? Is it a url? And if it is a url, the url of what?

如果有人可以握住我的手,并引导我了解这一点,我将不胜感激!谢谢!

I would be very grateful is someone could hold my hand and guide me into understanding this! Thank you!

(这是我的第一个问题;我希望我没有犯错,因为我还不知道这个地方的工作方式.)

(This is my first question here; I hope I haven't made mistakes as I don't know how this place works yet.)

推荐答案

我现在的第一个问题是:这个我的文件"字符串在哪里 线

The first question I have for now is: what is this "my file" string in the line

这应该是文件名,尽管它缺少扩展名.您应该添加.例如, .txt .jpg png ....

That's supposed to be the name of the file.Although, it is missing it's extension name. You should add that. For example, .txt, .jpg, png....

我应该在那里写什么?是网址吗?如果是网址, 的网址是什么?

What am I supposed to write there? Is it a url? And if it is a url, the url of what?

您只应该将扩展名写在"MyFile" 所在的文件名上.

You are just supposed to write the name of the file with the extension name where "MyFile" is.

示例使用:

在您的项目中,创建一个名为" StreamingAssets "的文件夹.

In your Project, you create a folder called "StreamingAssets".

假设您有一个名为"Anne.txt" 的文件,并且该文件位于"StreamingAssets" 中.文件夹,这应该是您的路径:

Let's say that you have a file named "Anne.txt", and the file is inside the "StreamingAssets". folder, this should be your path:

public string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "Anne.txt");


现在,我们将" Anne.txt "文件夹放置在名为" Data "的文件夹中,然后将其放置在" StreamingAssets "中>"文件夹,它应如下所示:" StreamingAssets/Data/Anne.txt ".


Now let's say that the "Anne.txt" folder is placed in a folder called "Data" which is then in the "StreamingAssets" folder, it should look like this: "StreamingAssets/Data/Anne.txt".

您的路径应为:

public string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "Data");
filePath = System.IO.Path.Combine(filePath , "Anne.txt");

就是这样.这里没什么复杂的.然后,将该路径字符串与WWW一起使用.

That's it. Nothing complicated here. You then use that path string with WWW.

另外,您的if (filePath.Contains("://"))应该是if (filePath.Contains ("://") || filePath.Contains (":///")).

编辑

如果要加载多个文件,我将该函数简化为该函数,以便将文件名作为参数.

If you have multiple files you want to load, I simplified that function into this so that it takes the file name as parameter.

IEnumerator loadStreamingAsset(string fileName)
{
    string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);

    string result;
    if (filePath.Contains("://") || filePath.Contains(":///"))
    {
        WWW www = new WWW(filePath);
        yield return www;
        result = www.text;
    }
    else
        result = System.IO.File.ReadAllText(filePath);
}

现在,假设您有3个文件,分别是" Anne.txt "," AnotherAnne.txt "和" OtherAnne.txt " "放置在" StreamingAssets "文件夹中,则可以使用以下代码加载它们:

Now, let's say that you have 3 files called "Anne.txt", "AnotherAnne.txt" and "OtherAnne.txt" placed in the "StreamingAssets" folder, you can load them with the code below:

StartCoroutine(loadStreamingAsset("Anne.txt"));

StartCoroutine(loadStreamingAsset("AnotherAnne.txt"));

StartCoroutine(loadStreamingAsset("OtherAnne.txt"));

这篇关于Application.streamingAssetsPath和WebGL构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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