我有这个运行时错误,我只是不明白是什么导致它。我在构建过程中没有遇到任何错误。我的代码分为两部分:一个游戏程序和一个主要引用的内容处理器。
完整错误是:
ContentLoadException未处理:找不到ContentTypeReader WindowsGame1.LevelContentReader,ContentImporter
以下是主要游戏:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace WindowsGame1
{
public class LevelContentReader : ContentTypeReader<Level>
{
protected override Level Read(ContentReader input, Level existingInstance)
{
Level level = new Level();
level.Background = input.ReadObject<Texture2D>();
int count = input.ReadInt32();
level.LevelObjects = new List<MyLevelObject>(count);
for (int i = 0; i < count; i++)
{
MyLevelObject obj = new MyLevelObject();
obj.model = input.ReadObject<Model>();
obj.Position = input.ReadVector3();
obj.Rotation = input.ReadVector3();
level.LevelObjects.Add(obj);
}
return level;
}
}
public class MyLevelObject
{
public Model model;
public Vector3 Position;
public Vector3 Rotation;
}
public class Level
{
public List<MyLevelObject> LevelObjects;
public Texture2D Background;
}
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Level mylevel;
Matrix view;
Matrix proj;
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);
view = Matrix.CreateLookAt(new Vector3(0, 10, 150), Vector3.Zero, Vector3.Up);
proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1.0f, 1000.0f);
mylevel = Content.Load<Level>("mylevel2");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(mylevel.Background, GraphicsDevice.Viewport.Bounds, Color.White);
spriteBatch.End();
foreach (MyLevelObject myObject in mylevel.LevelObjects)
{
foreach (ModelMesh mesh in myObject.model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
}
}
myObject.model.Draw(Matrix.CreateTranslation(myObject.Position) * Matrix.CreateFromYawPitchRoll(myObject.Rotation.X, myObject.Rotation.Y, myObject.Rotation.Z), view, proj);
}
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
这是我的内容处理器。主要游戏参考了这个,以及我的内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using TInput = System.String;
using TOutput = System.String;
namespace ContentImporter
{
[ContentTypeWriter]
public class LevelContentWriter : ContentTypeWriter<Level>
{
public override string GetRuntimeReader(TargetPlatform targetPlatform)
{
return "WindowsGame1.LevelContentReader, ContentImporter";
}
public override string GetRuntimeType(TargetPlatform targetPlatform)
{
return "ContentImporter.Level, ContentImporter";
}
protected override void Write(ContentWriter output, Level value)
{
output.WriteObject<TextureContent>(value.Background);
output.Write(value.LevelObjects.Count);
foreach (MyLevelObject obj in value.LevelObjects)
{
output.WriteObject<ModelContent>(obj.Model);
output.Write(obj.Position);
output.Write(obj.Rotation);
}
}
}
[ContentProcessor(DisplayName = "MyLevelProcessor")]
public class ContentProcessor1 : ContentProcessor<Level, Level>
{
public override Level Process(Level input, ContentProcessorContext context)
{
Level level = new Level();
level.LevelObjects = new List<MyLevelObject>();
//Process the input, we'll need to create the texture that is specified
ExternalReference<TextureContent> backgroundReference = new ExternalReference<TextureContent>(input.BackgroundFileName);
level.Background = context.BuildAndLoadAsset<TextureContent, TextureContent>(backgroundReference, "TextureProcessor");
foreach (MyLevelObject obj in input.LevelObjects)
{
MyLevelObject newobj = new MyLevelObject();
newobj.Position = obj.Position;
newobj.Rotation = obj.Rotation;
ExternalReference<NodeContent> modelReference = new ExternalReference<NodeContent>(obj.ModelFileName);
newobj.Model = context.BuildAndLoadAsset<NodeContent, ModelContent>(modelReference, "ModelProcessor");
level.LevelObjects.Add(newobj);
}
return level;
}
}
[ContentImporter(".mylevel", DisplayName = "My Level Importer", DefaultProcessor = "MyLevelProcessor")]
public class MyContentImporter : ContentImporter<Level>
{
public override Level Import(string filename, ContentImporterContext context)
{
Level level = new Level();
level.LevelObjects = new List<MyLevelObject>();
using (StreamReader reader = new StreamReader(File.OpenRead(filename)))
{
level.BackgroundFileName = GetAbsolutePath(filename, reader.ReadLine());
while (!reader.EndOfStream)
{
MyLevelObject info = new MyLevelObject();
info.ModelFileName = GetAbsolutePath(filename, reader.ReadLine());
string[] floats = reader.ReadLine().Split(' ');
info.Position = new Vector3(float.Parse(floats[0]), float.Parse(floats[1]), float.Parse(floats[2]));
info.Rotation = new Vector3(float.Parse(floats[3]), float.Parse(floats[4]), float.Parse(floats[5]));
level.LevelObjects.Add(info);
}
}
return level;
}
private string GetAbsolutePath(string input, string file)
{
return Path.Combine(Path.GetDirectoryName(input), file);
}
}
public class MyLevelObject
{
public ModelContent Model;
public Vector3 Position;
public Vector3 Rotation;
public string ModelFileName;
}
public class Level
{
public List<MyLevelObject> LevelObjects;
public TextureContent Background;
public string BackgroundFileName;
}
}
任何帮助都会在这件事上受到赞赏,因为我使用的书主要假设你接近C#的专家,它教授XNA背后的所有细节,而不是游戏概念。它不提供任何形式的故障排除。这本书是Tom Miller和Dean Johnson的XNA Game Studio 4.0编程。
BTW如果您需要的信息比我发布的信息多,我很乐意提供。