I am trying to serialize data in my XNA project. I have a Texture2D background property that I use during runtime, but I also have a ‘string property’ to hold my Background name. This will allow me to serialize the asset name so I can later use the info to de-serialize later and load into my game.
Problem is myTexture.Name property is suppose to hold the Asset Name but when I try to serialize into an XML file, the BackgroundName Element is empty.
Here is what the Property code looks like:
//This property is Only used for serialization, myTexture is Texture2D and is assigned in the ctor of the class
public string BGName { get { return this.myTexture.Name;} set{/*Empty on purpose*/} }
Could someone please advise, how to retrieve the asset name from a Texture2D, according to MSDN, this field holds the name of the texture.
The
Nameproperty onGraphicsResourceisn’t actually used by the XNA Framework; it’s up to you to populate it with whatever you feel is appropriate. You could write a helper method to do this easily enough: