如何在XNA在运行时加载纹理? [英] How do I load a texture in XNA at runtime?
问题描述
我正在使用XNA的框架,这样做的3D渲染的应用程序。我现在想从文件加载纹理。我发现两种方法至今:
I'm working on an application that uses the XNA framework to do it's 3D rendering. I now want to load a texture from file. I've found two methods so far:
-
Texture2D.FromStream(GraphicsDevice的,流)
这种方法的问题是,它只会加载GIF,PNG和JPG,我还需要TGA图像的支持。 - 创建一个
ContentManager
对象。这种方法的问题是,它似乎像所有的纹理需要静态添加到项目中,从文档:以前一个ContentManager可以加载的资产,你需要将资产添加到您的游戏项目。有问题的程序是一个关卡编辑器和需要哪些纹理事先是不知道。
Texture2D.FromStream(GraphicsDevice, Stream)
The problem with this approach is that it only loads gif, png and jpg and I also need support for tga images.- Create a
ContentManager
object. The problem with this approach is that it seems like all the textures need to be statically added to the project, from the documentation: "Before a ContentManager can load an asset, you need to add the asset to your game project". The program in question is a level editor and which textures are needed isn't known in advance.
有没有其他简单的方法来加载纹理,我在考虑使用一些其它的类加载图像(虽然我不知道我这,不是很熟悉C#),然后也许使用 Texture2D.SetData
方法?
Is there any other easy way to load the texture, I'm thinking about using some other class to load the image (although I don't know which, I'm not very familiar with C#) and then perhaps use the Texture2D.SetData
method?
有没有其他简单的方法来实现我想要达到什么目的?
Is there any other easy way to achieve what I'm trying to achieve?
推荐答案
有一些方法来达到你想要什么:
There are a few ways to achieve what you want:
-
你可以的编辑器中调用从的内容管道,动态创建内容的项目。如何做到这一点在的WinForms系列2样品。这大概是最好的方式,因为它允许您使用内容管道保持。
You could invoke the content pipeline from within your editor, dynamically creating your content project. How to do this is described in the WinForms Series 2 Sample. This is probably the "best" way, because it allows you to keep using the content pipeline.
您可以,就像你说的,解码TGA文件的自己并使用的SetData code>。有很多结果在谷歌C#TGA读者。 这是第一个。
You could, as you say, decode the TGA file yourself and use SetData
. There are lots of results for C# TGA readers on Google. This is the first one.
这篇关于如何在XNA在运行时加载纹理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!