通过Monogame内容管道加载XML文件?

Hun*_*r X 5 c# xml xna monogame

我目前正在尝试将我正在从XNA工作的游戏移植到Monogame,但是我在使用内容管道进行合作时遇到了一些麻烦.我的游戏使用许多XML文件作为XNB资产来表示游戏中的对象,我按照这里的说明创建了这些对象.但是,尝试将其逐字转换为Monogame会产生以下错误:

MonoGame.Framework.dll中发生未处理的"Microsoft.Xna.Framework.Content.ContentLoadException"类型异常

附加信息:无法将Parts\Window.xnb资产加载为非内容文件!

这是我用来定义XML文件内容的类:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace FileTypes
{
    public class PartFile
    {
        public struct State
        {
            public Rectangle[] frames;
            public int[] collision;
        }

        public Vector2 size;
        public string image;
        public string defaultState = "default";
        public bool fillBG;
        public int ticksPerFrame;
        public Dictionary<string, State> states;
        public string[] modules;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是项目中的一个XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type="FileTypes.PartFile">
    <size>2 3</size>
    <image>Computer</image>
    <defaultState>default</defaultState>
    <fillBG>false</fillBG>
    <ticksPerFrame>7</ticksPerFrame>
    <states>
      <Item>
        <Key>default</Key>
        <Value>
          <frames>
            0 0 40 60
            40 0 40 60
          </frames>
          <collision>
            0 0
            0 0
            0 0
          </collision>
        </Value>
      </Item>
      <Item>
        <Key>active</Key>
        <Value>
          <frames>
            80 0 40 60
            120 0 40 60
          </frames>
          <collision>
            0 0
            0 0
            0 0
          </collision>
        </Value>
      </Item>
    </states>
    <modules>
      <Item>testModule</Item>
    </modules>
  </Asset>
</XnaContent>
Run Code Online (Sandbox Code Playgroud)

最后这里是我用来加载文件的代码示例:

FileTypes.PartFile srcPart = content.Load<FileTypes.PartFile>("Parts\\" + name);
Run Code Online (Sandbox Code Playgroud)

有没有人知道我需要做什么才能让我的代码在Monogame中运行?我一直在互联网上寻找一段时间,但到目前为止,我还没有找到解决问题的方法.或者,如果我一直在讨论整个系统的错误,并且有一个更容易的方法来处理我想做的事情,我很乐意听到它.

提前致谢!

cra*_*mes 5

我写了一篇关于使用 MonoGame 管道加载自定义内容的较长教程。这是摘要

  1. 创建一个新项目来保存您的内容导入器、处理器和编写器
  2. 创建内容导入器
  3. 创建内容处理器
  4. 创建内容类型编写器
  5. 创建内容类型阅读器
  6. 从管道工具引用 DLL
  7. 将内容读入您的游戏

创建导入器、处理器、读取器和写入器与 XNA 相同。您可以参考MSDN 文档

棘手的部分是让您的 DLL 与管道工具一起工作。要将其添加为参考,请查找根树节点的References属性。


小智 0

除非我弄错了,否则您需要在文件的属性选项卡中将文件的构建操作设置为“内容”,并将输出目录的副本设置为“如果较新则复制”