Application.streamingAssetsPath和WebGL构建
在一个项目中,我正在StreamingAssets目录中有两个json文件。处理它们的脚本在独立PC版本中可完美运行,但在WebGL中完全不起作用。
我收到“找不到文件!” 根据脚本消息:
else if (!File.Exists (filePath)) {
Debug.LogError ("Cannot find file!");
}
我得到了使用Unity站点上的脚本API所描述的WWW类的答案,地址为: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);
}
}
我很乐意这样做,但是我在编码方面太新了,我需要一些解释。我现在有的第一个问题是:该行中的 字符串是什么?
public string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "MyFile");
我应该在那里写什么?是网址吗?如果是网址,该网址是什么?
如果有人可以握住我的手并引导我了解这一点,我将非常感激!谢谢!
(这是我在这里的第一个问题;我希望我没有犯错,因为我还不知道这个地方的工作方式。)
回答:
我现在有的第一个问题是:该行中的“我的文件”字符串是什么?
那应该是文件名,尽管它缺少扩展名。您应该添加。例如 .txt , .jpg , png ....
我应该在那里写什么?是网址吗?如果是网址,该网址是什么?
您只应该在扩展名 “ MyFile” 所在的位置写文件名。
在您的项目中,创建一个名为“ StreamingAssets ” 的文件夹。
假设您有一个名为 “ Anne.txt” 的文件,并且该文件位于 “ StreamingAssets”中 。文件夹,这应该是您的路径:
public string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "Anne.txt");
现在,假设将“ Anne.txt ”文件夹放置在名为“ Data ”的文件夹中,然后将其放置在“ StreamingAssets
”文件夹中,它的外观应为:“ StreamingAssets / Data / Anne.txt ”。
您的路径应为:
public string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "Data");filePath = System.IO.Path.Combine(filePath , "Anne.txt");
而已。这里没什么复杂的。然后,将该路径字符串与一起使用WWW
。
你也 if (filePath.Contains("://"))
应该是if (filePath.Contains ("://") ||
filePath.Contains (":///"))。
如果您要加载多个文件,则我将该函数简化为该函数,以便它将文件名作为参数。
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);
}
现在,假设您在“ StreamingAssets ”文件夹中放置了3个名为“ Anne.txt ”,“ AnotherAnne.txt ”和“
OtherAnne.txt ”的文件,可以使用以下代码加载它们: __
StartCoroutine(loadStreamingAsset("Anne.txt"));StartCoroutine(loadStreamingAsset("AnotherAnne.txt"));
StartCoroutine(loadStreamingAsset("OtherAnne.txt"));
以上是 Application.streamingAssetsPath和WebGL构建 的全部内容, 来源链接: utcz.com/qa/422815.html