最近我遇到了一些将资产加载为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");
}
但我每次都会得到同样的错误。
异常消息显示为标题&#34;无法将Character1资产加载为非内容文件&#34;并且内部异常表示&#34;找不到内容文件。&#34;
我还检查了文件应该复制到的文件夹,它包含.png和.xnb
答案 0 :(得分:2)
MonoGame 团队推出了一种新的更好的方法,可以使用MonoGame Pipeline应用程序管理项目中的内容,
要正确添加精灵,首先使用MonoGame管道工具打开 Content.mgcb 文件(你应该在你已经安装了monogame的地方找到它)
然后从编辑菜单中添加&gt;现有项目,然后选择您的图像,
构建内容,你应该好好去
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");
}