正在 Unity 中重新加载 XML 资源
Reloading XML asset in Unity
我将游戏进度存储在 XML 文件中。
由于玩家可以选择他们想玩的回合,但一旦完成就不能重复回合。
文件正在正确编辑和更新,但是,在我重新启动游戏之前,更改不会反映在游戏中。
我一直在使用 AssetDatabase.ImportAsset()
直到现在,但我需要 android 导出的替代方法。
好消息:在 Unity 中,save/read 文本文件非常容易。
重点...
您必须使用 Application.persistentDataPath(所有平台,始终 - 无一例外)。 “就这么简单!”
(A) 完全没有理由使用任何其他文件夹或路径。
(B) 事实上,您可以不使用任何其他文件夹或路径。
在Unity中读写文件就是这么简单。
using System.IO;
// IO crib sheet..
//
// get the file path:
// f = Application.persistentDataPath+"/"+fileName;
//
// check if file exists: System.IO.File.Exists(f)
// write to file: File.WriteAllText(f,t)
// delete the file if needed: File.Delete(f)
// read from a file: File.ReadAllText(f)
仅此而已。
string currentText = File.ReadAllText(filePath);
关于这里的问题,“我应该在第一个 运行 上手动添加它们吗”
很简单...
public string GetThatXMLStuff()
{
f = Application.persistentDataPath+"/"+"defaults.txt";
// check if it already exists:
if ( ! System.IO.File.Exists(f) )
{
// First run. Put in the default file
string default = "First line of file.\n\n";
File.WriteAllText(f, default);
}
// You know it exists no matter what. Just get the text:
return File.ReadAllText(f);
}
真的就这么简单 - 没什么。
我将游戏进度存储在 XML 文件中。 由于玩家可以选择他们想玩的回合,但一旦完成就不能重复回合。 文件正在正确编辑和更新,但是,在我重新启动游戏之前,更改不会反映在游戏中。
我一直在使用 AssetDatabase.ImportAsset()
直到现在,但我需要 android 导出的替代方法。
好消息:在 Unity 中,save/read 文本文件非常容易。
重点...
您必须使用 Application.persistentDataPath(所有平台,始终 - 无一例外)。 “就这么简单!”
(A) 完全没有理由使用任何其他文件夹或路径。
(B) 事实上,您可以不使用任何其他文件夹或路径。
在Unity中读写文件就是这么简单。
using System.IO;
// IO crib sheet..
//
// get the file path:
// f = Application.persistentDataPath+"/"+fileName;
//
// check if file exists: System.IO.File.Exists(f)
// write to file: File.WriteAllText(f,t)
// delete the file if needed: File.Delete(f)
// read from a file: File.ReadAllText(f)
仅此而已。
string currentText = File.ReadAllText(filePath);
关于这里的问题,“我应该在第一个 运行 上手动添加它们吗”
很简单...
public string GetThatXMLStuff()
{
f = Application.persistentDataPath+"/"+"defaults.txt";
// check if it already exists:
if ( ! System.IO.File.Exists(f) )
{
// First run. Put in the default file
string default = "First line of file.\n\n";
File.WriteAllText(f, default);
}
// You know it exists no matter what. Just get the text:
return File.ReadAllText(f);
}
真的就这么简单 - 没什么。