-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorials Asset Engine Asset Loader
Home ▸ Tutorials ▸ Asset Engine ▸ Asset Loader
An asset loader performs custom load processes for specific asset type.
An asset loader can be useful in some cases :
- Load a custom asset from an existing asset type
- Avoid creating all pipeline reader/writer for a custom asset if data can be loaded with an existing type
- Load a new instance (with new keyword, not from a Stream)
For example, the Mesh class in Pulsar.Graphics has a MeshLoader. This loader can create a new Mesh from a file or not. It uses a loaded Model instance to extract mesh informations and performs additional operations to create the Mesh instance.
Each asset loader is associated to one or more asset type. Whenever one asset of this type must be loaded, the asset loader is used to create the asset instead of the default load behavior.
All asset loader must implements the IAssetLoader interface.
This interface has two methods and two properties :
// This method is called when the asset loader is added to the asset engine
void Initialize(AssetEngine engine);
// This method is called each time an asset of a specific type must be created
// T is the type of asset to load
LoadedAsset Load<T>(string assetName, string path, object parameters, AssetFolder assetFolder);
// Array of asset types associated to the asset loader
Type[] SupportedTypes { get; }
// Name of the asset loader, must be unique
string Name { get; }
Pulsar has an abstract class AssetLoader that implements IAssetLoader and offers some useful functions to load from a file. You can inherit this class if in your process you need to read file.
Once your asset loader is ready, you can add it the asset engine. When a loader is added, it will be used to override the default load behavior.
CustomLoader myCustomLoader = new CustomLoader();
assetEngine.AddLoader(myCustomLoader);
The asset engine will use the Name property of the loader, so make sure that you use an unique name for each loader.
The property SupportedTypes of the IAssetLoader interface is used to associate one or more type to the asset loader.
public class CustomLoader : IAssetLoader
{
private Type[] _supportedTypes = { typeof(CustomAsset) };
public Type[] SupportedTypes
{
get { return _supportedTypes; }
}
}
The signature of the Load method of IAssetLoader is :
LoadedAsset Load<T>(string assetName, string path, object parameters, AssetFolder assetFolder);
As you can see the return type is LoadedAsset. This type is a class that can contains an asset and a list of associated disposable resources.
You must return an instance of this class filled with appropriate data.
public override LoadedAsset Load<T>(string assetName, string path, object parameters, AssetFolder assetFolder)
{
LoadedAsset result = new LoadedAsset();
result.Asset = new CustomAsset();
return result;
}
To load a file from an asset loader, you must inherit the AssetLoader class. This class provides a method to load a file :
protected void LoadFromFile<T>(string path, AssetFolder assetFolder, LoadedAsset result)
The first parameter is the path of the file, the second the folder where the asset will be stored and the third an instance of LoadedAsset that will contains the loaded asset.
The assetFolder parameter is important. If you try to load an asset that has external references, those external references will be loaded through the Load method of the folder. This process will be executed for each external reference.
Once the file is loaded, the result is stored in the LoadedAsset instance that you have passed to the method. This instance will contains only the first loaded asset. This means that all external references will be already loaded and stored in the asset folder (or not, it depends if another asset loader loads them).
LoadedAsset result = new LoadedAsset();
LoadFromFile<Model>("Mesh/MyCar", folder, result);
Model myCar = result.Asset as Model;