缺乏从其他项目访问XNA ContentManager



我有一个有点复杂的问题,因为我没有办法,所以我又来了。

我在应用程序上工作,从任何3d引擎生成镜头,我(不幸的是)建议XNA来实现这一点,因为我们想在c#中制作应用程序。我想发送我的编程伴侣与XNA代码库(在接口的形式),所以他甚至不需要知道图像是如何生成的。

这是我的问题-我不能为我的场景加载模型和纹理,因为contentmanager不在库项目中,尽管我单独运行xna游戏没有问题。

我的问题是:

  1. 是否可以访问/加载内容,以便在库中可用?
  2. 有什么库来生成3d场景(c#)你可以推荐吗?

是的,可以在库项目中加载内容。您可以在Game类之外创建ContentManager的实例,并使用它来加载内容。

创建ContentManager的新实例的技巧是两个构造函数,它们都接受IServiceProvidor的实例作为第一个参数。创建ContentManager的任何类都可以实现这个接口。这个接口只需要实现一个方法,那就是:
public object GetService(Type serviceType)

它将被ContentManager调用以检索IGraphicsDeviceService的实例。您需要一个可以实现这个接口的类,它主要是与设备创建和销毁相关的事件。有一个重要的属性需要实现,那就是:

public GraphicsDevice GraphicsDevice
我将在这个答案中留下很多样板代码,因为你可以在互联网上的其他地方找到它。下面的代码假设您已经初始化了XNA Graphics系统,并在创建该类 的实例之前创建了GraphicsDevice
public class ImageGenerator : IServiceProvider, IGraphicsDeviceService
{
    public GraphicsDevice GraphicsDevice { get; private set; }
    public ContentManager ContentManager { get; private set; }
    public ImageGenerator( GraphicsDevice device )
    {
        this.GraphicsDevice = device;
        this.ContentManager = new ContentManager( this );
    }
    public object GetService(Type serviceType)
    {
        if (serviceType == typeof(IGraphicsDeviceService))
        {
            return this;
        }
        return null;
    }
    public event EventHandler<EventArgs> DeviceCreated;
    public event EventHandler<EventArgs> DeviceDisposing;
    public event EventHandler<EventArgs> DeviceReset;
    public event EventHandler<EventArgs> DeviceResetting;
}

在走这条路之前,我建议你调查子类化Game,并允许XNA框架处理繁重的工作,你可以只担心渲染场景和创建图像。

Axiom (http://www.axiom3d.net)是一个c#引擎,能够生成3D场景到位图图像。