Pregunta

I'm trying to create a small XNA controls library for personal use, and I would like to provide default textures for the components. The problem is that I have no clue on how to include a content project inside a xna class library and access it from this class library to obtain those textures. Is this even possible? If yes, how can this be done?

¿Fue útil?

Solución

You can add content project to your XNA library by right clicking on the project header in Visual Studio Solution window and selecting Add content project... (Assuming the content project is already has been added into your solution).

If you want to use the resources provided by content project you have to pass Game::Content property into your library class constructor and load all the resources the same way as you do it in common XNA application.

public class Class1
{
    public Class1(ContentManager content)
    {
        var tex = content.Load<Texture2D>("test");
    }
}

Or you can implement your custom class as DrawableGameComponent and specified content will be loaded in LoadContent() phase and all overriden methods will be called automaticaly when its time to call them. Do smth like this:

public class Class1: DrawableGameComponent
{
    public Class1(Game game)
        : base(game)
    {
    }

    protected override void LoadContent()
    {
        var tex = this.Game.Content.Load<Texture2D>("test");
    }
}

You can add component in your Game class like this:

Components.Add(new Class1(this));

PS: If you use different content projects you might need to specify the root content folder before loading textures in your class library like this:

this.Game.Content.RootDIrectory="<ENTER CONTENT PROJECT NAME HERE>";
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top