Class ContentManager
The ContentManager is a run-time component which loads managed objects from .xnb binary files produced by the design time MonoGame Content Builder. It also manages the lifespan of the loaded objects, disposing the content manager will also dispose any assets which are themselves IDisposable.
public class ContentManager : IDisposable
- Inheritance
-
ContentManager
- Implements
- Derived
- Inherited Members
Constructors
ContentManager(IServiceProvider)
Initializes a new instance of the ContentMangaer.
public ContentManager(IServiceProvider serviceProvider)
Parameters
serviceProvider
IServiceProviderThe service provider that the ContentManager should use to locate services.
Remarks
By default, the ContentMangaer searches for content in the directory where the executable is located.
When creating a new ContentManager, if no instance of Game is otherwise required by the application, it is often better to create a new class that implements the IServiceProvider interface rather than creating an instance of Game just to create a new instance of GraphicsDeviceManager.
Exceptions
- ArgumentNullException
The
serviceProvider
parameter is null.
ContentManager(IServiceProvider, string)
Initializes a new instance of the ContentMangaer.
public ContentManager(IServiceProvider serviceProvider, string rootDirectory)
Parameters
serviceProvider
IServiceProviderThe service provider that the ContentManager should use to locate services.
rootDirectory
stringThe root directory the ContentManager will search for content in.
Remarks
By default, the ContentMangaer searches for content in the directory where the executable is located.
When creating a new ContentManager, if no instance of Game is otherwise required by the application, it is often better to create a new class that implements the IServiceProvider interface rather than creating an instance of Game just to create a new instance of GraphicsDeviceManager.
Exceptions
- ArgumentNullException
The
serviceProvider
parameter is null.
Properties
LoadedAssets
protected virtual Dictionary<string, object> LoadedAssets { get; }
Property Value
RootDirectory
Gets or Sets the root directory that this ContentManager will search for assets in.
public string RootDirectory { get; set; }
Property Value
ServiceProvider
Gets the service provider instance used by this ContentManager.
public IServiceProvider ServiceProvider { get; }
Property Value
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
protected virtual void Dispose(bool disposing)
Parameters
disposing
booltrue to release both managed and unmanaged resources; false to release only unmanaged resources.
~ContentManager()
protected ~ContentManager()
LoadLocalized<T>(string)
Loads an asset that has been processed by the Content Pipeline.
public virtual T LoadLocalized<T>(string assetName)
Parameters
assetName
stringThe asset name, relative to the ContentManager.RootDirectory, and not including the .xnb extension.
Returns
- T
The loaded asset. Repeated calls to load the same asset will return the same object instance.
Type Parameters
T
The type of asset to load.
Effect, Model, SoundEffect, Song, SpriteFont, Texture, Texture2D, and TextureCube are all supported by default by the standard Content Pipeline processor, but additional types may be loaded by extending the processor.
Remarks
This method attempts to load the asset based on the CurrentCulture searching for the asset by name and appending it with with the culture name (e.g. "assetName.en-US") or two letter ISO language name (e.g. "assetName.en"). If unsuccessful in finding the asset with the culture information appended, it will fall back to loading the default asset.
Before a ContentManager can load an asset, you need to add the asset to your game project using the steps described in Adding Content - MonoGame.
Exceptions
- ArgumentNullException
The
assetName
parameter is null or an empty string.- ObjectDisposedException
This was called after the ContentManger was disposed.
- ContentLoadException
The type of the
assetName
in the file does not match the type of asset requested as specified byT
.-or-
A content file matching the
assetName
parameter could not be found.-or-
The specified path in the
assetName
parameter is invalid (for example, a directory in the path does not exist).-or-
An error occurred while opening the content file.
Load<T>(string)
Loads an asset that has been processed by the Content Pipeline.
public virtual T Load<T>(string assetName)
Parameters
assetName
stringThe asset name, relative to the ContentManager.RootDirectory, and not including the .xnb extension.
Returns
- T
The loaded asset. Repeated calls to load the same asset will return the same object instance.
Type Parameters
T
The type of asset to load.
Effect, Model, SoundEffect, Song, SpriteFont, Texture, Texture2D, and TextureCube are all supported by default by the standard Content Pipeline processor, but additional types may be loaded by extending the processor.
Remarks
Before a ContentManager can load an asset, you need to add the asset to your game project using the steps described in Adding Content - MonoGame.
Exceptions
- ArgumentNullException
The
assetName
parameter is null or an empty string.- ObjectDisposedException
This was called after the ContentManger was disposed.
- ContentLoadException
The type of the
assetName
in the file does not match the type of asset requested as specified byT
.-or-
A content file matching the
assetName
parameter could not be found.-or-
The specified path in the
assetName
parameter is invalid (for example, a directory in the path does not exist).-or-
An error occurred while opening the content file.
OpenStream(string)
protected virtual Stream OpenStream(string assetName)
Parameters
assetName
string
Returns
ReadAsset<T>(string, Action<IDisposable>)
protected T ReadAsset<T>(string assetName, Action<IDisposable> recordDisposableObject)
Parameters
assetName
stringrecordDisposableObject
Action<IDisposable>
Returns
- T
Type Parameters
T
ReloadAsset<T>(string, T)
protected virtual void ReloadAsset<T>(string originalAssetName, T currentAsset)
Parameters
originalAssetName
stringcurrentAsset
T
Type Parameters
T
ReloadGraphicsAssets()
protected virtual void ReloadGraphicsAssets()
Unload()
Unloads all assets that were loaded by this ContentManger.
public virtual void Unload()
Remarks
If an asset being unloaded implements the IDisposable interface, then the IDisposable.Dispose method will be called before unloading.
UnloadAsset(string)
Unloads a single asset that was loaded by this ContentManager.
public virtual void UnloadAsset(string assetName)
Parameters
assetName
stringThe asset name, relative to the ContentManager.RootDirectory, and not including the .xnb extension.
Remarks
If the asset being unloaded implements the IDisposable interface, then the IDisposable.Dispose method will be called before unloading.
Exceptions
- ArgumentNullException
The
assetName
parameter is null or an empty string.- ObjectDisposedException
This was called after the ContentManger was disposed.
UnloadAssets(IList<string>)
Unloads a set of assets loaded by this ContentManager where each element in the provided collection represents the name of an asset to unload.
public virtual void UnloadAssets(IList<string> assetNames)
Parameters
Remarks
If the asset being unloaded implements the IDisposable interface, then the IDisposable.Dispose method will be called before unloading.
Exceptions
- ArgumentNullException
If the
assetNames
parameter is null.-or-
If an element in the collection null or an empty string.
- ObjectDisposedException
This was called after the ContentManger was disposed.