Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
888 views
in Technique[技术] by (71.8m points)

c# - Application.streamingAssetsPath and WebGL build

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

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.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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?

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

Example uses:

In your Project, you create a folder called "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");

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".

Your path should be:

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

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

Also your if (filePath.Contains("://")) should be if (filePath.Contains ("://") || filePath.Contains (":///")).

EDIT

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...