xml的写入

写入配置文件
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using UnityEngine.UI;
using System;
//处理加载缓存 获取配置的管理器
public class ConfigManager : Singleto<ConfigManager>
{

    
   
    
    //三个字典分别对三个类进行存储
    public static  Dictionary<int, StudentArr> booscfgDic = new Dictionary<int, StudentArr>();
  
    public void LoadAllConfig(string name,string password,Text caozuoscores, Text datiscores, Text scoresAll)
    {
        booscfgDic = LoadBooscfg<StudentArr>( name, password, caozuoscores.text, datiscores.text, scoresAll.text);
       
    }
    public Dictionary<int, T> LoadBooscfg<T>(string name,string password,string caozuo,string dati,string scoressall) where T : class, new()
    {
        Dictionary<int, T> dic = new Dictionary<int, T>();
        //通过filename寻找到Resources下的文件
        //TextAsset boosCfgInfo = Resources.Load<TextAsset>("Config/"
        //  + filename);
      // Debug.Log(boosCfgInfo);
        //新建xml文档 命名空间using System.Xml自带
        XmlDocument document =new XmlDocument();
        document.Load(Player.instance.xmlpath);
        //获取根节点
        XmlNode rootNode = document.SelectSingleNode("Root");
       
        //把字符串加载到对象中

        XmlNodeList nodelist = rootNode.ChildNodes;
        Debug.Log("源文件");
        Debug.Log(nodelist.Count);
        //增添子节点
        XmlElement StudentArr = document.CreateElement("StudentArr");
        //rootNode.LastChild.InnerText.ToString() + 1最后子节点索引值
        StudentArr.SetAttribute("id", nodelist.Count.ToString());
        StudentArr.SetAttribute("name", name);
        StudentArr.SetAttribute("password", password);
        StudentArr.SetAttribute("caozuoscores", caozuo);
        StudentArr.SetAttribute("datiscores", dati);
        StudentArr.SetAttribute("ScoresAll", scoressall);
        rootNode.AppendChild(StudentArr);
       
        Debug.Log("新文件");
        //取子节点集合
        document.Save(Player.instance.xmlpath);
        Debug.Log("存上了");
        //遍历
        foreach (XmlNode node in nodelist)
        {
            Debug.Log(node.Name);
            //节点 转换 元素类型
            XmlElement element = node as XmlElement;
            Debug.Log(element.Name);
         //  Debug.Log(element.GetAttribute("name"));
           // Debug.Log(element.InnerText);
            //BoosCfg booscfg = new BoosCfg();
            //booscfg.ID = int.Parse(element.GetAttribute("ID"));
            //booscfg.Hp = int.Parse(element.GetAttribute("Hp"));
            //booscfg.Name = element.GetAttribute("Name");
            //booscfg.AttackType = element.GetAttribute("AttackType");
            //booscfgDic.Add(booscfg.ID, booscfg);
            T obj = CreateAndSetValue<T>(element);
            //根据键值对进行存储
            dic.Add(int.Parse(element.GetAttribute("id")), obj);
            Debug.Log("1111");
        }
        return dic;
    }
    public T CreateAndSetValue<T>(XmlElement element) where T : class, new()
    {
        //应用反射也可以实例化对象
        T obj = new T();//T obj=Activator.CreateInstance<T>();
        //获取对象类型及旗下字段
        FieldInfo[] fieldInfos = typeof(T).GetFields();
        //遍历旗下字段
        foreach (FieldInfo fieldInfo in fieldInfos)
        {
            //获取当前子节点下各个字段内容值(value)
            string value = element.GetAttribute(fieldInfo.Name);
            ParsePropertyValue<T>(fieldInfo, obj, value);
        }
        return obj;
    }
    public void ParsePropertyValue<T>(FieldInfo fieldInfo, T obj, string valueString) where T : class, new()
    {
        System.Object value = null;
        if (fieldInfo.FieldType.IsEnum)
        {
            value = Enum.Parse(fieldInfo.FieldType, valueString);
        }
        else
        {
            if (fieldInfo.FieldType == typeof(int))
            {
                value = int.Parse(valueString);
            }
            if (fieldInfo.FieldType == typeof(float))
            {
                value = float.Parse(valueString);
            }
            if (fieldInfo.FieldType == typeof(bool))
            {
                value = bool.Parse(valueString);
            }
            if (fieldInfo.FieldType == typeof(string))
            {
                value = valueString;
            }
        }
        fieldInfo.SetValue(obj, value);
    }
   
}
public class StudentArr
{
    public int id;
    public string name;
    public string password;
    public int caozuoscores;
    public int datiscores;
    public int ScoresAll;
}
调用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public enum zuoti
{
    选项一,
    选项二,
    选项三,
    选项四,
    确定,

    退出
}

public class kaohebutton : Interactiable {
    
    public zuoti kaoheanjian;
    public override void Interactive()
    {
       if(kaoheanjian== zuoti.选项一)
        {
            KoheChengjidan.instance.textxuanxiang.text = "A";
        }
        if (kaoheanjian == zuoti.选项二)
        {
            KoheChengjidan.instance.textxuanxiang.text = "B";
        }
        if (kaoheanjian == zuoti.选项三)
        {
            KoheChengjidan.instance.textxuanxiang.text = "C";

        }
      
        if (kaoheanjian == zuoti.确定)
        {         

            if (KoheChengjidan.instance.textxuanxiang.text == KoheChengjidan.instance.duibiabcd)
            {
                Debug.Log("你选对le");
                //正确加分
                KoheChengjidan.instance.scores += 10;
                KoheChengjidan.instance.kaotidefen.text = "10";
                //原因
                KoheChengjidan.instance.reasonDati.sprite = Resources.Load<Sprite>("ScoresReason/选对");



                //激活分数面板
                KoheChengjidan.instance.cancasScores.SetActive(true);
                //将分数打印到Text上
                KoheChengjidan.instance.textscorres.text = KoheChengjidan.instance.scores.ToString();
                
               
                Debug.Log("调用存储");
                this.transform.parent.gameObject.SetActive(false);
            }
            else
            {
                Debug.Log("你没有选对");
                KoheChengjidan.instance.kaotidefen.text = "0";
                //原因
                KoheChengjidan.instance.reasonDati.sprite = Resources.Load<Sprite>("ScoresReason/选错");


                //激活分数面板
                KoheChengjidan.instance.cancasScores.SetActive(true);
                //将分数打印到Text上
                KoheChengjidan.instance.textscorres.text = KoheChengjidan.instance.scores.ToString();

               
                this.transform.parent.gameObject.SetActive(false);
            }
            //存到xml文件里
            ConfigManager.Instance.LoadAllConfig(Player. namestring,Player. passwordstring, KoheChengjidan.instance.caozuodefen, KoheChengjidan.instance.kaotidefen, KoheChengjidan.instance.textscorres);
        }
        if (kaoheanjian == zuoti.退出)
        {
            SceneManager.LoadScene(0);
        }
    }
}

ui_面板提示
using UnityEngine;
using System.Collections;
using System.Xml;
using org.in2bits.MyXls;
using System.IO;
using UnityEngine.UI;
using System.Collections.Generic;

public class ScoresManager : MonoBehaviour {


    //姓名
    public  InputField nameinput;
    //密码
    public  InputField password;

   
  


   //给name 和password复赋值
    public Button amsure;
  
    // Use this for initialization
    void Start()
    {  
        amsure.onClick.AddListener(SaveNamePassword);
    }

    // Update is called once per frame
    void Update()
    {


    }
    public void SaveNamePassword()
    {
        if (nameinput.text != null && password.text != null)
        {
          Player.namestring = nameinput.text;
          Player.passwordstring = password.text;
            amsure.transform.parent.gameObject.SetActive(false);
        }
    }
}

 
 
  <? xml version="1.0" encoding="utf-8" ?>
- < Root >
  < StudentArr id =" 0 " name =" " password =" " caozuoscores =" 80 " datiscores =" 0 " ScoresAll =" 80 " />
  </ Root >





猜你喜欢

转载自blog.csdn.net/fanfan_hongyun/article/details/80424677
今日推荐