我一直在尝试使用Xamarin在MonoGame中加载纹理。我的代码设置如下:
#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
#endregion
namespace TestGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Game World
Texture2D texture;
Vector2 position = new Vector2(0,0);
public Game1 ()
{
graphics = new GraphicsDeviceManager (this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = false;
}
/// <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);
//Content
texture = Content.Load<Texture2D>("player");
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update (GameTime gameTime)
{
// For Mobile devices, this logic will close the Game when the Back button is pressed
if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed) {
Exit ();
}
// TODO: Add your update logic here
base.Update (gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw (GameTime gameTime)
{
graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
//Draw
spriteBatch.Begin ();
spriteBatch.Draw (texture, position, Color.White);
spriteBatch.End ();
base.Draw (gameTime);
}
}
}当我调试它时,它会给出错误:
Microsoft.Xna.Framework.Content.ContentLoadException:无法将播放器资产加载为非内容文件!-> Microsoft.Xna.Framework.Content.ContentLoadException:目录未找到。-> System.IO.DirectoryNotFoundException:找不到路径的一部分-> System.Exception: -内部异常堆栈跟踪结束 at System.IO.__Error.WinIOError(Int32 errorCode,String maybeFullPath) at at System.IO.FileStream.Init(字符串路径、FileMode模式、FileAccess访问、Int32权限、布尔useRights、FileShare共享、Int32 bufferSize、FileOptions选项、SECURITY_ATTRIBUTES secAttrs、String msgPath、布尔bFromProxy、布尔useLongPath) at at System.IO.FileStream..ctor(字符串路径、FileMode模式、FileAccess访问、FileShare共享、Int32 bufferSize、FileOptions选项、字符串msgPath、布尔bFromProxy) at System.IO.FileStream..ctor(字符串路径、FileMode模式、FileAccess访问、FileShare共享) at at System.IO.File.OpenRead(字符串路径) ( at at Microsoft.Xna.Framework.TitleContainer.OpenStream(String name) (在Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName) -内部异常堆栈跟踪结束 (在Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName) 在Microsoft.Xna.Framework.Content.ContentManager.ReadAssetT -内部异常堆栈跟踪结束 在Microsoft.Xna.Framework.Content.ContentManager.ReadAssetT 在Microsoft.Xna.Framework.Content.ContentManager.LoadT 在c:\Users\Flame\Documents\Projects\TestGame\TestGame\Game1.cs:0中的TestGame.Game1.LoadContent() at Microsoft.Xna.Framework.Game.Initialize() 在TestGame.Game1.Initialization()中的c:\Users\Flame\Documents\Projects\TestGame\TestGame\Game1.cs:0 at Microsoft.Xna.Framework.Game.DoInitialize() (在Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior) at Microsoft.Xna.Framework.Game.Run() 在c:\Users\Flame\Documents\Projects\TestGame\TestGame\Program.cs:0中的TestGame.Program.Main()
那我做错什么了?
发布于 2013-07-17 20:24:41
将png文件的'Build action'设置为'Content',将'Copy to output directory'设置为'Copy if newer'。
您可以通过ctrl单击文件并按属性来打开Xamarin中的“属性”窗口。
您不应该包括文件扩展名。
发布于 2013-05-09 21:51:32
您需要将该文件添加到解决方案的内容目录中,如果在“属性”窗口中更新,则将其设置为Content / copy。然后在构建过程中将其复制到输出目录。
请注意,您可以使用预编译的XNB文件(通常使用XNA游戏工作室创建),也可以使用原始的PNG图像文件。如果使用后者,则需要在代码中添加文件扩展名。
发布于 2017-08-08 18:53:24
将Content文件夹移到资产中,对于每个资源文件,将“Build action”设置为“AndroidAsset”,并将“Copy to output目录”设置为“Copy(如果更新)”。
https://stackoverflow.com/questions/16467207
复制相似问题