ResourceManager(一)—— ArchiveManager

来源:互联网 发布:冰锐和锐澳的区别 知乎 编辑:程序博客网 时间:2024/05/21 23:31

ResourceManager这部分要联系前面的BuildAsset三篇一起看才行

文件目录:Assets/Scripts/ResourceManager/

Archive.cs

using System;using System.Collections;using System.Collections.Generic;using UnityEngine;public class Archive{    private Dictionary<string, string> mAllFiles;   //name-type    //构造函数    public Archive()    {        mAllFiles = new Dictionary<string, string> ();    }    public Dictionary<string, string> AllFiles    {        get        {            return mAllFiles;        }    }    public void Add(string fileName, string type)    {        mAllFiles.Add (fileName, type);    }    public string getPath(string fileName)    {        if (mAllFiles.ContainsKey(fileName))        {            return fileName + "." + mAllFiles [fileName];        }        else        {            Debug.LogError ("can not find" + fileName, ResourceCommon.DEBUGTYPENAME);        }        return null;    }}

ArchiveManager.cs

using System;using System.IO;using System.Xml;using System.Collections;using System.Collections.Generic;using UnityEngine;//继承单例//这个类主要就是读取Resource.bytes这个文件,//然后把信息保存起来public class ArchiveManager: Singleton<ArchiveManager>{    //所有Archive,每一个Archive里面又可以包含多个文件    internal Dictionary<string, Archive> mAllArchives;    //构造函数    public ArchiveManager()    {        mAllArchives = new Dictionary<string, Archive> ();    }    //初始化    public void Init()    {        //读取Resource.bytes        StreamReader sr = ResourcesManager.OpenText ("Resource");        XmlDocument doc = new XmlDocument ();        doc.LoadXml (sr.ReadToEnd ());        XmlElement root = doc.DocumentElement;        IEnumerator iter = root.GetEnumerator ();        //遍历Resource信息        while (iter.MoveNext())        {            XmlElement child_root = iter.Current as XmlElement;            IEnumerator child_iter = child_root.GetEnumerator ();            //没包含就加进去            if (!mAllArchives.ContainsKey (child_root.Name))            {                Archive arh = new Archive ();                mAllArchives.Add (child_root.Name, arh);            }            while (child_iter.MoveNext())            {                XmlElement file = child_iter.Current as XmlElement;                string name = file.GetAttribute ("name");                string type = file.GetAttribute ("type");                mAllArchives [child_root.Name].Add (name, type);            }        }        sr.Close ();    }    public string GetPath(string archiveName, string fileName)    {        if (mAllArchives.ContainsKey(archiveName))        {            return mAllArchives [archiveName].getPath (fileName);        }        else        {            Debug.LogError ("can not find" + archiveName, ResourceCommon.DEBUGTYPENAME);        }        return null;    }}
阅读全文
0 0
原创粉丝点击