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

在这里,我们使用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文件里的一个命令节点内容:


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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值