【问题标题】:Reloading XML asset in Unity在 Unity 中重新加载 XML 资源
【发布时间】:2016-03-25 15:20:10
【问题描述】:

我将游戏进度存储在 XML 文件中。 由于玩家可以选择他们想要玩的回合,但一旦完成就不能重复回合。 文件正在被正确编辑和更新,但是在我重新启动游戏之前,这些更改不会反映在游戏中。

到目前为止,我一直在使用 AssetDatabase.ImportAsset(),但我需要一个替代 android 导出的方法。

【问题讨论】:

  • 1) 具体哪些变化在游戏重新开始之前没有体现出来? 2) 玩家是否在游戏中途切换不同的“进度游戏存档”?
  • 感谢@joe 帮了大忙。如果你没问题,你也可以在这里回答,或者我可以使用该信息回答我自己的问题。
  • @JoeBlow 我看了你的方法,但我有两个问题。首先,persistentDataPath 文件夹中的数据很容易被用户获取和编辑,关于在游戏开始时在那里添加 XML 文件,我应该在第一次运行时手动添加它们。

标签: xml unity3d unity5


【解决方案1】:

好消息:在 Unity 中保存/读取文本文件非常容易。

关键点……

您必须使用 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);
 }

真的就这么简单——没什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多