我是一个初学者团结开发人员。我使用支持存储函数的资产创建了Save & Load函数。通过在Application.personDataPath/文件夹中创建Json形式的文件来保存它,这是一种常见的方法。
问题是它在Unity中运行得很好,但是当我把它构建成一个完整的游戏时,它就不是了。当我在Application.persistantDataPath/ (它是目标文件夹)中查找时,不会创建任何内容。
是否有您经常使用的解决方案或诊断方法?
有关的更多信息,请参见下面的代码。我使用了从购买的名为“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());
}https://stackoverflow.com/questions/69429928
复制相似问题