- Home /
can texture2D and GameObject be serialized?
Its there any way to serialize Texture2D and gameobjects? Ive created an inventory system that uses them (one is the Icon and the other is the particles that get instantiated when the Item is used) but when I attempt to save the data using binaryformatter I get serialization exception error saying that texture2D its not market as serializable (or GameObject); here is the Item class I use:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class IItem {
// classes that implement Item should define this method
public string Type;
public string name;
public int Cantidad;
public Texture2D Icon;
public int Value;
public GameObject Particles;
public IItem(string newName, int newCantidad, Texture2D newIcon, int newValue, GameObject newParticles, string newType){
name = newName;
Cantidad = newCantidad;
Icon = newIcon;
Value = newValue;
Particles = newParticles;
Type = newType;
}
}
Thanks in advance!
Not much hope of doing that with Binary Formatter - check out EZSave or my free Unity Serializer that handle this kind of thing for you. Not always straightforward though of course, serialization is a tricky subject.
I see, I already tried your Serializer once but I decided to do this game without any kind plugins and code everything on my own; what I will do is remove the textures and the gameobject from this class and add every posible texture and gameobject in the inventory class, it seems like a messy solution but in fact there aren't that many Items and many of the gameobjects (which are particle emitters) are reused between items (for example all the potion items use the same particle emitter); thanks!
At least for the texture2D, perhaps you save the path+name? Then when you need to load the texture, you use path+name to re-attach your texture to your gameobject?
Unity's classes are blackboxed, so we can't adjust their serializability, or write wrapper classes to add our own.
So what you need to do is create an extension class, that adds serialization methods to your classes. Save everything you need to reinitialize the object back exactly how it was. Then reinitialize everything in your deserialization methods. The cleaner you set up your serialization/deserialization, the better. It's super nice once they are in place, especially for multiplayer games (if you're serializing to text).
$$anonymous$$eep in $$anonymous$$d there's quite a bit to save for a gameobject (name, tag, trans.locpos, trans.locrot, trans.locscale, trans.parent, layer). Try not to miss any of the tiny vars.
Please post it here when you're done! I'd love to use it myself!
Your answer

Follow this Question
Related Questions
Object Placement alignment to background image 2 Answers
Implementing GetComponent or accessing children from SerializableObjects 1 Answer
GameObject HideFlags.DontSave not working as expected 1 Answer
Better way for a tile based system 0 Answers
How to get mipChain and linear values from existing Texture2D 0 Answers