I’ve rolled my own function making use of BinaryWriter for just saving the transform information for each object that has a transform in a scene. This works fine. When I load the information after changing the scene a bit (using SetActive to false on GameObjects) and save again, the state load doesn’t persist this change. I know why it isn’t, because I’m only storing location, rotation and scale, but I’d also like to store the active state boolean of these objects along with those transforms. Is there an easy way to do this using the BinaryWriter?
I was going to try to Serialize the GameObjects (would’ve seemed like the easiest method), but I had read somewhere that GameObject isn’t Serializeable because it wasn’t a “base class”. I’ll need to do more research before I can come up with a solution, I guess.
Just to make sure I understand…you are asking how to save a boolean using Binarywriter?
I personally love using BinaryWriter and BinaryReader.
Anyway, so if you are saving position, rotation, and scale, all floats, maybe you are doing something like this:
writer.Write(go.transform.position.x);
writer.Write(go.transform.position.y);
writer.Write(go.transform.position.z);
writer.Write(go.transform.rotation.x); // euler angles that is
writer.Write(go.transform.rotation.y);
writer.Write(go.transform.rotation.z);
writer.Write(go.transform.localscale.x);
writer.Write(go.transform.localscale.y);
writer.Write(go.transform.localscale.z);
// Save enabled state
writer.Write(writer.Write(go.Enabled));
Wouldn’t that save your transform AND your enabled state?
Personally, I’d subclass the binary writer and reader, and add my own serialization method to them for serializing Vectors and maybe even transforms. That way, it would look like this instead:
class MyWriter : BinaryWriter
{
public void Write(Vector3 vector)
{
Write(vector3.x);
Write(vector3.y);
Write(vector3.z);
}
}
then, when you serialize a gameobject, you just
writer.Write(go.transform.position);
writer.Write(go.transform.rotation); // Or its euler enagles
wrtier.Write(go.transform.localscale);
Or even better…create a single serialization method in your writer subclass that serializes an entire gameobject for you. Very handy elsewhere.
Thank you! THANK YOU! Really That was exactly what I needed, jc_lvngstn! For some reason I was using a Write function that only had Quaternion and Vector3 as acceptable parameters and didn’t understand to begin with what was going on. That cleared it up for me.