, Sr. Member
In case anyone is interested, here’s the method I came up with. Obviously it’s not perfect nor generic, but that’s for a protoype so I’ll probably come back to this later.
I’d love to hear from other people how they’re doing it, or if I’m “wrong”, or if there’s a better way, or an easier way to do the same thing… etc
package
{
import away3d.events.AssetEvent;
import away3d.events.LoaderEvent;
import away3d.library.AssetLibrary;
import away3d.library.assets.AssetType;
import away3d.loaders.parsers.MD5AnimParser;
import away3d.loaders.parsers.MD5MeshParser;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLRequest;
/**
* ...
* @author Matse
*/
public class ResourceLoader extends EventDispatcher
{
protected var _basePath:String = "assets/";
protected var _resourceList:Vector.<Vector.<String>>;
protected var _resourceCount:int;
protected var _loadCount:int;
public function ResourceLoader()
{
_resourceList = Vector.<Vector.<String>>([
// PLAYER resources
Vector.<String>(["player::mesh", "swar/swar.md5mesh"]),
Vector.<String>(["player::diffuse0", "swar/swar_diffuse.png"]),
Vector.<String>(["player::normal0", "swar/swar_normal.png"]),
Vector.<String>(["player::diffuse1", "swar/weapon_diffuse.png"]),
Vector.<String>(["player::normal1", "swar/weapon_normal.png"]),
Vector.<String>(["player::diffuse2", "swar/hair_diffuse.png"]),
Vector.<String>(["player::normal2", "swar/hair_normal.png"]),
Vector.<String>(["player::idle", "swar/idle.md5anim"]),
//Vector.<String>(["", ""]),
// MONSTER 0 resources
//Vector.<String>(["", ""]),
]);
AssetLibrary.enableParser(MD5AnimParser);
AssetLibrary.enableParser(MD5MeshParser);
_resourceCount = _resourceList.length;
_loadCount = 0;
}
public function startLoading():void
{
AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETE, assetComplete, false, 0, true);
AssetLibrary.addEventListener(LoaderEvent.RESOURCE_COMPLETE, resourceComplete, false, 0, true);
AssetLibrary.addEventListener(LoaderEvent.LOAD_ERROR, loadError, false, 0, true);
var req:URLRequest;
for (var i:int = 0; i < _resourceCount; i++) {
req = new URLRequest(_basePath + _resourceList[i][1]);
AssetLibrary.load(req, null, null, _resourceList[i][0]);
}
}
protected function assetComplete($evt:AssetEvent):void
{
var ns:String = $evt.asset.assetNamespace;
AssetLibrary.removeAsset($evt.asset, false);
var array:Array = ns.split("::");
if ($evt.asset.assetType == AssetType.SKELETON) {
$evt.asset.resetAssetPath("skeleton", array[0]);
} else {
$evt.asset.resetAssetPath(array[1], array[0]);
}
AssetLibrary.addAsset($evt.asset);
}
protected function resourceComplete($evt:LoaderEvent):void
{
_loadCount ++;
if (_loadCount == _resourceCount) {
allDone();
}
}
protected function loadError($evt:LoaderEvent):void
{
}
protected function allDone():void
{
AssetLibrary.removeEventListener(AssetEvent.ASSET_COMPLETE, assetComplete);
AssetLibrary.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, resourceComplete);
AssetLibrary.removeEventListener(LoaderEvent.LOAD_ERROR, loadError);
// clear assets from loading namespaces
for (var i:int = 0; i < _resourceCount; i++) {
AssetLibrary.removeNamespaceAssets(_resourceList[i][0], false);
}
dispatchEvent(new Event(Event.COMPLETE));
}
//________________________________________________________________________________
// GETTERS & SETTERS
public function get loadProgress():int
{
return _loadCount;
}
public function get loadTotal():int
{
return _resourceCount;
}
}
}
edit : the “preview post” feature doesn’t seem to be working
Just wanted to add : using this “method” I can easily retrieve my assets “later” in other scripts, for example :
playerMesh = AssetLibrary.getAsset("mesh", "player");