XNA使用XML

前端之家收集整理的这篇文章主要介绍了XNA使用XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

整个周末都奉献给了XNA和XML和《生活大爆炸》。

虽然很想写一篇总结的,但是等不及去实现新的创意了.于是只好将MSDN上相关的内容直接COPY到这里了.简单易用哦.

Adding an XML Content File to a Visual Studio Project

Describes how to add custom game data as an XML file through the Content Pipeline.

Adding XML files to game content

Custom game data that is expressed in an XML format can be easily integrated into your game through the XNA Game Studio Content Pipeline.

This example demonstrates the procedure for integrating custom XML data into the content project of a simple XNA Game Studio game for the Windows platform.

To define game data

Within the XNA Game Studio solution,you create a new Windows Game Library project.

  1. Right-click the solution node,point toAdd,clickNew Project,and then select the Windows Game Library template.

    Note

    A Windows Game Library project is created instead of a Content Pipeline Extension Library project so that the class we will define can be used by both the content importer that runs at build-time and the content loader at game runtime.

  2. In theNameBox,typeMyDataTypes,and then clickOK.

  3. InSolution Explorer,double-click Class1.cs to edit the new library project file.

  4. Replace the default template with the following code to define the class.

    C#
    namespace MyDataTypes
    {
        public class PetData
        {
            public string Name;
            public string Species;
            public float Weight;
            public int Age;
        }
    }
    

To add an XML file to game content

In this procedure,the "MyDataTypes" library is added as a reference in the content project.

  1. A new content item is created next.

  2. In theAdd New Itemdialog Box,selectXML file (.xml)in the Templates pane.

  3. The new "pets.xml" file automatically opens for editing.

  4. Replace the contents of the template file with the following XML code:

    XML
    <?xml version="1.0" encoding="utf-8" ?>
    <XnaContent>
      <Asset Type="MyDataTypes.PetData[]">
        <Item>
          <Name>Fifi</Name>
          <Species>Dog</Species>
          <Weight>11</Weight>
          <Age>6</Age>
        </Item>
        <Name>Bruno</Weight>21</Age>12</Name>Chloe</Species>Cat</Weight>6</Age>3</Name>Pickles</Species>Hamster</Weight>0.4</Age>1</Item>
      </Asset>
    </XnaContent>        
          
    

When you press F6 to build the solution,it should build successfully,including the custom game content imported from the XML file.

To load the data at runtime,see the tutorialLoading XML Content at Runtime.

Loading XML Content at Runtime

Describes how to load custom game data at game runtime through the Content Pipeline. Loading custom game content
This example concludes the procedure begun in the tutorialAdding an XML Content File to a Visual Studio Project.

Once custom game data is integrated as game content from an XML file through the Content Pipeline,it exists within your game runtime package in binary format. The data can beloaded at runtimethrough theContentManager.

To load the custom data in the game

The "MyDataTypes" library is added as a reference in the game project.

  1. Add the using declaration for the MyDataTypes namespace.

    using MyDataTypes;
    
  2. Add a data declaration for an array of type PetData,the class defined in the "MyDataTypes" library.

    PetData[] pets;
    
  3. In theLoadContentoverride function,load the custom content.

    protected override void LoadContent()
    {
        // Load the pet data table
        pets = Content.Load<PetData[]>("pets");
    }
    

    The custom game data now resides in the array of PetData objects.

XML Elements for XMLImporter

ML Elements

The following base elements are recognized byXmlImporter Class:

Element Parent Children Description
<XnaContent> <Asset> Top-level tag for XNA Content.
<Item> Marks the asset. TheTypeattribute specifies the corresponding namespace and class of the matching data type.
When the asset contains multiple objects (as in an array),marks a single object within the group. The child elements correspond to the properties of the data type's class definition.
Examples

Example 1: Single Object

This example demonstrates an XML file that defines an asset that consists of a single item (not an array).

Assume that the XML file is to define a single object of data for the class that is defined as:

C#
namespace XMLTest
{
    class MyTest
    {
        public int elf;
        public string hello;
    }
}
        

The XML file that specifies the data that the Content Loader will read into the object would appear as:

XML
xml version="1.0" encoding="utf-8"?>
<Asset Type="XMLTest.MyTest">
    <elf>23</elf>
    <hello>Hello World</hello>
  </XnaContent>
      
Example 2: Multiple Objects

This example demonstrates an XML file that defines an asset that is an array of objects.

Assume that the XML file is to define an array of data for the class that is defined as:

namespace MyDataTypes
{
    public class CatData
    {
        public string Name;
        public float Weight;
        public int Lives;
    }
}

The XML file that specifies the data that the Content Loader will read into the object array would appear as:

Asset Type="MyDataTypes.CatData[]">
    <Name>Rhys</Weight>17</Lives>9</Lives>
    </Name>Boo</Lives>5</XnaContent>
      

猜你在找的XML相关文章