Monogame android:如何从流或网络服务加载 XNB 模型?
Monogame android: How to load XNB model from stream or web service?
我正在为 Xamarin Android 项目开发 Monogame。
有人知道如何加载从网络服务创建的 XNB 模型吗?基本上我的网络服务将 return 一个 XNB 文件并且 Android 设备应该能够获取该文件以立即显示 3D 模型。我知道如何从 Assets Content 文件夹加载静态 XNB 模型,但不知道如何从流或外部 Web 服务加载它,如下所示:
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
myModel = Content.Load<Model>("http://mywebservice.com/getXNBModel/1/");
}
我最终创建了从 ContentManager 派生的新 class:
class XNBContentManager : ContentManager
{
class FakeGraphicsService : IGraphicsDeviceService
{
public FakeGraphicsService(GraphicsDevice graphicDevice)
{
GraphicsDevice = graphicDevice;
}
public GraphicsDevice GraphicsDevice { get; private set; }
#pragma warning disable 67
public event EventHandler<EventArgs> DeviceCreated;
public event EventHandler<EventArgs> DeviceDisposing;
public event EventHandler<EventArgs> DeviceReset;
public event EventHandler<EventArgs> DeviceResetting;
#pragma warning restore 67
}
class FakeServiceProvider : IServiceProvider
{
GraphicsDevice _graphicDevice;
public FakeServiceProvider(GraphicsDevice graphicDevice)
{
_graphicDevice = graphicDevice;
}
public object GetService(Type serviceType)
{
if (serviceType == typeof(IGraphicsDeviceService))
return new FakeGraphicsService(_graphicDevice);
throw new NotImplementedException();
}
}
private readonly MemoryStream _xnbStream;
public XNBContentManager(MemoryStream xnbStream, GraphicsDevice graphicDevice)
: base(new FakeServiceProvider(graphicDevice), "Content")
{
_xnbStream = xnbStream;
}
protected override Stream OpenStream(string assetName)
{
return new MemoryStream(_xnbStream.GetBuffer(), false);
}
}
然后在LoadContent方法中使用:
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
var content = new XNBContentManager(_xnbStream, this.GraphicsDevice);
myModel = content.Load<Model>("WhateverName");
}
我正在为 Xamarin Android 项目开发 Monogame。 有人知道如何加载从网络服务创建的 XNB 模型吗?基本上我的网络服务将 return 一个 XNB 文件并且 Android 设备应该能够获取该文件以立即显示 3D 模型。我知道如何从 Assets Content 文件夹加载静态 XNB 模型,但不知道如何从流或外部 Web 服务加载它,如下所示:
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
myModel = Content.Load<Model>("http://mywebservice.com/getXNBModel/1/");
}
我最终创建了从 ContentManager 派生的新 class:
class XNBContentManager : ContentManager
{
class FakeGraphicsService : IGraphicsDeviceService
{
public FakeGraphicsService(GraphicsDevice graphicDevice)
{
GraphicsDevice = graphicDevice;
}
public GraphicsDevice GraphicsDevice { get; private set; }
#pragma warning disable 67
public event EventHandler<EventArgs> DeviceCreated;
public event EventHandler<EventArgs> DeviceDisposing;
public event EventHandler<EventArgs> DeviceReset;
public event EventHandler<EventArgs> DeviceResetting;
#pragma warning restore 67
}
class FakeServiceProvider : IServiceProvider
{
GraphicsDevice _graphicDevice;
public FakeServiceProvider(GraphicsDevice graphicDevice)
{
_graphicDevice = graphicDevice;
}
public object GetService(Type serviceType)
{
if (serviceType == typeof(IGraphicsDeviceService))
return new FakeGraphicsService(_graphicDevice);
throw new NotImplementedException();
}
}
private readonly MemoryStream _xnbStream;
public XNBContentManager(MemoryStream xnbStream, GraphicsDevice graphicDevice)
: base(new FakeServiceProvider(graphicDevice), "Content")
{
_xnbStream = xnbStream;
}
protected override Stream OpenStream(string assetName)
{
return new MemoryStream(_xnbStream.GetBuffer(), false);
}
}
然后在LoadContent方法中使用:
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
var content = new XNBContentManager(_xnbStream, this.GraphicsDevice);
myModel = content.Load<Model>("WhateverName");
}