JME3资源管理之一:核心组件介绍
JME3资源管理之二:AssetLoader和AssetLocator
JME3资源管理之三:资源加载流程
JME3资源管理之四:心得和小节
AssetManager加载资源前,首先要注册各种AssetLoader和AssetLocator,否则AssetManager将不知道怎么去加载资源。
1、注册AssetLoader
AssetLoader保存在一个map中,通过后缀名来匹配。如果2个AssetLoader都注册了同样的后缀名,那么 后注册的AssetLoader会挤掉先定义的AssetLoader。
am.registerLoader(AWTLoader.class, "jpg");
2、注册AssetLocator
AssetLocator保存在一个list中。如果2个资源位置中都有同名的资源,那么 先注册的资源会被找到,后面的就忽略了。
am.registerLocator("res", FileLocator.class);//注册程序相对路径res am.registerLocator("C:/", FileLocator.class);// 注册绝对路径 am.registerLocator("/", ClasspathLocator.class);
3、默认注册
使用JME3的应用程序时,默认注册了下面这些AssetLoader和AssetLocater。
LOCATOR / com.jme3.asset.plugins.ClasspathLocator LOADER com.jme3.texture.plugins.AWTLoader : jpg, bmp, gif, png, jpeg LOADER com.jme3.audio.plugins.WAVLoader : wav LOADER com.jme3.audio.plugins.OGGLoader : ogg LOADER com.jme3.cursors.plugins.CursorLoader : ani, cur, ico LOADER com.jme3.material.plugins.J3MLoader : j3m LOADER com.jme3.material.plugins.J3MLoader : j3md LOADER com.jme3.material.plugins.ShaderNodeDefinitionLoader : j3sn LOADER com.jme3.font.plugins.BitmapFontLoader : fnt LOADER com.jme3.texture.plugins.DDSLoader : dds LOADER com.jme3.texture.plugins.PFMLoader : pfm LOADER com.jme3.texture.plugins.HDRLoader : hdr LOADER com.jme3.texture.plugins.TGALoader : tga LOADER com.jme3.export.binary.BinaryImporter : j3o LOADER com.jme3.export.binary.BinaryImporter : j3f LOADER com.jme3.scene.plugins.OBJLoader : obj LOADER com.jme3.scene.plugins.MTLLoader : mtl LOADER com.jme3.scene.plugins.ogre.MeshLoader : meshxml, mesh.xml LOADER com.jme3.scene.plugins.ogre.SkeletonLoader : skeletonxml, skeleton.xml LOADER com.jme3.scene.plugins.ogre.MaterialLoader : material LOADER com.jme3.scene.plugins.ogre.SceneLoader : scene LOADER com.jme3.scene.plugins.blender.BlenderModelLoader : blend LOADER com.jme3.shader.plugins.GLSLLoader : vert, frag, glsl, glsllib
上面的数据来自jme3默认文件: com/jme3/asset/Desktop.cfg
4、AssetConfig
除了在代码里面直接调用AssetManager的方法来注册,我们还可以利用配置文件来进行注册。
配置文件的格式就和上面的代码一样,AssetConfig类专门用于解析这种配置文件。然而实际上我们在编程的时候几乎不上AssetConfig,只要注意配置的方式就行了。我们这里主要谈谈怎么使用配置文件。
配置文件的使用有3个关键点:
(1)配置文件必须放在工程的classpath之下,否则无法识别。
(2)要在AppSettings中添加参数"AssetConfigURL",指定配置文件的加载路径。
(3)如果使用自定义配置文件,那么jme3默认的配置文件就不会生效了!
你可以直接在src文件夹下面创建一个文本文件,文件名无所谓,只要文件格式正确就行。使用方法如下:
public static void main(String[] args) { AppSettings settings = new AppSettings(); settings.set("AssetConfigURL", "org/pstale/asset/Assets.cfg"); SimpleApplication app = new YourApplication(); app.setSettings(settings); app.start(); }
那么,为什么配置文件会有这些限制呢?我们看一看Application类的源代码就知道了。
Application初始化AssetManager的时候,会从AppSettings中读取"AssetConfigURL"这个参数,然后再读取配置文件。
找不到配置文件的话,就会使用默认配置文件。
private void initAssetManager(){ if (settings != null){ String assetCfg = settings.getString("AssetConfigURL"); if (assetCfg != null){ URL url = null; try { url = new URL(assetCfg); } catch (MalformedURLException ex) { } if (url == null) { url = Application.class.getClassLoader().getResource(assetCfg); if (url == null) { logger.log(Level.SEVERE, "Unable to access AssetConfigURL in asset config:{0}", assetCfg); return; } } assetManager = JmeSystem.newAssetManager(url); } } if (assetManager == null){ assetManager = JmeSystem.newAssetManager( Thread.currentThread().getContextClassLoader() .getResource("com/jme3/asset/Desktop.cfg")); } }