无法将资产加载为非内容文件

Hel*_*elp 5 c# monogame

为什么会这种情况继续发生?我研究它并且知道它们有帮助.代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Security.Policy;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Pratice
{
    public class CharacterClass
{
    public Texture2D texture;
    public Vector2 position, velocity;
    public Rectangle SourceRect;
    public string path;
    public bool HasJumped;

    public CharacterClass()
    {
        HasJumped = false;
        position = new Vector2();
        texture = null;
    }

    public void Initialize()
    {

    }

    public void LoadContent(ContentManager Content)
    {
        path = "Character/BlueAnvil";
        texture = Content.Load<Texture2D>(path);
    }

    public void Update(GameTime gameTime)
    {
        position += velocity;

        //input Controls
        KeyboardState keyState = Keyboard.GetState();
        if (keyState.IsKeyDown(Keys.A))
            position.X -= 5f;
        if (keyState.IsKeyDown(Keys.D))
            position.X = 5f;
        if (keyState.IsKeyDown(Keys.Space) && HasJumped == false)
        {
            position.Y -= 10f;
            velocity.Y = -5f;
            HasJumped = true;
        }

        if (HasJumped == true)
        {
            float i = 1;
            velocity.Y += 0.15f*i;
        }

        if (position.Y + texture.Height >= 450)
            HasJumped = false;

        if (HasJumped == false)
            velocity.Y = 0f;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, Color.White);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我需要得到这个修复,所以我记得它.了解如何做到这一点我需要帮助来做到这一点.所以我需要帮助,了解我做错了什么.

Imp*_*ian 7

正如Nahuel Ianni在他的回答中所说,游戏从内容目录加载内容文件.因此,所有内容都应放在Content目录中.

换句话说,它所看到的实际路径是"Content/Character/BlueAnvil".确保将文件放在正确的目录中.

由此可能产生的其他问题是,如果您使用的是Visual Studio,则可能无法将文件复制到输出中.您需要选择文件并打开属性,然后选择复制到输出并将其设置为更新或始终.

最后,还有文件本身的文件格式.如果它不是.xnb,则不太可能被接受..xnb文件由XNA或Monogame内容管道项目创建.在XNA中,所有文件都必须转换为此格式,但Content Pipeline会为您执行此操作.在Monogame中,有些文件可以直接加载,但它们因操作系统而异.我不记得接受了.png和.wav文件.我无法回想起其他接受的文件格式,并且无法找到我上次搜索时看到的便携式桌面.

因此,游戏实际上要加载的是"Content/Character/BlueAnvil.xnb" 或"Content/Character/BlueAnvil.png"

编辑:虽然我发布这个答案已经有一段时间了,但它仍然大部分是真的,我觉得我应该提到Monogame现在已经删除了ContentManager加载非.xnb文件的能力,例如.png和.wav.它,然而,有通过诸如Texture2D.FromStream(GraphicsDevice的,FILESTREAM)来加载这些文件的功能.这是你应该使用的.


Nah*_*nni 5

您的游戏应该在项目中有一个"内容"目录.在该目录中,您应该放置内容资源,例如图像 - BlueAnvil.png.然后你应该在游戏ctor中设置一个设置,你将内容目录设置为"Content":

Content.RootDirectory = "Content";
Run Code Online (Sandbox Code Playgroud)

LoadContent()你的游戏方法之后,你必须加载资产:Content.Load<Texture2D>("Character/BlueAnvil")如果你已经将BlueAnvil文件设置为内​​容,它应该拉入你的纹理,如上所述.在构建项目时,XS应该执行"优化PNG"步骤.

内容文件夹最终位于资源包中,将从中提取并从中创建您请求的Texture2D对象.