西安网站优化推广方案,新网站做百度百科,电商小程序报价,查询网站是否被收录Unity资源路径有#xff1a;
1、StreamingAssets#xff1a;只读#xff0c;一般用于存放应用程序运行时需要加载的资源文件#xff0c;可以通过Application.streamingAssetsPath来获取。
2、PersistentDataPath#xff1a;可读写#xff0c;一般用于存放应用程序运行时…Unity资源路径有
1、StreamingAssets只读一般用于存放应用程序运行时需要加载的资源文件可以通过Application.streamingAssetsPath来获取。
2、PersistentDataPath可读写一般用于存放应用程序运行时生成或下载的文件可以通过Application.persistentDataPath来获取。
3、DataPath应用程序的安装目录它包含了应用程序的可执行文件和默认资源文件可以通过Application.dataPath来获取。
4、TemporaryCachePath临时缓存目录用于存放临时文件可以通过Application.temporaryCachePath来获取。
不同平台中资源路径的实际路径是不一样的可参考下表
api:Application.dataPath Application.streamingAssetsPath Application.persistentDataPathWindows 例C:/Projects/YourProject/ Assets 例 C:/Projects/YourProject/ Assets/StreamingAssets 例 C:/Users/YourUsername/AppData/ LocalLow/CompanyName/ YourProjectName iOS 例 /var/containers/Bundle/ Application/AppId/ YourProjectName.app/ Data 例 /var/containers/Bundle/ Application/AppId/ YourProjectName.app/ Data/Raw/ StreamingAssets 例 /var/mobile/Containers/Data/ Application/AppId/Documents Android 例 /data/app/com. companyname. yourprojectname-1/base.apk 例 /data/app/com. companyname. yourprojectname-1/base.apk!/assets 例 /storage/emulated/0/Android/ data/com.companyname. yourprojectname/files
上面的路径是相对路径并不是具体的文件路径。具体的文件路径会根据应用程序安装位置而变化。所以要保证不出错我们在读写资源的时候最好的办法就是使用代码的API方法即上表行一创建资源路径和读取资源。
在Windows、iOS、Android和Mac等平台上读取这些资源路径可以使用WWW类、UnityWebRequest类或者File类来进行读取不过需要注意的是在Android平台上StreamingAssets路径下的文件是压缩的无法直接使用File类读取需要使用WWW类或UnityWebRequest类来进行读取。如
string filePath1 Path.Combine(Application.streamingAssetsPath, filename.txt);
string filePath2 Path.Combine(Application.persistentDataPath, filename.txt);
string filePath3 Path.Combine(Application.dataPath, filename.txt);#if UNITY_ANDROID !UNITY_EDITOR
// 在Android平台上StreamingAssets路径下的文件是压缩的需要使用WWW类来读取
StartCoroutine(LoadFile(filePath1));#else
// 在其他平台上可以直接使用File类来读取
string files1 File.ReadAllText(filePath1);
string files2 File.ReadAllText(filePath2);
string files3 File.ReadAllText(filePath3);
#endif// 在Android平台上使用WWW类读取StreamingAssets路径下的文件
private IEnumerator LoadFile(string filePath)
{using (UnityWebRequest www UnityWebRequest.Get(filePath)){yield return www.SendWebRequest();if (www.result UnityWebRequest.Result.Success){string fileContent www.downloadHandler.text;Debug.Log(fileContent);}else{Debug.LogError(Failed to load file: www.error);}}
}