【问题标题】:Unity Save Load Function: Using Application.persistentDataPathUnity保存加载功能:使用Application.persistentDataPath
【发布时间】:2021-11-24 12:30:41
【问题描述】:

我是一名初学者 Unity 开发人员。 我使用支持存储功能的资产制作了保存和加载功能。 常用的保存方式是在Application.personDataPath/文件夹下创建一个Json形式的文件。

问题在于它在 Unity 编辑器中运行良好,但当我将它构建为一个完整的游戏时就不行了。当我在目标文件夹 Application.persistantDataPath/ 中查找时,没有创建任何内容。

是否有您经常使用的解决方案或诊断方法?

​更多信息,请看下面的代码。 我使用了从 Unity Asset Store 购买的名为“Corgi Engine”的资源。

设置文件路径

/// the method to use when saving and loading files (has to be the same at both times of course)

public static IMMSaveLoadManagerMethod saveLoadMethod = new MMSaveLoadManagerMethodBinary();

/// the default top level folder the system will use to save the file

private const string _baseFolderName = "/MMData/";

/// the name of the save folder if none is provided

private const string _defaultFolderName = "MMSaveLoadManager";

​

/// <summary>

/// Determines the save path to use when loading and saving a file based on a folder name.

static string DetermineSavePath(string folderName = _defaultFolderName)

{

string savePath;

// depending on the device we're on, we assemble the path

if (Application.platform == RuntimePlatform.IPhonePlayer)

{

savePath = Application.persistentDataPath + _baseFolderName;

} 

else 

{

savePath = Application.persistentDataPath + _baseFolderName;

}

#if UNITY_EDITOR

savePath = Application.dataPath + _baseFolderName;

#endif

​

savePath = savePath + folderName + "/";

return savePath;

}

创建目录并保存

/// <summary>

/// Save the specified saveObject, fileName and foldername into a file on disk.

public static void Save(object saveObject, string fileName, string foldername = _defaultFolderName)

{

string savePath = DetermineSavePath(foldername);

string saveFileName = DetermineSaveFileName(fileName);

// if the directory doesn't already exist, we create it

if (!Directory.Exists(savePath))

{

Directory.CreateDirectory(savePath);

}

// we serialize and write our object into a file on disk

​

FileStream saveFile = File.Create(savePath + saveFileName);

​

saveLoadMethod.Save(saveObject, saveFile);

saveFile.Close();

}

转换为 Json 并保存

/// <summary>

/// Saves the specified object at the specified location after converting it to json

public void Save(object objectToSave, FileStream saveFile)

{

string json = JsonUtility.ToJson(objectToSave);

// if you prefer using NewtonSoft's JSON lib uncomment the line below and commment the line above

//string json = Newtonsoft.Json.JsonConvert.SerializeObject(objectToSave);

StreamWriter streamWriter = new StreamWriter(saveFile);

streamWriter.Write(json);

streamWriter.Close();

saveFile.Close();

Debug.Log(objectToSave.GetType());

}

【问题讨论】:

  • 没用过StreamWriter,但是对于Unity我一般只用System.IO.File.WriteAllText ( pathWhereToStore, JsonUtility.ToJson ( objectToSave ) )

标签: c# json unity3d save


【解决方案1】:

正如您的代码所说,您在编辑器模式下使用 Application.dataPath(资产文件夹),而在 Android 和 iOS 中使用 Application.persistentDataPath。这就是结果不同的原因。

请参阅 firstsecond 的文档。查看每种情况下文件的存储位置。

如果您仍然无法在 Android 中找到您的文件,请尝试运行时调试 - 运行 Debug.Log(Application.persistentDataPath); 或将其分配给场景中的某个文本对象

【讨论】:

    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 2021-12-11
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多