how to store Texture2D var in a XML file or in PlayerPrefs?

I have an inventory with this class slot:

class Slot
{
	var Number_Slot : int;
	var steto : String;										
   	var icon : Texture2D;
   	var name : String;
}

When i take and object in game, it copy his “public var Icon : Texture2D” in Slot.icon.
So inventory show the correct icon of the object taken.
It work fine. :smile:

Now the problem: I know how save a int and a string variable in a XML file, but how to save a Texture2D variable?
:frowning:

I want to create a save for all the slot of my inventory, but don’t know how store the icon images.

var Number_Slot : int; OK! :smile:
var steto : String; OK! :smile:
var icon : Texture2D; ERROR! :frowning:
var name : String; OK! :smile:

Here the error:

[B]InvalidOperationException: The type of the argument object 'UnityEngine.Texture2D' is not primitive.[/B]
System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive (System.String name, System.String ns, System.Object o, Boolean xsiType)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObject (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob, System.String element, System.String namesp, Boolean isNullable, Boolean needType, Boolean writeWrappingElem)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteMemberElement (System.Xml.Serialization.XmlTypeMapElementInfo elem, System.Object memberValue)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteListContent (System.Object container, System.Xml.Serialization.TypeData listType, System.Xml.Serialization.ListMap map, System.Object ob, System.Text.StringBuilder targetString)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteMemberElement (System.Xml.Serialization.XmlTypeMapElementInfo elem, System.Object memberValue)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteElementMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObjectElementElements (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObjectElement (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob, System.String element, System.String namesp)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObject (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob, System.String element, System.String namesp, Boolean isNullable, Boolean needType, Boolean writeWrappingElem)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteMemberElement (System.Xml.Serialization.XmlTypeMapElementInfo elem, System.Object memberValue)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteElementMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObjectElementElements (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObjectElement (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob, System.String element, System.String namesp)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObject (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob, System.String element, System.String namesp, Boolean isNullable, Boolean needType, Boolean writeWrappingElem)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteMemberElement (System.Xml.Serialization.XmlTypeMapElementInfo elem, System.Object memberValue)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteElementMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObjectElementElements (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObjectElement (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob, System.String element, System.String namesp)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteObject (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob, System.String element, System.String namesp, Boolean isNullable, Boolean needType, Boolean writeWrappingElem)
System.Xml.Serialization.XmlSerializationWriterInterpreter.WriteRoot (System.Object ob)
System.Xml.Serialization.XmlSerializer.Serialize (System.Object o, System.Xml.Serialization.XmlSerializationWriter writer)
System.Xml.Serialization.XmlSerializer.Serialize (System.Xml.XmlWriter writer, System.Object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces)
Rethrow as InvalidOperationException: There was an error generating the XML document.
System.Xml.Serialization.XmlSerializer.Serialize (System.Xml.XmlWriter writer, System.Object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces)
System.Xml.Serialization.XmlSerializer.Serialize (System.Xml.XmlWriter xmlWriter, System.Object o)
save_engine.SerializeObject (System.Object pObject) (at Assets/Scripts/Avventura_script/save_engine.js:311)
save_engine.OnGUI () (at Assets/Scripts/Avventura_script/save_engine.js:277)

I’m using http://www.unifycommunity.com/wiki/index.php?title=Save_and_Load_from_XML

that will not work, ever

texture2d is a reference to another object but that object will not serialize.

what you will need to do is store a string there. more precisely: the path to the texture to load it from the Resources folder.

thats the only way you can do this

1 Like

I’m not sure about Javascript, but I know that it isn’t possible to serialize a Texture2D object in C#. You wouldn’t really want to serialize it anyways as the image-data would have to be encoded in “text” and it would just look like millions of characters of nonsense.

Instead you can make your own proxy class to store the info you need… I did this for a recent project and it works out nicely. Instead of actually serializing/deserializing a Texture2D you have a class (or even just a string) that contains the path to the file (if loading a file externally) or the name of the resource in your assets folder.

After deserializing you just use either the WWW class to load an external image from a string path, or you use Resources.Load to get the Texture2D reference from your already loaded assets.

You can use the same proxy method to serialize anything that is not serializable. I find it much preferable to using the default (ugly) serializing that C# does… where everything is an XML node rather than using attributes for numbers and strings and such.

Ok, thanks!
There is a way to get the image path of a texture from the Texture2D var?

Because:

1- I create and object with script “var icon : Texture2D;” in it
2- I click and drag the image from resurce into Texture2D in inspector pannel
3- now can I do a request alike “get image path from var icon” ?

Edit. Solved! :slight_smile:

script:

var icon : Texture2D;
var icon_name : String;
icon_name = icon.name;