码迷,mamicode.com
首页 > 编程语言 > 详细

[Unity实战]剧情对话(三)

时间:2015-04-19 10:17:02      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:剧情

在这里,我们使用xml来实现保存与读取功能。


首先,是头文件的引用:

using System.Xml;

保存到xml文件:

void Save()
    {
        string filePath = Application.dataPath + @"/my.xml";

        XmlDocument xmlDoc = new XmlDocument();
        XmlElement root = xmlDoc.CreateElement("root");

        for (int i = 0; i < options.Count; i++)
        {
            XmlElement child = xmlDoc.CreateElement("child");
            XmlElement x = xmlDoc.CreateElement("type");
            x.InnerText = options[i].ToString();
            child.AppendChild(x);

            int count = 1;
            for (int q = 0; q < strings[q].Length; q++)
            {
                XmlElement q1 = xmlDoc.CreateElement("string_" + count);
                q1.InnerText = strings[i][q];
                child.AppendChild(q1);
                count++;
            }

            for (int q = 0; q < ints[q].Length; q++)
            {
                XmlElement q1 = xmlDoc.CreateElement("int_" + count);
                q1.InnerText = ints[i][q].ToString();
                child.AppendChild(q1);
                count++;
            }

            for (int q = 0; q < floats[q].Length; q++)
            {
                XmlElement q1 = xmlDoc.CreateElement("float_" + count);
                q1.InnerText = floats[i][q].ToString();
                child.AppendChild(q1);
                count++;
            }

            for (int q = 0; q < vector3s[q].Length; q++)
            {
                XmlElement q1 = xmlDoc.CreateElement("vector3_" + count);
                q1.InnerText = vector3s[i][q].ToString();
                child.AppendChild(q1);
                count++;
            }
            root.AppendChild(child);
        }

        xmlDoc.AppendChild(root);
        xmlDoc.Save(filePath);
        AssetDatabase.Refresh();
        Debug.Log("Ok!!");
    }

读取xml文件:

void Load(string xmlPath)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlPath);

        XmlNodeList childs = xmlDoc.SelectSingleNode("root").ChildNodes;//所有child
        for (int i = 0; i < childs.Count; i++)
        {
            XmlNodeList nodes = childs[i].ChildNodes;//单个child的所有节点
            
            string a = nodes[0].InnerText;
            switch (a)
            {
                case "CommandDestory":
                    commands.Add(new CommandDestory(nodes[1].InnerText));
                    break;
                case "CommandDialogue":
                    int int1 = int.Parse(nodes[6].InnerText);
                    int int2 = int.Parse(nodes[7].InnerText);
                    int int3 = int.Parse(nodes[8].InnerText);
                    commands.Add(new CommandDialogue(nodes[1].InnerText, int1, int2, int3));
                    break;
                case "CommandDialogueModeEnter":
                    commands.Add(new CommandDialogueModeEnter());
                    break;
                case "CommandDialogueModeExit":
                    commands.Add(new CommandDialogueModeExit());
                    break;
                case "CommandGenerate":
                    Vector3 v1 = StringToVector3(nodes[16].InnerText);
                    Vector3 v2 = StringToVector3(nodes[17].InnerText);
                    commands.Add(new CommandGenerate(nodes[1].InnerText,v1,v2));
                    break;
                case "CommandMove":
                    Vector3 v = StringToVector3(nodes[16].InnerText);
                    commands.Add(new CommandMove(nodes[1].InnerText, v, float.Parse(nodes[11].InnerText)));
                    break;
                case "CommandMusic":
                    commands.Add(new CommandMusic(nodes[1].InnerText));
                    break;
                case "CommandPlayAnim":
                    commands.Add(new CommandPlayAnim(nodes[1].InnerText, int.Parse(nodes[6].InnerText)));
                    break;
                case "CommandRotate":
                    Vector3 v3 = StringToVector3(nodes[16].InnerText);
                    commands.Add(new CommandRotate(nodes[1].InnerText, v3, float.Parse(nodes[11].InnerText)));
                    break;
                case "CommandWait":
                    commands.Add(new CommandWait(float.Parse(nodes[11].InnerText)));
                    break;
                default:
                    break;
            }
        }
    }

    Vector3 StringToVector3(string s)
    {
        s = s.Substring(1,s.Length - 2);
        string[] a = s.Split(',');
        Vector3 b = new Vector3(float.Parse(a[0]), float.Parse(a[1]), float.Parse(a[2]));
        return b;
    }

xml文件里的一个命令节点内容:

技术分享

可以看到,节点名称都被标号了,这样读取的时候就很容易拿到我们想要的数据了。


其实做编辑器工具是一种挺好的加快效率的方式,因为生活中,程序主要负责的是程序,策划主要就是弄表格等等的,这样就可以把程序的一部分工作交给策划来弄,策划只需在编辑器上使用这些工具,就可以实现同等的功能,可谓是双赢啊哈哈。。

[Unity实战]剧情对话(三)

标签:剧情

原文地址:http://blog.csdn.net/lyh916/article/details/45126317

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!