jump to navigation

TexturePacker XNA Content Pipeline Extension August 2, 2012

Posted by Jesse in : Game Development , trackback

TexturePacker is pretty awesome.

The next St. Louis game jam will take place this coming weekend, so I’ve been going through and cleaning up some of my base code. I haven’t seen an XNA content pipeline loader for TexturePacker atlases, so I decided to share mine.

ThirdPartyNinjas.XnaUtility.1.0.zip

There’s a simple test included, and it shows how to load a texture atlas, look up a texture in the atlas, and render that texture using a few different origin options.

If you have trimming turned on, using the origin options can make things easier. There are four main options:

  1. TextureRegion.OriginTopLeft. This will draw/rotate around the original (pre-trimmed) top-left.
  2. TextureRegion.OriginBottomRight. The original bottom-right.
  3. TextureRegion.Center. The original center.
  4. Calculate your own origin using Width and Height in TextureRegion.Bounds.

A few things to keep in mind:

  1. While the loader supports rotated sprites if you choose that option in TexturePacker, the XNA SpriteBatch doesn’t easily support UV rotations so you’ll need to do extra work.
  2. By default, TexturePacker will name the texture and atlas with the same base filename. (i.e. foo.png and foo.xml) You’ll need to change that, or change the asset name of one of them to prevent a filename collision at build time.
  3. The content importer supports either the JSON-Array or Generic XML export options in TexturePacker.
  4. I didn’t put any sort of license notice in the source, but feel free to use it however you want. Not attribution or acknowledgement required. (If you need a specific license or info, let me know.)

Yell if you have any problems.

Comments»

1. AndreasLoew - August 2, 2012

Nice! I’ll link to it from my tutorials page.

2. GravitySoul - February 3, 2013

Hi, I don’t have the opportunity to check any codes as I’m responsible for the drawings and don’t have visual studio or XNA- how does it work and is there anything I should keep in my mind? I figured if this is a nice option, one might as well try to use it. How is it possible to load different sprite sheets (like character walking, or jumping) from one file? It’s really hard to comprehend.

3. Jesse - February 3, 2013

Rather than using different sprite sheets for running and jumping, you can use a tool like TexturePacker to combine all of the individual frames down to a single sheet. In fact, if you have a small enough game, you can fit all of the graphics for the game into a single sheet. (That’s what we did with Hypership Out of Control for iOS.)

There’s even some easy way to automate sprite sheet generation for your programmer. I recommend the tutorials on the TexturePacker site: http://www.codeandweb.com/texturepacker

The code I’ve shared here is just a way to load the sprite sheets exported from TexturePacker into a game written with XNA.

4. GravitySoul - February 4, 2013

I see, thank you. I’ll take a look at your recommended website. Also, thank you for the fast reply. Anything that can be of any help, I’m grateful for- I’d rather know of it than not.

Your example code here may anyway pose as a good resource if the programmers agree to using TexturePacker.