Unity加载XML管理

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

public class ParseXMLManager : MonoBehaviour
{
    private static ParseXMLManager instance = null;
    private List<BaseParseXML> list = new List<BaseParseXML> ();

    public delegate void FinishCallBack ();

    public FinishCallBack finishCallBack;
    public bool allFinish = false;

    void Awake ()
    {

        instance = this;

        list.Add (new NewPropParseXML ());
        list.Add (new LoadingTipsParseXml ());
        list.Add (new TaskParseXml ());
        list.Add (new NewSkillParseXml ());
        list.Add (new LandParseXML ());
        list.Add (new MonsterParseXML ());
        list.Add (new ClientConstantConfigParseXML ());
        list.Add (new BuyCornParseXML ());
        list.Add (new BuyDiamondParseXML ());
        list.Add (new NewLandParseXML ());
        list.Add (new RandomLandParseXML ());
        list.Add (new RoleGrowUpParseXML ());
        list.Add (new CarGrowUpParseXML ());
        list.Add (new ObstacleParseXML ());
        list.Add (new CellectParseXML ());
        list.Add (new EnemyParseXML ());
        list.Add (new SkillEffectParseXml ());
        list.Add (new SkillParseXml ());
        list.Add (new AchievementParseXML ());
        list.Add (new PetParseXml ());
        list.Add (new RewardShowParseXML ());
        list.Add (new RewardCostParseXML ());
        list.Add (new RewardDropParseXML ());
        list.Add (new FirstTopUpGiftBagParseXML ());
        list.Add (new TopupGiftBagParseXML ());
        list.Add (new UpgradeGiftBagParseXML ());
        list.Add (new GameGiftParseXML ());
        list.Add (new SignInParseXML ());
        list.Add (new BossConfigParseXML ());
        list.Add (new GreatGiftbBagParseXML ());


//      int landConfigCount = LevelConfigParseXML.GetLevelCount (0);//在XML中,第一行数据表示,当前有多少个XML要解析
        int singleLevelXmlCount = 2;
        //Lv1(场景类型)_1(关卡等级)_1(关卡数量)、Lv1_1_2
        int landConfigCount = 5;
//      for (int i = 0; i < landConfigCount; i++) {
        for (int i = 0; i < landConfigCount; i++) {
//          singleLevelXmlCount = LevelConfigParseXML.GetLevelCount (i + 1);
//          singleLevelXmlCount = i + 1;
            singleLevelXmlCount = 2;
            UserInfos.GetInstance ().roadType_Count.Add (i + 1, singleLevelXmlCount);
            for (int j = 0; j < singleLevelXmlCount; j++) {
//          for (int j = 0; j < 2; j++) {
                list.Add (new LandConfigParseXML (string.Format ("{0}_{1}_{2}", 1, i + 1, j + 1)));
//              GLogger.LogError (string.Format ("{0}_{1}_{2}", 1, i + 1, j + 1));
            }
        }


        //Lv1(场景类型)_1(关卡等级)_1(关卡数量)、Lv1_1_2
        for (int i = 2; i <= 3; i++) {
            for (int j = 1; j <= 5; j++) {
                for (int k = 1; k <= 2; k++) {
                    list.Add (new LandConfigParseXML (string.Format ("{0}_{1}_{2}", i, j, k)));
//                  GLogger.LogError (string.Format ("{0}_{1}_{2}", i, j, k));
                }
            }
        }

        list.Add (new LandConfigParseXML (string.Format ("{0}_{1}", 100, 1)));
        list.Add (new LandConfigParseXML (string.Format ("{0}_{1}", 200, 1)));

    }

    public static ParseXMLManager GetInstance ()
    {
        return instance;
    }

    void Update ()
    {
        if (allFinish) {
            return;
        }
        int num = 0;
        for (int i = 0; i < list.Count; i++) {
            if (list [i].isFinish) {
                num++;
            }
        }

        if (num == list.Count) {
            allFinish = true;

            if (finishCallBack != null) {
                finishCallBack ();
            }
        }
    }
    //注册所有xml解析器
    void Start ()
    {

    }
    //开始解析xml
    public void StartParseXML ()
    {
        allFinish = false;
        Debug.LogError ("==============解析XML数量:" + list.Count);
        for (int i = 0; i < list.Count; i++) {
            StartCoroutine (list [i].DownAndParseXml ());
        }
    }
}




using System;
using UnityEngine;
using System.IO;
using System.Collections;


public class BaseParseXML
{
    protected string configName = "";

    public bool isFinish = false;


    public virtual void parseXML(WWW www){

    }

    public IEnumerator DownAndParseXml(){
        if (configName != "") {
            string loadUrl = null;
            if (ResourcesUpdate.GetInstance ().isServer) {
                loadUrl = GameUtils.GetDocumentsPath () + "/Config/" + configName;
            } else {
                loadUrl = GameUtils.GetDocumentsPath () + "/Config/" + configName;
//              loadUrl = GameUtils.GetAssetPath() + "/Config/" + configName;
            }
            WWW www = new WWW(loadUrl);
            yield return www;
            if (www.isDone) {
                parseXML (www);
//              Debug.LogError (www.text);
                isFinish = true;
            }
        }
    }
}


using UnityEngine;
using System.Collections;
using System.IO;
using System.Xml;
using System.Linq;
using System.Text;
using System.Collections.Generic;

/// <summary>
/// 成就描述信息XML配置解析
/// </summary>
public class AchievementParseXML : BaseParseXML
{
    public static Dictionary<int,AchievementEntity> achievementDict = new Dictionary<int, AchievementEntity> ();
    public static Dictionary<int,List<AchievementEntity>> achievementTypeDict = new Dictionary<int,List<AchievementEntity>> ();

    public AchievementParseXML ()
    {
        configName = "achievementConfig.xml";
    }

    public override void parseXML (WWW www)
    {
        XmlDocument doc = new XmlDocument (); 
        doc.LoadXml (www.text);
        XmlNode items = doc.SelectSingleNode ("root");
        foreach (XmlNode item in items) {
            XmlElement _item = (XmlElement)item;

            AchievementEntity achievementEntity = new AchievementEntity ();

            achievementEntity.achievementId = int.Parse (_item.GetAttribute ("achievementId"));
            achievementEntity.achievementDescription = _item.GetAttribute ("achievementDescription");
            achievementEntity.achievementIcon = _item.GetAttribute ("achievementIcon");
            achievementEntity.achievementType = int.Parse (_item.GetAttribute ("achievementType"));
            achievementEntity.achievementStatistics = int.Parse(_item.GetAttribute ("achievementStatistics"));
            achievementEntity.achievementConditionId = int.Parse(_item.GetAttribute ("achievementConditionId"));
            achievementEntity.achievementValue = int.Parse (_item.GetAttribute ("achievementValue"));
            achievementEntity.achievementRewardType = int.Parse (_item.GetAttribute ("achievementRewardType"));
            achievementEntity.achievementRewardPropId = int.Parse (_item.GetAttribute ("achievementRewardPropId"));
            achievementEntity.achievementRewardCount = int.Parse (_item.GetAttribute ("achievementRewardCount"));

            achievementDict.Add (achievementEntity.achievementId, achievementEntity);

//          if (!achievementTypeDict.ContainsKey (achievementEntity.type)) {
//          achievementDict.Add (achievementEntity.type, new List<AchievementEntity> ());
//          }

//          achievementTypeDict [achievementEntity.type].Add (achievementEntity);

        }
    }

    public static AchievementEntity GetAchievementEntityById (int _id)
    {
        if (achievementDict.ContainsKey (_id)) {
            return achievementDict [_id];
        }
        return new AchievementEntity ();
    }
}



using UnityEngine;
using System.Collections;

/// <summary>
/// 成就描述实体类
/// </summary>
/// 
public class AchievementEntity
{
    public int achievementId{ set; get; }

    public string achievementDescription{ set; get; }

    public string achievementIcon{ set; get; }

    public int achievementType{ set; get; }

    public int achievementStatistics{ set; get; }

    public int achievementConditionId{ set; get; }

    public int achievementValue{ set; get; }

    public int achievementRewardType{ set; get; }

    public int achievementRewardPropId{ set; get; }

    public int achievementRewardCount{ set; get; }
}


<?xml version="1.0" encoding="UTF-8"?>
<root>
  <item achievementId="101" achievementDescription="拥有2名角色" achievementIcon="Collection" achievementType="1" achievementStatistics="2" achievementConditionId="1" achievementValue="2" achievementRewardType="2" achievementRewardPropId="2" achievementRewardCount="10" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值