Hi Choons,
Are you familiar with iterators in general? This is a very simple (but
powerful ;)) implementation of that pattern. This is how you use it if
you want to loop through all assets that has been loaded:
var asset : IAsset;
var it : AssetLibraryIterator = AssetLibrary.createIterator();
while (asset = it.next()) {
trace(
asset.name);
}
You can also jump back and forth (or use a for loop if you prefer)
using the numAssets property and setIndex() method, and retrieve the
current asset using the currentAsset property:
var i : uint;
for (i=0; i < it.numAsset; i++) {
it.setIndex(i);
trace(
it.currentAsset.name);
}
If you want to limit the iteration to just a particular type, or a
particular namespace, you specify these as parameters in the
createIterator call:
AssetLibrary.createIterator(AssetType.MESH); // Only meshes
AssetLibrary.createIterator(null, "myns"); // All in NS "myns"
AssetLibrary.createIterator(AssetType.MESH, "myns"); // Both
The third example above will only return meshes in namespace "myns",
i.e. no materials, textures or anything else from that namespace, and
no meshes from any other namespace.
If you want to filter using some custom method, use the third
parameter to define a function callback which will be invoked with a
single parameter (the asset) for every asset during the filter stage.
That method must return a boolean indicating whether the particular
asset should be included or not.
function includeIfCar(asset : IAsset) : Boolean
{
if (asset.name.indexOf('car')>=0)
return true;
else
return false;
}
AssetLibrary.createIterator(null,null, includeIfCar);
Regardless of how you invoke createIterator(), the returned object is
always an iterator that you should use the way I described above. Hope
this helps!
Cheers
/R