Application.persistentDataPath在构建中的位置

Gre*_*aue 3 c# json unity-game-engine unity5

当我从编辑器运行游戏并保存加载数据时,我发现数据在:C:\Users\User\AppData\LocalLow\DefaultCompany\projectname\data.

当我构建它并获得可执行文件时,该数据仍可正常加载,但如果我保存并重新启动,则不会保存.但是当我从编辑器中启动它时它会这样做.

这是我的代码中的错误,还是我不从Unity Editor运行它的其他地方的文件?

稍后当我导出游戏以启动游戏时,我会拥有持久数据I Json文件,这些文件必须与游戏一起使用才能运行.

为清楚起见,这里是处理Json的保存/加载的类:

public class DataHandler
{
    //Save Data
    public static void saveData<T>(T dataToSave, string dataFileName)
    {
        string tempPath = Path.Combine(Application.persistentDataPath, "data");
        tempPath = Path.Combine(tempPath, dataFileName + ".txt");

        //Convert To Json then to bytes
        string jsonData = JsonUtility.ToJson(dataToSave, true);
        byte[] jsonByte = Encoding.ASCII.GetBytes(jsonData);

        //Create Directory if it does not exist
        if (!Directory.Exists(Path.GetDirectoryName(tempPath)))
        {
            Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
        }
        //Debug.Log(path);

        try
        {
            File.WriteAllBytes(tempPath, jsonByte);
            Debug.Log("Saved Data to: " + tempPath.Replace("/", "\\"));
        }
        catch (Exception e)
        {
            Debug.LogWarning("Failed To PlayerInfo Data to: " + tempPath.Replace("/", "\\"));
            Debug.LogWarning("Error: " + e.Message);
        }
    }

    //Load Data
    public static T loadData<T>(string dataFileName)
    {
        string tempPath = Path.Combine(Application.persistentDataPath, "data");
        tempPath = Path.Combine(tempPath, dataFileName + ".txt");

        //Exit if Directory or File does not exist
        if (!Directory.Exists(Path.GetDirectoryName(tempPath)))
        {
            Debug.LogWarning("Directory does not exist");
            return default(T);
        }

        if (!File.Exists(tempPath))
        {
            Debug.Log("File does not exist");
            return default(T);
        }

        //Load saved Json
        byte[] jsonByte = null;
        try
        {
            jsonByte = File.ReadAllBytes(tempPath);
            Debug.Log("Loaded Data from: " + tempPath.Replace("/", "\\"));
        }
        catch (Exception e)
        {
            Debug.LogWarning("Failed To Load Data from: " + tempPath.Replace("/", "\\"));
            Debug.LogWarning("Error: " + e.Message);
        }

        //Convert to json string
        string jsonData = Encoding.ASCII.GetString(jsonByte);

        //Convert to Object
        object resultValue = JsonUtility.FromJson<T>(jsonData);
        return (T)Convert.ChangeType(resultValue, typeof(T));
    }

    public static bool deleteData(string dataFileName)
    {
        bool success = false;

        //Load Data
        string tempPath = Path.Combine(Application.persistentDataPath, "data");
        tempPath = Path.Combine(tempPath, dataFileName + ".txt");

        //Exit if Directory or File does not exist
        if (!Directory.Exists(Path.GetDirectoryName(tempPath)))
        {
            Debug.LogWarning("Directory does not exist");
            return false;
        }

        if (!File.Exists(tempPath))
        {
            Debug.Log("File does not exist");
            return false;
        }

        try
        {
            File.Delete(tempPath);
            Debug.Log("Data deleted from: " + tempPath.Replace("/", "\\"));
            success = true;
        }
        catch (Exception e)
        {
            Debug.LogWarning("Failed To Delete Data: " + e.Message);
        }

        return success;
    }
}
Run Code Online (Sandbox Code Playgroud)

Pro*_*mer 14

在下面的答案中:

  • companyname = 构建设置中的公司名称
  • productname = 构建设置中的产品名称

在此输入图像描述

Windows:

C:\Users\<userprofile>\AppData\LocalLow\<companyname>\<productname>
Run Code Online (Sandbox Code Playgroud)

Windows商店:

%userprofile%\AppData\Local\Packages\<productname>\LocalState
Run Code Online (Sandbox Code Playgroud)

Mac:

~/Library/Application Support/companyname/productname
Run Code Online (Sandbox Code Playgroud)

旧版Unity on Mac:

  • ~/Library/Caches folder

  • ~/Library/Application Support/unity.companyname.productname.

Linux:

$XDG_CONFIG_HOME/unity3d/<companyname>/<productname>
Run Code Online (Sandbox Code Playgroud)

这与...相同

~/.config/unity3d/<companyname>/<productname>
Run Code Online (Sandbox Code Playgroud)

Android:

/Data/Data/com.<companyname>.<productname>/files
Run Code Online (Sandbox Code Playgroud)

在Android设备上使用SD卡:

/storage/sdcard0/Android/data/com.<companyname>.<productname>/files
Run Code Online (Sandbox Code Playgroud)

iOS:

/var/mobile/Containers/Data/Application/<RandomFolderName>/Documents
Run Code Online (Sandbox Code Playgroud)

RandomFolderName全名的示例:

/var/mobile/Containers/Data/Application/<055811B9-D125-41B1-A078-F898B06F8C58>/Documents
Run Code Online (Sandbox Code Playgroud)

在iOS上,您将可以访问应用程序的沙箱,即文件夹.您必须在此目录中创建文件夹才能在其中创建新文件.


如果您在json文件中有默认数据值,请将该文件放在Assets/Resources文件夹中,以便它是只读的,然后用它读取TextAsset.加载游戏时,您可以使用它PlayerPrefs来检查这是否是第一次加载游戏.

如果这是第一次,请使用来自的值TextAsset.text.如果不使用那么随DataHandler类保存的值.

大概是这样的:

if (PlayerPrefs.GetInt("FIRSTTIMEOPENING", 1) == 1)
{
    Debug.Log("First Time Opening");

    //Set first time opening to false
    PlayerPrefs.SetInt("FIRSTTIMEOPENING", 0);

    //USE TextAsset to load data
    TextAsset txtAsset = (TextAsset)Resources.Load("player", typeof(TextAsset));
    string tileFile = txtAsset.text;
    PlayerInfo pInfo = JsonUtility.FromJson<PlayerInfo>(tileFile);
}
else
{
    Debug.Log("NOT First Time Opening");

    //USE DataHandler to load data
    PlayerInfo pInfo = DataHandler.loadData<PlayerInfo>("player");
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,很好的答案。那么为什么我只需要在 firstTimeOpening 上阅读资源呢?另外,这也适用于 Sprites 吗?因为如果我需要在运行时更改它们,我会使用 `Resources.Load` 设置大量精灵图像。 (2认同)