StreamingAssets也是Unity中特殊的文件夹,用于存放运行时可以直接访问的资源。StreamingAssets一般存放数据或配置文件、图片、视频资源等。
StreamingAssets的文件路径可以通过Application.streamingAssetsPath来获取。
加载或访问使用WWW类或UnityWebRequest类。如:
WWW类:
// 在StreamingAssets文件夹中的资源路径
string filePath = Path.Combine(Application.streamingAssetsPath, "data.txt");
IEnumerator LoadTextureFromFile3(string filePath)
{
WWW www = new WWW(filePath);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
var data = www.text;
}
else
{
Debug.LogError("下载失败:" + www.error);
}
}
UnityWebRequest类:
// 在StreamingAssets文件夹中的资源路径
string filePath = Path.Combine(Application.streamingAssetsPath, "data.txt");
UnityWebRequest www = UnityWebRequest.Get(filePath);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
string data = www.downloadHandler.text;
// 处理数据
}
else
{
Debug.LogError("加载失败: " + www.error);
}