C# / XNA - Load objects to the memory - how it works?

后端 未结 3 1894
渐次进展
渐次进展 2021-01-12 08:20

I\'m starting with C# and XNA. In the \"Update\" method of the \"Game\" class I have this code:

t = Texture2D.FromFile( [...] ); //t is a \'Texture2D t;\'
         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-12 09:03

    As Andrew Russell says, loading your image 60 times per second is not what you want to do. However, I would add: Don't use Texture2D.FromFile at all!

    XNA provides a robust content pipeline for you -- use it!

    1. Add your image to your game project. XNA by default knows what to do with PNG, GIF, and JP(E)G image types. When you compile your project, XNA will also process your image into the XNA Binary (*.XNB) file format.
    2. In MyGame.LoadContent, load your image using myTexture = Content.Load(@"My/Image/Folder/MyImageAssetName")

    Content.Load will load the compiled XNB version of your image. You can also do things in the image properties in your project such as set a masking color (ex: all white pixels in your JPG will be transparent in your game), scale the image, and change the asset name. (The asset name defaults to the image file name, but you can change it in the properties, and the asset name is what you'll use for Content.Load.)

    Content.Load also caches the things it loads, so that if for some reason you have to call Load on the same asset multiple times, you're not multiplying the memory you use.

    The XNA content pipeline can be used for many other things as well as images, including 3D models, sound, and even your own classes.

提交回复
热议问题