我正在构建一个将接受JPEG/PNG的XNA游戏的游戏编辑器,并输出其中包含Texture2D内容的XNB文件。在XNA中以编程方式序列化Texture2D
我有我的自定义序列为我定制等,我都挖下到使用反射等,已经进入真正的脏位调用ContentCompiler,我CAN实际得到的那部分我自己的格式工作。但是我现在需要序列化为常规的Texture2D格式,XNA知道如何编译(在构建项目时在Visual Studio中)并加载(在游戏中使用最常用的Content.Load方法。我不想制作自己的图像当我尝试编译一个Texture2D时,它并没有给我一个错误,它创建了XNB文件,但文件内容不是图像数据,它只有3-4 KB的开销(其中I认为是一个Texture2D对象的其他非图像属性)
这里是我使用的编译代码:
protected void InitializeWriter()
{
Type compilerType = typeof(ContentCompiler);
cc = compilerType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0].Invoke(null) as ContentCompiler;
compileMethod = compilerType.GetMethod("Compile", BindingFlags.NonPublic | BindingFlags.Instance);
}
internal void Write(string objectName, string path, object objectToWrite)
{
if (basePath == null)
{
throw new InvalidOperationException("The base path to write has not been set.");
}
if (cc == null) { InitializeWriter(); }
string fullPath = Path.Combine(basePath, path, objectName + ".xnb");
using (FileStream fs = File.Create(fullPath))
{
compileMethod.Invoke(cc, new object[]{
fs, objectToWrite, TargetPlatform.Windows, GraphicsProfile.Reach, false/*true*/, fullPath, fullPath
});
}
}
我有我自己的内容处理器和此代码的工作与他们完美此。只是调用ContentCompiler的编译方法,但使用反射(因为它是一个内部类)。
我不明白为什么Texture2D序列化,但没有实际的位图数据。
谢谢, 可以。
编辑 对不起,它不编译的Texture2D(它抱怨我的GraphicsDevice的循环引用),如果我尝试编译一个位图对象,它没有错误编译但不序列实际的位图数据)
如何将TextureImporter导入到我的项目引用中,您是否也可以解释它?谢谢 ! – icaptan 2012-03-12 09:14:49
引用XNA内容管道组件。 – 2012-03-24 20:06:24
我无法在XNA 4.0上找到任何对XnaContentPipeline程序集的引用,我尝试将它添加到项目中。你能给出全名吗?谢谢 ! – icaptan 2012-03-24 22:05:56