在Unity中重新加载XML资产 [英] Reloading XML asset in Unity

查看:105
本文介绍了在Unity中重新加载XML资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将游戏的进度存储在XML文件中. 由于玩家可以选择他们想要玩的回合,但一旦完成就不再重复一回合. 文件正在正确编辑和更新,但是,直到我重新启动游戏,更改才会反映在游戏内部.

I am storing the progress of the game in XML file. Since the player can choose the round they want to play, but not repeat a round once completed. The files are being editted and updated correctly, however, the changes are not reflecting inside the game until i restart the game.

直到现在我一直在使用AssetDatabase.ImportAsset(),但是我需要android导出的替代方法.

I had been using AssetDatabase.ImportAsset() until now, but i need an alternative for android export.

推荐答案

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

The good news: in Unity it's extremely easy to save/read text files.

关键点...

(A)完全没有理由使用任何其他文件夹或路径.

(A) There is utterly no reason, whatsoever, to use any other folders or paths.

(B)实际上,您可以使用任何其他文件夹或路径.

(B) Indeed, you simply can not use any other folders or paths.

在Unity中写入和读取文件非常容易.

It's this easy to write and read files in 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);

关于这里的问题,我应该在第一次运行时手动添加它们"

Regarding the question here, "should I add them manually on the first run"

很简单...

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);
 }

就这么简单-没什么.

这篇关于在Unity中重新加载XML资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆