This T4 templete will generate a public static class called ContentRefrence in your project with const string fields whos values are the path string needed to load your content using the ContentManager object.
For Example, if your Content hierarchy is as follows
|- Content
|- Textures
|- player_01.png
|- player_02.png
|- Audio
|- SFX
|- menu click.ogg
|- ding.ogg
The generated ContentReference.cs file would be as follows
public static class ContentReference
{
public static class Textures
{
public const string Player_01 = @"textures/player_01";
public const string Player_02 = @"textures/player_02";
}
public static class Audio
{
public static class SFX
{
public const string Menu_click = @"audio/sfx/menu click";
public const string Ding = @"audio/sfx/ding";
}
}
}
- Clone or Download the repo
- Add the ContentReference.tt file to your project in the Content directory
After adding the ContentReference.tt file to your project, there are a few configurations options you can use to customize the output of the generated class file. To adjust these options, open the ContentRefrence.tt file. Find the section called Generator Settings and adjust the values of the settings.
The following are the default values if you need them for refrence.
// -------------------------------------------------------------
// Generator Settings
// Adjust the settings below. Explinations are provided
// -------------------------------------------------------------
// Set this to true if you want the generated class to be
// encapsulated by a namespace
INCLUDE_NAMESPACE = false;
// If you set the above to true, enter the name of the namespace
// you wish to use.
NAMESPACE = "";
// This determines how Class Names are generated
// CLASS_STYLE.FirstCharUpper = Only the first character of the class name is upper case
// CLASS_STYLE.MatchName = Name is the same casing as the directory name its based on
CLASS_STYLE = ClassStyle.FirstCharUpper;
// This determines how Field Names are generated
// FIELD_STYLE.Proper = Name is camel case
// FIELD_STYLE.MatchName = Name is the same casing as the directory/filename
FIELD_STYLE = FieldStyle.MatchName;
// This determines how to handle spaces in file names
// SpaceStyle.Remove = Removes all spaces
// SpaceStyle.Underscore = Replaces all spaces with _
SPACE_STYLE = SpaceStyle.Remove;
// This determines how to handle file names that match a
// reseved C# keyword
// ReservedStyle.AtSymbol = Places an @ symbole in front of the generated name
// ReservedStyle.Underscore = Places an _ in front of the generated name
RESERVED_STYLE = ReservedStyle.AtSymbol;
// This is the directory where the content tool outputs the .xnb files it builds
// For MonoGame Cross Platform Desktop Project it's "bin\DesktopGl"
CONTENT_DIR = @"bin\DesktopGL";
Be sure to include the semi-colon (;) at the end of each line if you adjust the values.
The table below explains the different options and their values
Option | Description |
---|---|
INCLUDE_NAMESPACE | Set this to |
NAMESPACE | If the |
CLASS_STYLE | Determines how class names are generated. Available values are
|
FILED_STYLE | Determins how filed names are generated. Available values are
|
SPACE_STYLE | Determines how spaces in class and field names are handled. Available values are
|
RESERVED_STYLE | Determins how class and field names are handles if they match a reserved C# keyword. Availalbe values are
|
CONTENT_DIR | This is the directory where the content pipelien tool outputs the .xnb files it builds relative to the Content directory (e.g. For MonoGame Cross Platform Desktop Project the value is |
This template will auto generate a class .cs file called ContentReference.cs.
After adding and building content using the Content Pipeline Tool, right-click the ContentReference.tt file in the Solution Explorer window and select "Run Custom Tool". This will refresh the generated class file with the references to the new content added.
Now, when loading content in your game using the ContentManager object, instead of having to remember all those pesky string for the path to your content, you can just use the values in the generated ContentReference as follows (example below uses the content hierarchy described at the top of the readme)
var texture = Content.Load<Texture2D>(ContentReference.Textures.Player_01);
var menuSfx = Content.Load<SoundEffect>(ContentReference.Audio.SFX.Menu_click);