U3D客户端框架(资源管理篇)之可写区资源管理器
一、可写区资源管理器的作用
顾名思义,可写区资源管理器就是对读写区文件访问API的封装,对检查可写区域资源的API的封装、资源版本记录作用、可写区资源信息的记录。
Unity中获取可写区的API
Unity中拿到可写区路径的API:Application.persistentDataPath
安卓手机中可写区目录
安卓手机上的可写区域目录一般是:/storage/emulated/0/Android/data/包名/files/ 目录下
二、代码实现
详细的讲解只会挑选出来一些重点的代码来讲一下。其余的函数看全部代码即可。
加载可写区域资源包信息
游戏启动时候,会把可写区的所有assetbundle信息缓存进字典。以便于热更中,加载资时可以取到文件信息避免频繁的文件IO
//加载可写区资源包信息
public Dictionary<string, AssetBundleInfoEntity> GetAssetBundleVersionList(ref string version)
{
//拿到资源版本号
version = PlayerPrefs.GetString(ConstDefine.ResourceVersion);
//把json文件反序列化成文件信息map
string json = IOUtil.GetFileText(LocalVersionFilePath);
return JsonMapper.ToObject<Dictionary<string, AssetBundleInfoEntity>>(json);
}
获取文件字节数组&检查文件存在
public bool CheckFileExists(string path)
{
return File.Exists(string.Format("{0}/{1}",Application.persistentDataPath,path));
}
//获取本地文件字节数组
public byte[] GetFileBuffer(string path)
{
return IOUtil.GetFileBuffer(string.Format("{0}/{1}", Application.persistentDataPath, path));
}
LocalAssetsManager.cs 完整代码
//可写区资源管理器
//LocalAssets资源管理器
public class LocalAssetsManager
{
//本地版本文件路径
public string LocalVersionFilePath
{
get
{
return string.Format("{0}/{1}",Application.persistentDataPath,ConstDefine.VersionFileName);
}
}
//获取可写区版本文件是否存在
public bool GetVersionFileExists()
{
return File.Exists(LocalVersionFilePath);
}
public bool CheckFileExists(string path)
{
return File.Exists(string.Format("{0}/{1}",Application.persistentDataPath,path));
}
//获取本地文件字节数组
public byte[] GetFileBuffer(string path)
{
return IOUtil.GetFileBuffer(string.Format("{0}/{1}", Application.persistentDataPath, path));
}
//保存资源版本号(资源版本,只是单纯的资源版本,0.0.1)
//资源版本号记在了本地配置里,版本文件记在了VersionFile.bytes 版本文件里
public void SetResourceVersion(string version)
{
PlayerPrefs.SetString(ConstDefine.ResourceVersion,version);
}
//保存资源版本文件(filelist,里面记录了所有ab包的文件信息。把文件信息都写入到本地去 VersionFile.bytes)
public void SaveVersionFile(Dictionary<string,AssetBundleInfoEntity>dic)
{
//把map结构映射成json格式的string
string json = JsonMapper.ToJson(dic);
IOUtil.CreateTextFile(LocalVersionFilePath,json);
}
//加载可写区资源包信息
public Dictionary<string, AssetBundleInfoEntity> GetAssetBundleVersionList(ref string version)
{
//拿到资源版本号
version = PlayerPrefs.GetString(ConstDefine.ResourceVersion);
//把json文件反序列化成文件信息map
string json = IOUtil.GetFileText(LocalVersionFilePath);
return JsonMapper.ToObject<Dictionary<string, AssetBundleInfoEntity>>(json);
}
}
