Application.persistentDataPath 在构建中的位置 [英] Location of Application.persistentDataPath in a build

查看:66
本文介绍了Application.persistentDataPath 在构建中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从编辑器运行游戏并保存加载数据时,我发现该数据位于:C:UsersUserAppDataLocalLowDefaultCompanyprojectnamedata.

当我构建它并获得一个可执行文件时,该数据仍然可以正常加载,但是如果我保存并重新启动,它不会被保存.但是,当我从编辑器启动它时,它确实如此.

这是我的代码中的错误还是当我不从 Unity 编辑器运行它时文件在其他地方?

稍后当我导出游戏以启动它时,我有持久数据和 Json 文件,这些数据必须与游戏一起运行.

为清楚起见,

Windows:

C:UsersAppDataLocalLow

Windows 应用商店:

%userprofile%AppDataLocalPackagesLocalState

<小时>

Mac:

~/Library/Application Support/companyname/productname

Mac 上的旧版 Unity:

  • ~/Library/Caches 文件夹

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

Linux:

$XDG_CONFIG_HOME/unity3d/<公司名称>/<产品名称>

相同

~/.config/unity3d//

安卓:

/Data/Data/com../files

在 Android 设备上使用 SD 卡:

/storage/sdcard0/Android/data/com../files

iOS:

/var/mobile/Containers/Data/Application//Documents

RandomFolderName 全名示例:

/var/mobile/Containers/Data/Application/<055811B9-D125-41B1-A078-F898B06F8C58>/Documents

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

<小时>

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

如果这是第一次,请使用 TextAsset.text.如果不使用,则使用 DataHandler 类保存值.

大概是这样的:

if (PlayerPrefs.GetInt("FIRSTTIMEOPENING", 1) == 1){Debug.Log("第一次打开");//设置第一次打开为falsePlayerPrefs.SetInt("FIRSTTIMEOPENING", 0);//使用TextAsset加载数据TextAsset txtAsset = (TextAsset)Resources.Load("player", typeof(TextAsset));字符串 tileFile = txtAsset.text;PlayerInfo pInfo = JsonUtility.FromJson(tileFile);}别的{Debug.Log("不是第一次打开");//使用DataHandler加载数据PlayerInfo pInfo = DataHandler.loadData("player");}

When I run my game from the editor and save load data, I find that data in: C:UsersUserAppDataLocalLowDefaultCompanyprojectnamedata.

When I build it and get an executable, that data still loads fine, but if I save and restart, it does not get saved. However it does when I launch it from the Editor.

Is this a fault in my code or is the files somewhere else when I don't run it from Unity Editor?

Later when I export the game to launch it, I have persistent data I Json files that have to come with it for the game to work.

For clarity, here is the class that handles save/load of 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;
    }
}

解决方案

In the answer below:

  • companyname = Company name from the Build Settings
  • productname = Product name from the Build Settings

Windows:

C:Users<userprofile>AppDataLocalLow<companyname><productname>

Windows Store:

%userprofile%AppDataLocalPackages<productname>LocalState


Mac:

~/Library/Application Support/companyname/productname

older version of Unity on Mac:

  • ~/Library/Caches folder

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

Linux:

$XDG_CONFIG_HOME/unity3d/<companyname>/<productname>

which is the-same as

~/.config/unity3d/<companyname>/<productname>

Android:

/Data/Data/com.<companyname>.<productname>/files

with SD card on the Android device:

/storage/sdcard0/Android/data/com.<companyname>.<productname>/files

iOS:

/var/mobile/Containers/Data/Application/<RandomFolderName>/Documents

Example of the RandomFolderName full name:

/var/mobile/Containers/Data/Application/<055811B9-D125-41B1-A078-F898B06F8C58>/Documents

On iOS, you will be given access to the app's sandbox which is the Document folder. You must create folder inside this directory in order to create a new file inside it.


If you have a default data values in a json file, put the file in Assets/Resources folder so that it will be read-only then read it with TextAsset. When the game loads, you can use PlayerPrefs to check if this is the first time the game being loaded.

If this is the first time, use the value from TextAsset.text. If it is not use then value saved with the DataHandler class.

Roughly something like this:

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

这篇关于Application.persistentDataPath 在构建中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆