U3D客户端框架(资源管理篇)之可写区资源管理器
2023-09-11 14:22:31 时间
一、可写区资源管理器的作用
顾名思义,可写区资源管理器就是对读写区文件访问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);
}
}
相关文章
- 四、OCR-docker部署最新版PaddleServing和PaddleOCR,服务端,客户端,dockerfile部署-pipeline python部署,GPU运行
- 渗透测试-暴力破解之验证码客户端验证绕过
- 渗透测试-任意文件上传及客户端绕过案例
- SSH客户端 (通过密码连接远程linux主机)
- 【华为联机对战服务】客户端退出重连或中途进入游戏,新玩家如何补帧?
- 大数据学习——hdfs客户端操作
- 服务器端如何判断客户端是不是手机
- netty系列之:手持framecodec神器,创建多路复用http2客户端
- [Oracle]如何获得出现故障时,客户端的详细连接信息
- 曹工杂谈:花了两天时间,写了一个netty实现的http客户端,支持同步转异步和连接池(1)--核心逻辑讲解
- U3D客户端框架之基于类对象池的可回收变量代码实现
- U3D客户端框架(资源管理篇)之资产加载器 AssetLoaderRoutine
- U3D客户端框架之实现基于UnityWebRequest的Http服务 实现HttpCallBackArgs参数类、HttpRoutine访问器、HttpManager管理器
- U3D客户端框架之 拓展StringBuilder实现InsertNoGC、IndexOf、LastIndexOf、ReplaceNoGC、AppendNoGC API 减少GC
- JavaScript hash 与 history 实现客户端路由的原理