Joys of Serialization…. in Unity….

So, serialization is super awesome…. Especially Binary Serialization, where you can save a file that cannot be edited outside your Application.

However, when attempting to use Binary Serialization in UNITY, it causes a whole mess of problems. For example, if you want to use Vector3s, Colors, etc., you must create your own float3 (or equivalent) so that you can serialize it, because Unity doesn’t have them as Serializable objects.

 

So, I decided to write up a basic text based tutorial on how I dealt with this:
Note: [System.Serializable] is KEY!

 

[System.Serializable]
public class ship {
[System.Serializable]
public class shipParts {
public string name;
public float3 position;
//…. more position and color data stored here as float3’s
}

public string name; //Information about the ship stored here
public List parts;
}

[System.Serializable]
public struct float3
{
public float3(float X, float Y, float Z)
{
this.x = X;
this.y = Y; // constructor for X Y Zs.
this.z = Z;
}

public float3(Vector3 vec3)
{
this.x = vec3.x;
this.y = vec3.y; // constructor for vector3s
this.z = vec3.z;
}

public float3(Color col3)
{
this.x = col3.r;
this.y = col3.g; // constructor for colors
this.z = col3.b;
}

public float x;
public float y; // variables
public float z;
};

As you can see here, I had to create a custom struct, just to store out the positions, then, when I re-instantiated, I had to create a new Vector3/Color and put in the values the float3.x, float3.y, and float3.z values.

Now, that I got that into place, time to serialize!

//……… code

var parts = new List();

//foreach part in scene, we need to create a part for it
foreach (var inSceneMesh in _inSceneMeshes)
{
var j = new ship.shipParts();
j.name = inSceneMesh.name.Split(‘(‘)[0];
j.position = new float3(inSceneMesh.position);
//more information about the part to store….
parts.Add(j);
}

//create our ship 🙂
var ship = new ship();
ship.parts = parts;
ship.name = _shipName;

//create a directory to save it in, if it doesn’t exist…
if (!Directory.Exists(string.Format(@”{0}\Saves\Ships”, Application.dataPath)))
Directory.CreateDirectory(string.Format(@”{0}\Saves\Ships”, Application.dataPath));

//open up a stream, that allows us to write data
var stream = new FileStream(string.Format(@”{0}\Saves\Ships\{1}.ship”, Application.dataPath, ship.name), FileMode.Create, FileAccess.Write, FileShare.Write);
//create a Binary Formatter, and serialize the data into a Binary format
//then close stream
var formatter = new BinaryFormatter();
formatter.Serialize(stream, ship);
stream.Close();

//……… code

Now that we have it all serialized, we can then go and de-serialize it! Yeah! Almost done!

//…….. more code
using (var stream = new FileStream(shipFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var formatter = new BinaryFormatter();
var obj = (ship)formatter.Deserialize(stream);
if (GUILayout.Button(obj.name))
{
ship.currentShip = obj;
Application.LoadLevel(“Builder”);
}
}
//…….. more code

As you can see, de-serialization is much easier, but that isn’t the end of it, that is just making it so that we can load it up in our Builder scene, so that we can take the info and stick it in, but you should be able to figure it out from there. 🙂 All you have to do is instantiate each shipPart, and and set its properties, which are stored in the ship.currentShip static variable, which holds a ship….

Hope this helps all of y’all!

Advertisements

Ship Editor Alpha version!

So, I finally got my wonderful, wonderful Ship editor working:
Download here!
The play and Editor buttons are in the upper left hand corner, play does nothing at the moment, however. Go to editor, and then click new. Then you can build! Name your ship, and press save, then press Exit, and go beck to editor, and click load, and you can get your ship.
It doesn’t include, at the moment at least, ship overriding, redo, or texturing. Undo is also slightly buggy.

Side project

Working on a Side project currently.

You know that Ship editor from Spore?
Well I am recreating that SORT OF. Not to the full extent of the spore editor, but enough to get what I want. This will be used in something I will be working on, on the side, as a project that I probably WILL get done before getting out of high school.

Anyways, pictures in the future!

Testing multiblog app

Testing an app on my phone to blog on multiple blogs, so far, seems ok…

A* working!

Pathfinding, basic AI, and a pathfinding editor are all working! sweet!

http://www.mediafire.com/?jdl4hae7cvj02j2

If you want to download the player to view how it works, there is the link.

Welcome to my Dev Blog!

Here, I will be posting my Dev stuff!

Twitter: @MarkusClardy

Youtube: steamisM50