【发布时间】:2015-10-16 22:30:09
【问题描述】:
我正在使用 Windows 窗体为 MonoGame 创建一些工具。我正在使用您可以在 Xbox Live 论坛上找到的教程。我实现了图形设备,但我不知道如何加载内容。有人可以帮我吗?
我正在使用 MonoGame 3.4 和 Visual Studio 2015
【问题讨论】:
我正在使用 Windows 窗体为 MonoGame 创建一些工具。我正在使用您可以在 Xbox Live 论坛上找到的教程。我实现了图形设备,但我不知道如何加载内容。有人可以帮我吗?
我正在使用 MonoGame 3.4 和 Visual Studio 2015
【问题讨论】:
要加载内容,您需要ContentManager。 Monogame 3.4 中 ContentManager 的构造函数采用 IServiceProvider 实例并解析 IGraphicsDeviceService 以获取 GraphicsDevice 实例。
由于您已经实现了GraphicsDevice,您只需实现IGraphicsDeviceService 和IServiceProvider。
我将只实现 ContentManager 工作所必需的。
首先实现IGraphicsDeviceService 以返回GraphicsDevice。
public class DeviceManager : IGraphicsDeviceService
{
public DeviceManager(GraphicsDevice device)
{
GraphicsDevice = device;
}
public GraphicsDevice GraphicsDevice
{
get;
}
public event EventHandler<EventArgs> DeviceCreated;
public event EventHandler<EventArgs> DeviceDisposing;
public event EventHandler<EventArgs> DeviceReset;
public event EventHandler<EventArgs> DeviceResetting;
}
然后实现IServiceProvider返回IGraphicsDeviceService
public class ServiceProvider : IServiceProvider
{
private readonly IGraphicsDeviceService deviceService;
public ServiceProvider(IGraphicsDeviceService deviceService)
{
this.deviceService = deviceService;
}
public object GetService(Type serviceType)
{
return deviceService;
}
}
最后你可以初始化ContentManager的一个新实例。
var content = new ContentManager(
new ServiceProvider(
new DeviceManager(graphicsDevice)));
不要忘记添加对Microsoft.Xna.Framework.Content 的引用。
【讨论】: