2

在 unity3d 中使用 XmlDocument 函数时有什么需要考虑的吗?我遇到了这个奇怪的问题:当从 Awake() 或 OnGUI() 调用使用 XmlDocument 的函数时,文档被成功编辑。但是,当从按钮事件内部调用它时,事件很难在保存文档之前得到一个编辑良好的字符串,它无法修改文档本身。

编辑文件的函数(有时):

    public static void addTestProfile () {

            string path = Application.dataPath + "/Documents/Profiles.xml";
            Hashtable tempTable = new Hashtable();
            tempTable.Add("user", "chuck II");
            tempTable.Add("url", "funny");
            tempTable.Add("passwrod", "1234asdf");
            General.StoreProfile(tempTable, path);
        }


public static void StoreProfile(Hashtable profile, string path) {
        Debug.Log("profile to store name: " + profile["password"]);
        XmlDocument doc = new XmlDocument();
        doc.Load(profilesPath);

        XmlElement element = doc.CreateElement("Profile");

        XmlElement innerElement1 = doc.CreateElement("user");
        innerElement1.InnerText = profile["user"] as string;
        element.AppendChild(innerElement1);

        XmlElement innerElement2 = doc.CreateElement("url");
        innerElement2.InnerText = profile["url"] as string;
        element.AppendChild(innerElement2);

        XmlElement innerElement3 = doc.CreateElement("password");
        innerElement3.InnerText = profile["password"] as string;
        element.AppendChild(innerElement3);
        doc.DocumentElement.AppendChild(element);

        doc.Save(profilesPath);
        Debug.Log(doc.InnerXml);
    }

我创建了一个新项目只是为了测试这个问题,在调用 Application.loadLevel(); 之前调用文件时不会编辑文件;

在这里它运行良好并且文件本身被编辑:

void OnGUI () {
         General.addTestProfile();  // General is the singleton class that contains the function implementation
}

但是有些这不起作用:

// GUI Save btn
if (GUI.Button(new Rect(255, 20, 60, 35), "Add")) {
      General.addTestProfile();   // General is the singleton class that contains the function implementation
      Application.LoadLevel(0);
}

当我在 save() xmlDocument 函数之前打印结果字符串时,它显示了新项目,但不知何故 xml 文件保持不变。我是否遗漏了一些可能与执行顺序有关的重要内容?像超时之类的东西?

4

2 回答 2

1

这只是一个疯狂的猜测,但这可行吗?

// PSEUDOCODE
bool pressed

function OnGUI()
   if GUI.Button then
      pressed = true
   end if
   if pressed then
      addTestProfile();
   end if
end function

我从这个链接获得了帮助:http: //answers.unity3d.com/questions/9538/new-to-unity-3d-gui-button-help-needed-please

于 2011-02-01T02:36:47.573 回答
1

当它跳回场景 1 时,它正在重写原始文件。

“Debug.Log”糟透了!,该指令从未打印出对 createProfilesFile() 函数的第二次调用。所以只少了一行:

if (System.IO.File.Exists(profilesPath)) return;

这里的 createProfilesFile() 函数:

public static void CreateProfilesFile (string path) {
    Debug.Log("create init");      // This line wasn't called the second time ... 

    if (System.IO.File.Exists(path)) return;

    // Create a new file specified path
    XmlTextWriter textWriter = new XmlTextWriter(path,null);
    // Opens the document
    textWriter.WriteStartDocument();
    // Write comments
    textWriter.WriteComment("This document contains the profiles that have been created.");
    textWriter.WriteStartElement("Profiles");
        textWriter.WriteStartElement("Profile");
            textWriter.WriteStartElement("user");
                textWriter.WriteString("foo user");
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("url");
                textWriter.WriteString("foo url");
            textWriter.WriteEndElement();       
            textWriter.WriteStartElement("password");
                textWriter.WriteString("foo password");
            textWriter.WriteEndElement();
        textWriter.WriteEndElement();
    textWriter.WriteEndElement();
    // Ends the document.
    textWriter.WriteEndDocument();
    // close writer
    textWriter.Close();
}
于 2011-02-09T16:21:05.693 回答