XNAssets is MonoGame/FNA asset management library that - unlike MonoGame Content Pipeline - loads raw assets.
There are two ways of referencing XNAssets in the project:
-
Through nuget(works only for MonoGame): https://www.nuget.org/packages/xnassets
-
As repo(works for both MonoGame and FNA):
a. Clone this repo.
b. Execute
git submodule update --init --recursive
within the folder the repo was cloned to.c. Add src/XNAssets.MonoGame.csproj or src/XNAssets.FNA.csproj to the solution.
In order to create AssetManager two parameters must be passed to its constructor: GraphicsDevice and IAssetResolver. Latter is simple interface that opens asset stream by its name.
XNAAssets provides 3 implementation of IAssetResolver:
- FileAssetResolver that opens Stream using File.OpenRead. Sample AssetManager creation code:
FileAssetResolver assetResolver = new FileAssetResolver(Path.Combine(PathUtils.ExecutingAssemblyDirectory, "Assets"));
AssetManager assetManager = new AssetManager(GraphicsDevice, assetResolver);
- ResourceAssetResolver that opens Stream using Assembly.GetManifestResourceStream. Sample AssetManager creation code:
ResourceAssetResolver assetResolver = new ResourceAssetResolver(typeof(MyGame).Assembly, "Resources.");
AssetManager assetManager = new AssetManager(GraphicsDevice, assetResolver);
- TitleContainerAssetResolver that opens Stream using TitleContainer.OpenStream. Sample AssetManager creation code:
TitleContainerAssetResolver assetResolver = new TitleContainerAssetResolver("Assets");
AssetManager assetManager = new AssetManager(GraphicsDevice, assetResolver);
After AssetManager is created, it could be used following way to load SpriteFont:
SpriteFont font = assetManager.Load<SpriteFont>("fonts/arial64.fnt");
Or following way to load Texture2D:
Texture2D texture = assetManager.Load<Texture2D>("images/LogoOnly_64px.png");
XNAssets allows to load following asset types out of the box:
Type | AssetLoader Type | Description |
---|---|---|
Texture2D | Texture2DLoader | Texture in BMP, TGA, PNG, JPG, GIF or PSD format. Alpha is being premultiplied after the loading |
SpriteFont | SpriteFontLoader | Font in AngelCode's BMFont .fnt format |
string | StringLoader | Loads any resource as string |
SoundEffect | SoundEffectLoader | SoundEffect in WAV format |
It is possible to make XNAssets use custom asset loaders by marking custom types with attribute AssetLoaderAttribute.
I.e. following code makes it so UserProfile class will be loaded by UserProfileLoader:
[AssetLoader(typeof(UserProfileLoader))]
public class UserProfile
{
public string Name;
public int Score;
}
Now let's say that we store user profiles as xml files that look like following:
<?xml version="1.0" encoding="utf-8" ?>
<UserProfile>
<Name>XNAssets</Name>
<Score>10000</Score>
</UserProfile>
Then UserProfileLoader class should look like this:
internal class UserProfileLoader : IAssetLoader<UserProfile>
{
public UserProfile Load(AssetLoaderContext context, string assetName)
{
var data = context.Load<string>(assetName);
var xDoc = XDocument.Parse(data);
var result = new UserProfile
{
Name = xDoc.Root.Element("Name").Value,
Score = int.Parse(xDoc.Root.Element("Score").Value)
};
return result;
}
}
Now it should be possible to load user profile with following code:
UserProfile userProfile = assetManager.Load<UserProfile>("profile.xml");