Unity resource path and reading

Unity resource paths are:

1. StreamingAssets: read-only, generally used to store resource files that need to be loaded when the application is running, and can be obtained through Application.streamingAssetsPath.

2. PersistentDataPath: Readable and writable. It is generally used to store files generated or downloaded when the application is running. It can be obtained through Application.persistentDataPath.

3. DataPath: The installation directory of the application. It contains the executable file and default resource files of the application, which can be obtained through Application.dataPath.

4. TemporaryCachePath: Temporary cache directory, used to store temporary files, which can be obtained through Application.temporaryCachePath.

The actual paths of resource paths in different platforms are different. Please refer to the following table:

api: Application.dataPath

Application.streamingAssetsPath

Application.persistentDataPath
Windows

例:C:/Projects/YourProject/

Assets

example:

C:/Projects/YourProject/

Assets/StreamingAssets

example:

C:/Users/YourUsername/AppData/

LocalLow/CompanyName/

YourProjectName

iOS

example:

/var/containers/Bundle/

Application/AppId/

YourProjectName.app/

Data

example:

/var/containers/Bundle/

Application/AppId/

YourProjectName.app/

Data/Raw/

StreamingAssets

example:

/var/mobile/Containers/Data/

Application/AppId/Documents

Android

example:

/data/app/com.

companyname.

yourprojectname-1/base.apk

example:

/data/app/com.

companyname.

yourprojectname-1/base.apk!/assets

example:

/storage/emulated/0/Android/

data/com.companyname.

yourprojectname/files

The path above is a relative path, not a specific file path. The exact file path will vary depending on where the application is installed. Therefore, to ensure that there is no error, when we read and write resources, the best way is to use the API method of the code (i.e., row 1 in the above table) to create the resource path and read the resource.

On platforms such as Windows, iOS, Android and Mac, you can use the WWW class, UnityWebRequest class or File class to read these resource paths. However, it should be noted that on the Android platform, the files in the StreamingAssets path are compressed. , it cannot be read directly using the File class. You need to use the WWW class or UnityWebRequest class to read. like:

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

Guess you like

Origin blog.csdn.net/mr_five55/article/details/134817873