gpt4 book ai didi

c# - Application.persistentDataPath 在构建中的位置

转载 作者:行者123 更新时间:2023-11-30 13:18:47 26 4
gpt4 key购买 nike

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

当我构建它并获得可执行文件时,该数据仍然可以正常加载,但如果我保存并重新启动,它就不会被保存。但是,当我从编辑器中启动它时,它会发生。

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

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

为清楚起见,here是处理 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;
}
}

最佳答案

在下面的答案中:

  • companyname = 来自Build Settings
  • 的公司名称
  • productname = 来自Build Settings
  • 的产品名称

enter image description here

window :

C:\Users\<userprofile>\AppData\LocalLow\<companyname>\<productname>

Windows 商店:

%userprofile%\AppData\Local\Packages\<productname>\LocalState

Mac:

~/Library/Application Support/companyname/productname

Mac 上旧版本的 Unity:

  • ~/Library/Caches 文件夹

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

Linux:

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

这与

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

安卓:

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

在 Android 设备上使用 SD 卡:

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

iOS:

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

RandomFolderName 全名示例:

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

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


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

如果这是第一次,请使用 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");
}

关于c# - Application.persistentDataPath 在构建中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44419658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com