【问题标题】:How to read and write a xml file into the same location?如何在同一位置读取和写入 xml 文件?
【发布时间】:2016-09-10 01:19:08
【问题描述】:

我已经准备了包含一些内容的 xml 文件,并希望在 iOS 设备上播放时加载它,但我也想更改加载的数据并再次将其序列化到同一个文件中。 在 Unity 编辑器(Windows)中它运行良好,但是当我在 iOS 设备上测试它时,我似乎可以使用 WWW 类从 StreamingAssets 读取,但我不能写入它。 我还发现我可以读取和写入由 Application.persistentDataPath 创建的路径。但似乎该位置位于设备中的某个位置,我无法将我的 xml 放入该位置,并且用户可以访问该文件夹,所以这不是一个好的解决方案,不是吗?

这里是我用来加载和保存数据的代码。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

public class testxml : MonoBehaviour {

public Text result;
public InputField firstPart, secondPart;
public Toggle toggle;

private List<int> listToSave;

// Use this for initialization
void Start () {
    listToSave = new List<int>();
}

public void Save()
{
    Serialize();
}

public void Load()
{
    StartCoroutine(Deserialize());
}

private void Serialize()
{
    string path = GetPath();
    try
    {
        Debug.Log("trying to save");

        var serializer = new XmlSerializer(typeof(List<int>));
        using (var fs = new FileStream(path, FileMode.OpenOrCreate))
        {
            serializer.Serialize(fs, listToSave);
        }
    }
    catch (XmlException e)
    {
        result.text = "error";
        Debug.LogError(path + "  with " + (toggle.isOn ? "persistent data path" : "data path"));
        Debug.LogError("xml exc while des file : " + e.Message);
    }
    catch (System.Exception e)
    {
        result.text = "error";
        Debug.LogError("exc while des file : " + e.Message);
        Debug.LogError(path + "  with " + (toggle.isOn ? "persistent data path" : "data path"));
        System.Exception exc = e.InnerException;
        int i = 0;
        while (exc != null)
        {
            Debug.Log("inner " + i + ": " + exc.Message);
            i++;
            exc = exc.InnerException;
        }
    }
}

private IEnumerator Deserialize()
{
    Debug.Log("trying to load");
    string path = GetPath();
    var www = new WWW(path);
    yield return www;
    if (www.isDone && string.IsNullOrEmpty(www.error))
    {
        try
        {
            var serializer = new XmlSerializer(typeof(List<int>));
            MemoryStream ms = new MemoryStream(www.bytes);
            listToSave = serializer.Deserialize(ms) as List<int>;
            ms.Close();
            result.text += "Done\n";
            foreach (var i in listToSave)
                result.text += i + "\n";
        }
        catch (XmlException e)
        {
            result.text = "error";
            Debug.LogError(path + "  with " + (toggle.isOn?"persistent data path":"data path"));
            Debug.LogError("xml exc while des file : " + e.Message);
        }
        catch (System.Exception e)
        {
            result.text = "error";
            Debug.LogError("exc while des file : " + e.Message);
            Debug.LogError(path + "  with " + (toggle.isOn ? "persistent data path" : "data path"));
            System.Exception exc = e.InnerException;
            int i = 0;
            while(exc!=null)
            {
                Debug.Log("inner "+i+": " + exc.Message);
                i++;
                exc = exc.InnerException;
            }
        }

        yield break;
    }
    else
    {
        Debug.LogError("www exc while des file " + www.error);
        Debug.LogError(path + "  with " + (toggle.isOn ? "persistent data path" : "data path"));
        yield break;
    }
}

private string GetPath()
{
    string path = firstPart.text;
    if (toggle.isOn)
    {
        path += Application.persistentDataPath;
    }
    else
        path += Application.dataPath;
    path += secondPart.text;
    return path;
}
}

【问题讨论】:

  • 你确定你一直在使用stackoverflow.com/a/36236745/294884,对吗?
  • 所以我必须一直使用 Application.persistentDataPath 吗?但是那个文件夹在哪里?
  • 您只能使用 Application.persistentDataPath。 (Unity 文档曾经提及 任何其他内容,这非常令人困惑。)您无法知道“它在哪里”,它是每次都决定的。如果您的意思是,在您的 Mac 或 PC 上,您想查看文件夹(以检查某些内容),只需将此代码添加到任意位置 Debug.Log(Application.persistentDataPath);,您就会看到它。希望对您有所帮助。
  • 是的,我有类似 /var/mobile/Containers/Data/Application/419FA6FF-1ACC-452E-A54F-4470FDA0F2E2/Documents 之类的东西,但我找不到这个文件夹
  • 你的意思是在安卓上吗?对你不能看到它。这就是电话的工作原理。 (也许越狱就能看到。)

标签: ios xml serialization unity3d deserialization


【解决方案1】:

“我想把我的xml文件放在这个文件夹中,然后读取它。就像游戏的默认信息一样”

简单,只需将其放入您的资产中即可。就这样吧……

 public TextAsset myXMLFile;

在 Inspector 中将文件拖到那里。你已经完成了。


“但我也想更改该文件并保存”

很公平。你要做的是

(1) 制作路径 p = Application.persistentDataPath + "values.txt"

(2) 程序启动。

(3) 检查“p”是否存在。如果是,请阅读并转到(6)

(4) 如果不是,请阅读 textasset 并将其保存到“p”

(5) 转到点 (3)

(6) 你完成了。

这是唯一的方法。这确实是 Unity 中的正常程序,您可以在每个 Unity 应用程序中执行此操作。没有别的办法!

【讨论】:

  • 是的,但我也想更改该文件并保存
猜你喜欢
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多