【发布时间】:2019-04-03 07:37:07
【问题描述】:
我正在创建一个 RPG 游戏,我需要使用 Tiled 加载地图。我有 Tiled 部分(我正在使用MonoGame.Extended)。但我需要关于地图的额外数据。我的计划是使用包含必要信息的 JSON 文件。但是,我希望它通过内容管道,因为它与瓦片地图直接相关。
我尝试使用自定义内容管道扩展。它使用JSON.Net 将JSON 文件反序列化为Dictionary<string, dynamic>。但是,当我编译 DLL 文件并尝试将其引用到管道工具中时,管道工具总是会崩溃。
内容导入器:
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline;
using Newtonsoft.Json;
namespace JsonExtension
{
[ContentImporter(".json", DefaultProcessor = "JsonProcessor")]
public class JsonImporter : ContentImporter<Dictionary<string, dynamic>>
{
public override Dictionary<string, dynamic> Import(string filename, ContentImporterContext context)
{
context.Logger.LogMessage("Importing JSON file: {0}", filename);
using (var streamReader = new StreamReader(filename))
{
JsonSerializer serializer = new JsonSerializer();
return (Dictionary<string, dynamic>)serializer.Deserialize(streamReader, typeof(Dictionary<string, dynamic>));
}
}
}
}
内容处理器:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content.Pipeline;
namespace JsonExtension
{
[ContentProcessor]
public class JsonProcessor : ContentProcessor<Dictionary<string, dynamic>, Dictionary<string, dynamic>>
{
public override Dictionary<string, dynamic> Process(Dictionary<string, dynamic> input, ContentProcessorContext context)
{
context.Logger.LogMessage("Processing JSON");
return input;
}
}
}
内容类型编写器:
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Newtonsoft.Json;
namespace JsonExtension
{
[ContentTypeWriter]
class JsonTypeWriter : ContentTypeWriter<Dictionary<string, dynamic>>
{
protected override void Write(ContentWriter output, Dictionary<string, dynamic> value)
{
output.Write(JsonConvert.SerializeObject(value));
}
public override string GetRuntimeType(TargetPlatform targetPlatform)
{
return typeof(Dictionary<string, dynamic>).AssemblyQualifiedName;
}
public override string GetRuntimeReader(TargetPlatform targetPlatform)
{
return "JsonExtension.JsonReader";
}
}
}
有没有更好的方法来存储平铺地图元数据?还是有更好的方法通过管道导入它?还是我的内容导入器有问题?
【问题讨论】:
-
与崩溃相关的错误信息是什么?
-
您可以将 Tiled 中的自定义属性添加到地图、图层,甚至添加到 Tiled 中的每个对象。我使用 TiledSharp 而不是 Monogame.Extended 但我确信它也支持自定义道具的使用。
-
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information那是来自内容管道的错误消息