Skip to content

Tutorials Asset Engine Asset

Takumii edited this page Feb 11, 2014 · 5 revisions

HomeTutorialsAsset Engine ▸ Asset

Asset

How to load an asset

Assets are loaded from folder.

AssetFolder class has a method called Load. This method works exactly like the Load method of ContentManager.

// With this signature, assetName parameter is also the path of the asset
Load<T>(string assetName, object parameters = null);

Like ContentManager, Load method will creates the asset the first time it is called and returns the same instance for other calls.

// Content project with one asset
- Content
	- Mesh
		- Car.fx

// Example of code
Storage myStorage = assetEngine.CreateStorage("MyStorage");
myStorage.AddFolder("Content"); // folder linked to the root of the content project

AssetFolder rootFolder = myStorage["Content"];
Mesh car = rootFolder.Load<Mesh>("Mesh\Car");
Mesh sameCar = rootFolder.Load<Mesh>("Mesh\Car"); // Same instance as above

The path used to load an asset will depend on the directory used by the folder.

If you load the same asset (same file) from two different folders this will create two different instance.

// Storage from previous example
myStorage.AddFolder("Content\Mesh"); // folder linked to the Mesh folder

AssetFolder rootFolder = myStorage["Content"];
AssetFolder meshFolder = myStorage["Content\Mesh"];

// Load the Car.fx file from two different folder
// The path used depends on the folder path
Mesh car = rootFolder.Load<Mesh>("Mesh\Car");
Mesh anotherCar = meshFolder.Load<Mesh>("Car"); // Different instance than above

How to load an asset with a different key

The Load method has another signature that allows you to load an asset with a name different from his path.

// With this signature, the assetName parameter is the key
Load<T>(string assetName, string path, object parameters = null);

Here is an example :

// Storage and folder used from previous example
Mesh car = rootFolder.Load<Mesh>("MyCar", "Mesh\Car"); // MyCar is the key associated to the asset

// If you want to load the same asset again, you can do :
Mesh sameCar = rootFolder.Load<Mesh>("MyCar"); // The asset is already loaded so only the key is needed

Be careful when you use this version of Load method. You can load the same asset multiple times within the same folder if you use different keys but with the same path.

// Those two calls will create two different instance from the same file
Mesh car = rootFolder.Load<Mesh>("MyCar", "Mesh\Car");
Mesh anotherCar = rootFolder.Load<Mesh>("MyAnotherCar", "Mesh\Car");

How to load an asset with extra parameters

Each version of the Load method has one optional parameters named Parameters of type object.

This can be used to pass extra data when loading an asset. This is only used by asset loader.

You can pass extra parameters this way :

object parameters = new CustomParameters(); // Parameters will depend on the type of asset to load
Mesh car = rootFolder.Load<Mesh>("Mesh\Car", parameters);

// or

object parameters = new CustomParameters();
Mesh car = rootFolder.Load<Mesh>("MyCar", "Mesh\Car", parameters);

How to unload an asset

Asset can be unloaded from a folder :

rootFolder.Unload("MyCar");

How to unload all assets in a storage

All assets within a folder can be unloaded at once :

rootFolder.UnloadAll();