ResourceManager(一)—— ArchiveManager

本文介绍了一个资源管理系统的设计与实现,该系统通过解析Resource.bytes文件来管理游戏中的各种资源。系统主要包括两个核心类:Archive用于存储单个归档中的文件及其类型;ArchiveManager则负责管理所有的Archive实例,并提供获取资源路径的方法。

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;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值