Skip to content

Asset Management

Julien Pires edited this page Aug 2, 2013 · 5 revisions

HomeModules overview ▸ Asset Management

Introduction

Asset module provides tools to manage resources during the lifecycle of the game. Load, store, find and unload are common task provided by the module. The module make use of the ContentManager of XNA to keep control on the resources loaded.

How it works

Asset

An asset can be anything that need to load resources. This can be a mesh, a texture or a sound. In general an asset need to load resources with the XNA ContentManager. A base class is provided for all asset:

public abstract class Asset
{
...
}

All asset have to inherit this class to be managed by the asset system. The engine implements different kind of asset but the user can defines its own asset.

Asset storage

An asset storage can be seen as a container. An storage can contains any asset type and work with it. You can find a loaded asset in these storage. Each storage has its own instance of ContentManager. This means you can have a better control over loaded resources and thus on the memory used.

For example, you can use one storage per level. During the loading screen you load all the asset in the storage, you use them during the level and when it's over you unload the storage. By unloading the storage all resources loaded by the associated ContentManager will be unloaded too. This avoid to use one instance of ContentManager during the entire lifetime of the game and accumulate resources in memory. With a storage you don't have to wait until the game stops to unload all.

AssetStorageManager class is the entry point to create and delete storage. For example, to create a storage you have to do:

AssetStorage storage = AssetStorageManager.Instance.CreateStorage("StorageName", "ContentPath");

StorageName is the name of the storage, it have to be unique. ContentPath is the path of the content project.

Destroying a storage is as easy as creating one:

AssetStorageManager.Instance.CreateStorage("StorageName");

The storage will be destroy and loaded resources. Be careful, once the storage is destroy using asset linked to it can cause exception.

AssetStorageManager has a special asset storage called SystemStorage. This storage lives during all the lifetime of the game. It can be used to load and keep in memory special or redundant asset. For example, in your game you have a default texture in case of missing texture or a menu which can appears anywhere and anytime. This avoid to load the same resources many times if it's often used.

AssetManager

To load and unload asset in a storage you have to do it with an asset manager. An asset manager is class implementing IAssetManager interface. This interface contains only one method:

Asset CreateInstance(string name, object parameter);

The first argument is the name of the asset and the second an array of optional parameters.

All asset manager have to use AssetGroup. An asset group is a kind of container for a specific type of asset. For example, a group of mesh or a group of texture. It manages all asset of a type regardless in which storage they are. The asset group is responsible for adding an asset in a storage or removing it, you can't add asset directly in a storage. In fact when you want to load an asset you don't have to call directly asset group load method. It's the responsibility of the asset manager to provides various load method.

For example, the MaterialManager implements IAssetManager and provides three methods to load a material.

public Material LoadWithTexture(string name, string storage, Texture diffuse, Texture normalMap, Texture specular)
public Material LoadFromFile(string name, string storage, string diffuseMap, string normalMap, string specularMap)
public Material LoadEmptyMaterial(string name, string storage)

Each methods ask at least for a name and the storage in which to store the material. Its in inside these methods that load method of AssetGroup will be called.