【XML serializer_Deserialize|C#】使用S+D加载已有的xml文件(关于封装自定义类的数组和字典)SerizlizerDictionary

自定义类:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using UnityEngine;

public enum StateRange
{
    StatePic,
    VideoPlaying,
    SceneLoad,
}

public class Daji
{
    public float daJiWaitForTime = 5.0f;
    public int stateRangeInt = 0;
    public Daji()
    {

    }

    public Daji(float daJiWaitForTime,StateRange stateRange)
    {
        this.daJiWaitForTime = daJiWaitForTime;
        switch (stateRange)
        {
            case StateRange.StatePic:
                this.stateRangeInt = 0;
                break;
            case StateRange.VideoPlaying:
                this.stateRangeInt = 1;
                break;
            case StateRange.SceneLoad:
                this.stateRangeInt = 2;
                break;
        }
    }
}

public class GuoChang
{
    public float GuoChangtForTime = 5.0f;
    public int stateRangeInt = 0;
    public int isOwnHaveInt = 0;

    public GuoChang()
    {
    }

    public GuoChang(float GuoChangtForTime, StateRange stateRange, bool isOwnHave)
    {
        this.GuoChangtForTime = GuoChangtForTime;
        switch (stateRange)
        {
            case StateRange.StatePic:
                this.stateRangeInt = 0;
                break;
            case StateRange.VideoPlaying:
                this.stateRangeInt = 1;
                break;
            case StateRange.SceneLoad:
                this.stateRangeInt = 2;
                break;
        }
        switch (isOwnHave)
        {
            case false:
                this.isOwnHaveInt = 0;
                break;
            case true:
                this.isOwnHaveInt = 1;
                break;
        }
    }
}

public class XunHuan/*: IXmlSerializable*/
{
    public float XunHuantForTime = 5.0f;
    public int stateRangeInt = 0;

    public XunHuan()
    {
    }

    public XunHuan(float XunHuantForTime, StateRange stateRange)
    {
        this.XunHuantForTime = XunHuantForTime;
        switch (stateRange)
        {
            case StateRange.StatePic:
                this.stateRangeInt = 0;
                break;
            case StateRange.VideoPlaying:
                this.stateRangeInt = 1;
                break;
            case StateRange.SceneLoad:
                this.stateRangeInt = 2;
                break;
        }
      
    }

   /* public XmlSchema GetSchema()
    {

        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        //throw new System.NotImplementedException();
        reader.Read();//读到XunHuantForTime结点
        reader.Read();//读到XunHuantForTime结点包裹的内容
        this.XunHuantForTime = float.Parse(reader.Value);
        reader.Read();读到XunHuantForTime尾部结点
        reader.Read();
        reader.Read();
        this.stateRangeInt = int.Parse(reader.Value);

    }

    public void WriteXml(XmlWriter writer)
    {
        // throw new System.NotImplementedException();
        writer.WriteElementString("XunHuantForTime", this.XunHuantForTime.ToString());
        writer.WriteElementString("stateRangeInt", this.stateRangeInt.ToString());
    }*/
}


public class PlanJiHua3
{
    public int version ;
    public Daji daji ;

    public List<GuoChang> guoChangList ;
    public SerizlizerDictionary<int, XunHuan> xunHuanDic ;
}

S后结果:

 

测试结果 S+D:

XMLSDtes

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class XMLSDtest : MonoBehaviour
{
    private void Awake()
    {

        PlanJiHua3 planJiHua = new PlanJiHua3();
        planJiHua.version = 1;
        planJiHua.daji = new Daji();
        planJiHua.guoChangList = new List<GuoChang>(2) { new GuoChang(), new GuoChang() };
        planJiHua.xunHuanDic = new SerizlizerDictionary<int, XunHuan>() { { 1, new XunHuan() }, { 2, new XunHuan() } };
        XmlDataMgr.Instance.SaveData(planJiHua, "PlanJiHua3");
     
    }
    // Start is called before the first frame update
    void Start()
    {


        PlanJiHua3 planJiHua3 = XmlDataMgr.Instance.LoadData(typeof(PlanJiHua3), "PlanJiHua3") as PlanJiHua3;
        Debug.Log("planJiHua3.daji.daJiWaitForTime" + GameDataMgr.Instance.planJiHua3.daji.daJiWaitForTime);
        Debug.Log("planJiHua3.daji.daJiWaitForTime" + GameDataMgr.Instance.planJiHua3.xunHuanDic[1].XunHuantForTime);


        GameDataMgr.Instance.SetPlanJiHua3(planJiHua3);
        Debug.Log("planJiHua3.daji.daJiWaitForTime++++++" + GameDataMgr.Instance.planJiHua3.daji.daJiWaitForTime);
        Debug.Log("planJiHua3.daji.daJiWaitForTime++++++" + GameDataMgr.Instance.planJiHua3.xunHuanDic[1].XunHuantForTime);
    }

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值