`
windybell
  • 浏览: 14882 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

JME3资源管理之三:资源加载流程

 
阅读更多
系列目录:
JME3资源管理之一:核心组件介绍
JME3资源管理之二:AssetLoader和AssetLocator
JME3资源管理之三:资源加载流程
JME3资源管理之四:心得和小节

JME3在加载资源的过程中,AssetManager会先根据AssetKey去缓存中查找资源,如果找得到的话当然皆大欢喜,找不到的话才会去读取磁盘。

具体加载的流程是这样的:
1、在缓存中查找资源

检查AssetCache中的资源,若找不到就进行下一步,若找到就直接返回了。
    AssetCache cache = handler.getCache(key.getCacheType());
    Object obj = cache != null ? cache.getFromCache(key) : null;


2、匹配AssetLoader

根据资源后缀名来匹配AssetLoader,找不到的话抛出异常。
    AssetLoader loader = handler.aquireLoader(key);


3、资源定位

遍历所有注册过的AssetLocater,返回AssetInfo,找不到的话会抛出异常。
    AssetInfo info = handler.tryLocate(key);


4、加载资源

调用AssetLoader的load(AssetInfo info)方法,返回资源对象。
    obj = loader.load(info);


5、后续处理

AssetLoader返回Object类型的对象,经过AssetProcessor处理后,转换成实际的对象类型。
比如AWTLoader读取图片数据后,返回Image类型的对象。再通过TextureProcessor处理后才变成Texture对象。
    AssetProcessor proc = handler.getProcessor(key.getProcessorType());
    if (proc != null){
        // do processing on asset before caching
        obj = proc.postProcess(key, obj);
    }


6、保存到缓存

资源加载结束后,对象会保存到缓存中。
    if (cache != null){
        // At this point, obj should be of type T
        cache.addToCache(key, (T) obj);
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics