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