【问题标题】:Monogame android: How to load XNB model from stream or web service?Monogame android:如何从流或网络服务加载 XNB 模型?
【发布时间】:2015-10-23 00:26:05
【问题描述】:

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

protected override void LoadContent()
{            
     spriteBatch = new SpriteBatch(GraphicsDevice);
     myModel = Content.Load<Model>("http://mywebservice.com/getXNBModel/1/");
}

【问题讨论】:

    标签: c# android monogame


    【解决方案1】:

    我最终创建了从 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");
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-19
      • 1970-01-01
      • 2013-03-23
      • 2013-09-18
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多