夏天的风

将来的你一定会感激现在努力的自己

《组合变身小宠物游戏》ConfigManager(修改更新中)【初学者】

using UnityEngine;
using Mono.Xml;
using System.Security;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Reflection;
using ZPStudio;

public class ConfigManager : MonoBehaviour
{

    private static ConfigManager mInstance = null;
 
    public Dictionary<int, PetXmlData> petXmlDataDict = new Dictionary<int, PetXmlData>();
    public List<int> petIdList = new List<int>();

    public Dictionary<int, PetCombineXmlData> PetCombineXmlDataDict = new Dictionary<int, PetCombineXmlData>();
    public static ConfigManager Instance
    {
        get
        {
            return mInstance;
        }
    }
    //*************
    public int GetpetXmlDictCount()//获取长度
    {
        return petXmlDataDict.Count;
    }

    public PetXmlData GetPetXMLInfo(int DictOrder)
    {
        return petXmlDataDict[DictOrder];
    }

    public PetXmlData GetPetData(int id)//根据id获得宠物数据信息
    {
        PetXmlData tmp = null;
        petXmlDataDict.TryGetValue(id, out tmp);
        return tmp;
    }
    //*******
    public int GetPetCombineDicCount()
    {
        return PetCombineXmlDataDict.Count;
    }

    public PetCombineXmlData GetPetCombineDicData(int id1, int id2)//根据id1和id2确定某条数据(之后获取它的combineId)
    {
        
        for (int i = 0; i < PetCombineXmlDataDict.Count; i++)
        {
            PetCombineXmlData tmp = null;
            PetCombineXmlDataDict.TryGetValue(i, out tmp);
            if (tmp != null &&tmp.id1 == id1 && tmp.id2 == id2)
                return tmp;
        }
        return null;
    }

    //*************
    public bool IsLoaded
    {
        get;
        private set;
    }

    void Awake()
    {
        if (Instance != null)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
            mInstance = this;
            mInstance.InitGlobal();
            mInstance.StartCoroutine(InitConfig());
        }
    }


    void InitGlobal()
    {
        DataRecord.Load();

    }

    IEnumerator InitConfig()
    {
        IsLoaded = false;

        petXmlDataDict.Clear();
        PetCombineXmlDataDict.Clear();
        yield return StartCoroutine(ReadXmlData<int, PetXmlData>("Pet", "id", petXmlDataDict));
        yield return StartCoroutine(ReadXmlData<int, PetCombineXmlData>("PetCombine", "id", PetCombineXmlDataDict));
        IsLoaded = true;
        //UIManager.instance.ShowPanel(UIPanelNames.PetIllustrationsPanel);
    }

    public IEnumerator ReadXmlData<TKEY, TRECORD>(string filename, string keyName, Dictionary<TKEY, TRECORD> dictRecord) where TRECORD : new()
    {
        string path = Application.streamingAssetsPath + "/" + filename + ".xml";
        WWW www = null;
        if (Application.platform == RuntimePlatform.Android)
        {
            www = new WWW(path);
        }
        else
        {
            www = new WWW("file://" + path);
        }
        yield return www;

        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(string.Format("Error Message : {0}", www.error));
            yield break;
        }

        XmlReader.ReadXmlData<TKEY, TRECORD>(www.text, keyName, dictRecord);
    }

}

阅读更多
版权声明:本文为博主“等木鱼的猫”原创文章,转载请注明出处 http://blog.csdn.net/u012761191 https://blog.csdn.net/u012761191/article/details/52353617
个人分类: unity
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭