Hey i’ve been looking around for a while now and i still cant find a straight answer to this problem. I have an XNA game in which part of the UI shows an icon which will change every so often(how often is irrelevant). I have decided to use a simple spritesheet maker which outputs the spritesheet as well as a simple XML file with the locations of each of the seperate icons in it.
What i would like to be able to do is read the spritesheet location and icon size of the appropriate icon from the XML file into a Rectangle. Then i can just use this as the source rectangle for the drawing bit.
However i have yet to come across a simple explanation of how to load in XML files into an XNA 4.0 project without writing my own content pipeline (something i would like to avoid if possible) and once loaded how to effectively (in general) extract the data into the Rectangle variable.
The XML file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with TexturePacker http://texturepacker.com-->
<!-- $TexturePacker:SmartUpdate:748fc56befb00c22540f953093f731a7$ -->
<!--Format:
n => name of the sprite
x => sprite x pos in texture
y => sprite y pos in texture
w => sprite width (may be trimmed)
h => sprite height (may be trimmed)
oX => sprite's x-corner offset (only available if trimmed)
oY => sprite's y-corner offset (only available if trimmed)
oW => sprite's original width (only available if trimmed)
oH => sprite's original height (only available if trimmed)
r => 'y' only set if sprite is rotated
-->
<TextureAtlas imagePath="Loadout_Icons.png" width="185" height="86">
<sprite n="Charge_Down.png" x="0" y="0" w="37" h="43"/>
<sprite n="Charge_Up.png" x="37" y="0" w="37" h="43"/>
<sprite n="Damage_Down.png" x="74" y="0" w="37" h="43"/>
<sprite n="Damage_Up.png" x="111" y="0" w="37" h="43"/>
<sprite n="FireRate_Down.png" x="148" y="0" w="37" h="43"/>
<sprite n="FireRate_Up.png" x="0" y="43" w="37" h="43"/>
<sprite n="Health_Down.png" x="37" y="43" w="37" h="43"/>
<sprite n="Health_Up.png" x="74" y="43" w="37" h="43"/>
<sprite n="Speed_Down.png" x="111" y="43" w="37" h="43"/>
<sprite n="Speed_Up.png" x="148" y="43" w="37" h="43"/>
</TextureAtlas>
Also im not sure if im shooting myself in the foot by not making my own XML files, would that be easier?
I have read most of what msdn had to offer with no avail but any links to relevant pages or questions would be much appreciated.
Thanks in advance.
As it was only mentioned in a comment I decided to say that I ended up using Niko Drašković’s suggestion of the purpose built pipeline extension made specifically for texture packer which can be found at http://thirdpartyninjas.com/blog/2012/08/02/texturepacker-xna-content-pipeline-extension/. That make it very easy to work with XML files produced by texture packer.
However as a bit of a side note and another reason why I revisited this question was to say that I have been doing a fair bit of work with XML’s lately (trying to get a better understanding for them) and the best way that I have found to deal with reading custom XML files without writing a new pipeline extension for each one (as elegant as that now seems)is as follows. I add the XML file to the content directory like you would any other texture etc. In the properties of the XML I set Build Action to “Content” and then Copy to Output Directory to “Copy Always”. From here I have a separate class in which I pass in the name of the file and inside which I process everything I need from the XML. These classes start with:
and then go on (using the rest of the System.Xml.Linq library) to read and store the appropriate data, with methods to access data easily. This may not be the best approach to tackling the problem but it has since worked for me and is simple enough to understand and implement quickly. Hope this can help anyone else with a similar issue.