经过测试在安卓平台无法用File来读取StraemingAssets文件夹里的内容
会找不到文件
string path;
void Awake()
{
string filepath = Application.streamingAssetsPath;
path = filepath + "/Lua/test.txt";
LoadTextAsset();
GetAllFileTextInfos();
StartCoroutine(StreamReaderJsonFromSSPath());
}
public string LoadTextAsset()
{
return File.ReadAllText(path);
}
public string GetAllFileTextInfos()
{
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
fs.Seek(0, SeekOrigin.Begin);
var bytes = new byte[fs.Length];
fs.Read(bytes, 0, (int)fs.Length);
string s = Encoding.UTF8.GetString(bytes);
fs.Flush();
fs.Dispose();
fs.Close();
return s;
}
//Debug.Log("所有文件资源信息Assets下文件夹数量:" + fileInfos.Count);
}
catch (Exception e)
{
Debug.LogError(e.Message);
return null;
}
}
IEnumerator StreamReaderJsonFromSSPath()
{
UnityWebRequest webRequest = UnityWebRequest.Get(path);
webRequest.SendWebRequest();//读取数据
string str;
while (true)
{
yield return new WaitForSeconds(0.1f);
if (webRequest.result == UnityWebRequest.Result.ConnectionError)
{
Debug.LogError(path + "请求错误:" + webRequest.error);
}
else
{
if (webRequest.downloadHandler.isDone)//是否读取完数据
{
//读取
//str = Encoding.UTF8.GetString(webRequest.downloadHandler.data);
str = webRequest.downloadHandler.text;
break;
}
}
}
Debug.Log(str);
webRequest.Dispose();
}
public void GetFiles()
{
//路径
string filepath = Application.streamingAssetsPath;
//获取指定路径下面的所有资源文件
if (Directory.Exists(filepath))
{
DirectoryInfo direction = new DirectoryInfo(filepath);
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
Debug.Log(files.Length);
for (int i = 0; i < files.Length; i++)
{
if (files[i].Name.EndsWith(".meta"))
{
continue;
}
Debug.Log("Name:" + files[i].Name);
Debug.Log("FullName:" + files[i].FullName);
Debug.Log("DirectoryName:" + files[i].DirectoryName);
}
}
}