如上图所示:
炉石根据资源的不同类型进行分别的AssetBundle打包,一类资源对应一个或者多个资源包;(一类资源分多个包的规则不得而知);
有的资源包真的本地化单独打包,例如“fonts0.unity3d”==》“fontszhCN0.unity3d”;
在程序中,资源包的分类对应枚举类型“AssetFamily”;
资源包的具体路径信息,存储在Asset.paths,这是一个静态变量;在初始化时,手动填写必要的信息,类似这样:
- Dictionary<AssetFamily, AssetFamilyPathInfo> dictionary = new Dictionary<AssetFamily, AssetFamilyPathInfo>();
- AssetFamilyPathInfo info = new AssetFamilyPathInfo {
- format = Data/Actors/{0}.unity3d,
- sourceDir = Assets/Game/Actors
- };
- info.exts = new string[] { prefab };
- dictionary.Add(AssetFamily.Actor, info);
class AssetLoader :资源加载
游戏运行时需要加载各种资源,基本上都是通过AssetLoader(也有个别情况适用了Resources.Load())。接下来我们就重点看一下AssetLoader的实现思路。
AssetLoader对上层提供资源对象加载接口,对于每种类型的资源都提供一组函数,例如LoadCardPrefab,LoadActor等等。对于对象加载完成、加载进度等提供回调函数。这些函数只是一些简单的包装,其内部都调用到LoadCachedGameObject()或LoadCachedObject()这两个核心函数。
从这两个函数的流程可以看到,资源加载使用到了Cache机制:
首先从AssetCache中查找,如果找到了,则更新Cache项的时间戳,并调用回调;
如果没有找到,则向AssetCache添加一个Request,然后启动Coroutine:CreateCachedAsset(),它的调用步骤是:
- 调用AssetCache.StartLoading();
- 启动Coroutine:CreateCachedAsset_FromBundle<RequestType>():
- 使用AssetLoader.GetBundleForAsset()找到资源所属的AssetBundle;
- 调用AssetBundle.LoadAsync()来真正加载资源;
- 在加载的过程中,根据处理的结果调用:AssetCache.CacheRequest的OnLoadFailed()、OnLoadSucceeded()、OnProgressUpdate()等函数;
- 在AssetCache查找此资源,如果找到了,则加载成功,调用回调函数;
- 调用AssetCache.StopLoading();
我们都知道在开发过程中,不能使用AssetBundle(每次启动都要打包,肯定收不了)。怀疑它的Editor模式相关的代码是用预编译宏处理来实现的,所以未出现在发布出来的程序集当中,类似这样:
- #if UNITY_EDITOR
- Obj = Resources.LoadAssetAtPath(assetPath, typeof(T));
- if (Obj == null)
- Debug.LogError (Asset not found at path: + assetPath);
- yield break;
- #else
- yield break;
- Debug.LogError (Asset not found at path: + assetPath);
- if (Obj == null)
- Obj = Resources.LoadAssetAtPath(assetPath, typeof(T));
前面我们已经讲到:
- AssetCache中的资源项的时间戳,由AssetLoader在资源加载请求时维护;
- AssetCache主要负责管理Cache数据,而真正的资源加载动作还是在AssetLoader中执行;
- AssetCache的资源淘汰主要由外部的各个模块根据自己认为需要的时机去调用,例如:
- SceneMgr.ClearCachesAndFreeMemory()
- LoadingScreen.ClearAssets()
- SoundMgr.UnloadSoundBundle()
- 等等
另外,程序启动时会自动更新资源包(在Login.OnAssetsVersion()中启动),主要是通过UpdateManager和Downloader两个类来处理。
OK,总结一下炉石的资源管理机制:
对游戏资源按照类型分包,每一类资源包可以有多个;
在游戏运行时使用Cache机制。
相关阅读:《炉石传说》架构设计分析:启动流程与Scene管理
锐亚教育,游戏开发论坛|游戏制作人|游戏策划|游戏开发|独立游戏|游戏产业|游戏研发|游戏运营| unity|unity3d|unity3d官网|unity3d 教程|金融帝国3|8k8k8k|mcafee8.5i|游戏蛮牛|蛮牛 unity|蛮牛
- SoundMgr.UnloadSoundBundle()
- LoadingScreen.ClearAssets()
- SceneMgr.ClearCachesAndFreeMemory()
- AssetCache的资源淘汰主要由外部的各个模块根据自己认为需要的时机去调用,例如:
- AssetCache主要负责管理Cache数据,而真正的资源加载动作还是在AssetLoader中执行;
- 在AssetCache查找此资源,如果找到了,则加载成功,调用回调函数;
- 在加载的过程中,根据处理的结果调用:AssetCache.CacheRequest的OnLoadFailed()、OnLoadSucceeded()、OnProgressUpdate()等函数;
- 调用AssetBundle.LoadAsync()来真正加载资源;
- 使用AssetLoader.GetBundleForAsset()找到资源所属的AssetBundle;
- 启动Coroutine:CreateCachedAsset_FromBundle<RequestType>():
- 还没有人评论,欢迎说说您的想法!