首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单位节省负荷功能:使用Application.persistentDataPath

单位节省负荷功能:使用Application.persistentDataPath
EN

Stack Overflow用户
提问于 2021-10-04 00:45:00
回答 1查看 1.7K关注 0票数 0

我是一个初学者团结开发人员。我使用支持存储函数的资产创建了Save & Load函数。通过在Application.personDataPath/文件夹中创建Json形式的文件来保存它,这是一种常见的方法。

问题是它在Unity中运行得很好,但是当我把它构建成一个完整的游戏时,它就不是了。当我在Application.persistantDataPath/ (它是目标文件夹)中查找时,不会创建任何内容。

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

有关​的更多信息,请参见下面的代码。我使用了从购买的名为“Corgi Engine”的资产。

设置文件的路径

代码语言:javascript
复制
/// 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;

}

创建目录并保存

代码语言:javascript
复制
/// <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的转换与的保存

代码语言:javascript
复制
/// <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());

}
EN

回答 1

Stack Overflow用户

发布于 2021-10-04 07:18:19

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

查看第一第二的文档。查看在每种情况下应将文件存储在何处。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69429928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档