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

被折叠的 条评论
为什么被折叠?



