【问题标题】:Unity XmlDocument function not always workingUnity XmlDocument 功能并不总是有效
【发布时间】:2011-06-18 23:02:33
【问题描述】:

在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 文件保持不变。 我是否遗漏了一些可能与执行顺序有关的重要内容?像超时之类的东西?

【问题讨论】:

    标签: c# xmldocument unity3d delayed-execution


    【解决方案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

    【讨论】:

      【解决方案2】:

      当它跳回场景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();
      }
      

      【讨论】:

      • 在任何一种情况下,我都不建议在 GUI 循环中执行文件 IO。在大多数情况下,最好使用某种形式的协程来执行该功能,因为您已经注意到 OnGUI 调用被多次调用。一般来说,您永远不会直接在 OnGUI 调用中执行任何实际上不绘制 GUI 的操作。
      猜你喜欢
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 2021-08-02
      • 2016-05-10
      • 2012-11-17
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多