【问题标题】:MonoGame Windows OpenGL project "Could not load asset as non-content file" Asset is set to Copy AlwaysMonoGame Windows OpenGL 项目“无法将资产加载为非内容文件”资产设置为始终复制
【发布时间】:2016-01-26 05:18:56
【问题描述】:

最近我在将资源加载为 Texture2D 时遇到了一些麻烦。我尝试将路径更改为“..\assetName”、“assetName”、“assetName.png”和“..\assetName.png”(正如this问题的答案之一所建议的那样,但他们都没有作业。我还尝试在添加文件时使用 MonoGame 内容管道,并且“复制到输出目录”参数当前设置为“始终复制”。

我尝试使用一些非常简单的代码创建一个新项目来加载内容文件,它的内容如下

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TempSol2
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
    Texture2D texture;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
        texture = Content.Load<Texture2D>("Character1");
    }

但我每次都会遇到同样的错误。

异常消息显示为标题显示“无法将 Character1 资产加载为非内容文件”,内部异常显示“未找到内容文件”。

我还检查了文件应该复制到的文件夹,它包含 .png 和 .xnb

【问题讨论】:

    标签: c# exception assets monogame


    【解决方案1】:

    MonoGame 团队引入了一种新的更好的方法来使用 MonoGame Pipeline 应用程序来管理项目中的内容,

    要正确添加精灵,首先使用 MonoGame Pipeline 工具打开 Content.mgcb 文件(您应该在安装 monogame 的地方找到它)

    然后从编辑菜单中,添加 > 现有项目,然后选择您的图像,

    构建内容,您应该一切顺利

     spriteBatch.Begin();
     spriteBatch.Draw(texture,Vector2.Zero,Color.White);
     spriteBatch.End();
    

    从here阅读有关管理内容的更多信息。

    编辑 要加载 Sprite,请使用完全相同的方式

      protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
    
            // TODO: use this.Content to load your game content here
            texture = Content.Load<Texture2D>("Character1");
        }
    

    【讨论】:

    • 你忘了说如何加载内容
    • 我实际上尝试在手机上对其进行编辑,但显然效果不佳。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多