Xna 内容管道扩展 - 在自定义处理器中加载其他内容
我目前正在尝试 Xna Content Pipeline 扩展。在该实验中,我尝试加载一个包含另一个需要加载的“内容项”的文件。例如:
public class CustomItem
{
public string Name;
public Texture2D Texture;
}
现在,在我的内容处理器中,我可以创建“CustomItem”的新实例并初始化“名称”字段,因为它只是一个字符串。但是,我无法在内容编译期间加载纹理文件(注意:纹理只是一个示例,理想情况下我希望能够加载任何其他内容类型)。
我正在寻找的是这样的东西:
// ... start class ...
public override CustomItem Process(SomeInputFormat input, ContentProcessorContext context)
{
return new CustomItem()
{
Name = input.ItemName,
Texture = context.LoadAsset<Texture2D>(input.ItemTexturePath) // I realise LoadAsset<T>() does not exist - it's an example of what would be ideal
};
}
// ... end class ...
有谁知道这是否真的可能,如果是的话,如何去做?如果可能的话,我不想走后期加载其他内容项的路线,或者使用二进制读取器和写入器创建我自己的自定义内容加载。
I'm currently experimenting with Xna Content Pipeline extensions. Within that experimentation, I'm trying to load a file that contains another 'content item' that is in need of loading. For example:
public class CustomItem
{
public string Name;
public Texture2D Texture;
}
Now, in my content processor, I can create a new instance of 'CustomItem' and initialize the Name field, since it's simply a string. However, I can't load the texture file during content compilation (NOTE: The texture is just an example, ideally I'd like to be able to load any other content type).
What I'm looking for is something like:
// ... start class ...
public override CustomItem Process(SomeInputFormat input, ContentProcessorContext context)
{
return new CustomItem()
{
Name = input.ItemName,
Texture = context.LoadAsset<Texture2D>(input.ItemTexturePath) // I realise LoadAsset<T>() does not exist - it's an example of what would be ideal
};
}
// ... end class ...
Does anyone know if this is actually possible, and if so, how to go about it? I'd rather not go down the route of late loading the other content items if possible, or creating my own, custom content loading using binary readers and writers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在内容管道中使用
Texture2D
。您必须使用Texture2DContent
,它是前者的代理类型。反过来,您的类型中必须有一种机制,允许成员在内容构建时为Texture2DContent
,但在运行时为Texture2D
。 本文< /a> 为您提供了三种执行此操作的方法。您可以使用 ContentProcessorContext.BuildAndLoadAsset 来获取您的 Texture2DContent 对象。此纹理数据将嵌入到该资源的
.xnb
文件中。如果您实际上不需要在管道中使用纹理数据,特别是如果您打算在多个资源之间共享相同的纹理,则可以使用 ContentProcessorContext.BuildAsset 来获取
纹理的外部引用
,该纹理内置于其自己的.xnb
文件中,位于资源的.xnb
文件(以及ContentManager
)外部将处理加载 你)。You can't use
Texture2D
in the content pipeline. You have to useTexture2DContent
, which is a proxy-type for the former. In turn, you must have a mechanism in your type for allowing the member to beTexture2DContent
at content build time, butTexture2D
at run-time. This article gives you three ways of doing this.You can use
ContentProcessorContext.BuildAndLoadAsset
to get yourTexture2DContent
object. This texture data will be embedded into your.xnb
file for that asset.If you don't actually need to use the texture data in the pipeline, and in particular if you intend to share the same texture between multiple assets, you can use
ContentProcessorContext.BuildAsset
to get anExternalReference
to the texture, which is built into its own.xnb
file, external to your asset's.xnb
file (andContentManager
will handle the loading for you).