Monogame android:如何从流或Web服务加载XNB模型?

时间:2015-09-28 09:10:58

标签: c# android monogame

我正在为Xamarin Android项目开发Monogame。 任何人都知道如何加载从Web服务创建的XNB模型?基本上我的Web服务将返回一个XNB文件,Android设备应该能够获取该文件以立即显示3D模型。我知道如何从Assets Content文件夹加载静态XNB模型,但不知道如何从流或外部Web服务加载它,如下所示:

IBOutlet

1 个答案:

答案 0 :(得分:0)

我最终创建了从ContentManager派生的新类:

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");
}