You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MonoGame项目启动失败:mgcb执行报错求助

解决MonoGame启动时mgcb命令执行错误(错误码1)

问题描述

使用MonoGame制作技术演示游戏,已修复Visual Studio中所有错误与警告,但启动时触发mgcb构建报错,错误信息如下:

C:\Users\Andre\.nuget\packages\monogame.content.builder.task\3.8.1.303\build\MonoGame.Content.Builder.Task.targets(142,5): error MSB3073: выход из команды "dotnet mgcb /quiet /@:"C:\Users\Andre\source\repos\Mission Phantasma\Content\Content.mgcb" /platform:Windows /outputDir:"C:/Users/Andre/source/repos/Mission Phantasma/Content/bin/Windows/Content" /intermediateDir:"C:/Users/Andre/source/repos/Mission Phantasma/Content/obj/Windows/net6.0-windows/Content" /workingDir:"C:/Users/Andre/source/repos/Mission Phantasma/Content/"" с кодом 1. [C:\Users\Andre\source\repos\Mission Phantasma\Mission Phantasma.csproj]

游戏核心代码:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended;
using MonoGame.Extended.Tiled;
using MonoGame.Extended.Tiled.Renderers;
using MonoGame.Extended.ViewportAdapters;

namespace Mission_Phantasma
{
    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;

        private Texture2D playerTexture;
        private Vector2 playerPosition;
        private Vector2 playerVelocity;
        private bool isJumping;
        private float jumpForce = -10f;
        private float gravity = 0.5f;

        private TiledMap tiledMap;
        private TiledMapRenderer mapRenderer;
        private OrthographicCamera camera;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            playerTexture = Content.Load<Texture2D>("Player1");

            tiledMap = Content.Load<TiledMap>("map");

            var viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, _graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight);
            camera = new OrthographicCamera(viewportAdapter);
            camera.LookAt(playerPosition);

            mapRenderer = new TiledMapRenderer(GraphicsDevice);
        }

        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyboardState = Keyboard.GetState();

            float acceleration = 0.5f;
            float maxSpeed = 5f;

            if (keyboardState.IsKeyDown(Keys.A))
                playerVelocity.X -= acceleration;
            else if (keyboardState.IsKeyDown(Keys.D))
                playerVelocity.X += acceleration;

            if (keyboardState.IsKeyDown(Keys.Space) && !isJumping)
            {
                playerVelocity.Y = jumpForce;
                isJumping = true;
            }

            playerVelocity.X = MathHelper.Clamp(playerVelocity.X, -maxSpeed, maxSpeed);
            playerVelocity.Y += gravity;

            playerPosition += playerVelocity;

            camera.LookAt(playerPosition);

            mapRenderer.Update(gameTime);


            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin(transformMatrix: camera.GetViewMatrix());

            mapRenderer.Draw();

            _spriteBatch.Draw(playerTexture, playerPosition, Color.White);

            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

怀疑XML格式的Tiled地图是问题根源,新建Windows项目并复制代码、导入资源后,仍出现相同报错。

排查与解决步骤

1. 查看mgcb详细错误信息

当前命令带/quiet参数会隐藏具体错误,手动执行命令去掉该参数:
打开命令提示符,切换到Content目录:

cd "C:\Users\Andre\source\repos\Mission Phantasma\Content"

执行完整构建命令:

dotnet mgcb /@:"Content.mgcb" /platform:Windows /outputDir:"C:/Users/Andre/source/repos/Mission Phantasma/Content/bin/Windows/Content" /intermediateDir:"C:/Users/Andre/source/repos/Mission Phantasma/Content/obj/Windows/net6.0-windows/Content" /workingDir:"C:/Users/Andre/source/repos/Mission Phantasma/Content/"

这会显示mgcb处理资源时的具体错误(如地图格式不兼容、路径错误、依赖缺失等)。

2. 验证Tiled地图兼容性

  • 确认地图是用MonoGame.Extended支持的Tiled版本导出(建议1.9.x或更早版本,避免新版本特性不兼容)。
  • 打开map.xml检查格式:确认标签闭合、属性值无特殊字符、tileset引用路径正确。
  • 确保地图依赖的tileset文件已添加到Content项目,且导入器设为TiledMapTilesetImporter、处理器设为TiledMapTilesetProcessor。

3. 检查Content.mgcb配置

用MonoGame Content Builder工具打开Content.mgcb:

  • 确认map.xml已添加,导入器为TiledMapImporter、处理器为TiledMapProcessor(需依赖MonoGame.Extended工具)。
  • 检查所有资源路径是否无中文/特殊字符(项目路径含空格Mission Phantasma可尝试改为无空格路径测试)。
  • 确认Player1纹理的导入器/处理器设置正确(TextureImporter和TextureProcessor)。

4. 匹配MonoGame与MonoGame.Extended版本

确保安装的MonoGame.Extended版本与MonoGame 3.8.1.303兼容,建议安装3.8.0版本(与MonoGame 3.8.x系列匹配),避免版本冲突导致处理器错误。

5. 修复mgcb工具安装

若工具安装不完整,重新安装:

dotnet tool uninstall -g dotnet-mgcb
dotnet tool install -g dotnet-mgcb
dotnet mgcb-editor --register

重启Visual Studio后重新生成项目。

6. 最小化项目测试

创建全新MonoGame Windows项目:

  • 先仅添加Player1纹理,测试能否正常启动。
  • 确认没问题后,逐步添加Tiled地图和tileset,定位具体问题资源。

内容的提问来源于stack exchange,提问作者Андрей Тарасов

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 13:05:17