Ok, so i've read a few pages about unity's serialization and i think i got some of it, but not really everything. I need to serialize these : Vertices, wich are unique Polygons, wich have references to the vertices. Polygons can have repeated vertices. Triangles, wich are unique, but each triangle has reference to three vertices, and up to 3 other triangles, wich are its neighbors. Some neighbors may be null. Map, wich is simply a 2d list of unique points. They dont have any references to the vertices or anything else. Here is the basic code structure Code (CSharp): using UnityEngine; using System; public class SerializeMe : MonoBehaviour { [SerializeField] internal Vertex[] vertices; // Want to serialize the vertices. No duplicate vertices exist. [SerializeField] internal Polygon[] polygons; // Want to serialize the polygons. Two different polygons may have the same vertex reference, or even one polygon itself may have two repeated vertices. [SerializeField] internal Triangle[] triangles; // Triangles have pointers to vertices and to other triangles. Dont want to lose these references. [SerializeField] internal Map map; // The map is kind of simple, its just a 2d list of non-repeated points. [Serializable] internal sealed class Vertex { internal float x; // Do i need to put [SerializeField] on this and all of the other internal variables? internal float y; // Want to serialize all of these internal values. internal Vertex(float x, float y) { this.x = x; this.y = y; } } [Serializable] internal sealed class Polygon { internal Vertex[] vertices; // How do i serialize these references without losing the actual reference? internal Polygon(Vertex[] vertices) { this.vertices = vertices; } } [Serializable] internal sealed class Triangle { internal Vertex[] vertices; // Dont want to lose reference to the original vertices upon serialization. internal Triangle[] neighbors; // Is this going to cause the 'null class loop' as some neighbors might be null, and null values cant be serialized? internal float[] values; // Want to serialize these as well. internal Triangle(Vertex[] vertices) { this.vertices = vertices; } internal void SetNeighbors(Triangle[] neighbors) { this.neighbors = neighbors; } } [Serializable] internal sealed class Map { private MapPoint[,] points; // Is there any problem of serializing this? No duplicate points, no nested stuff. internal float size; // Want to serialize this value. internal float resolution; // Want to serialize this value. internal MapPoint this[int i, int j] { get { return points[i, j]; } } internal Map(MapPoint[,] points) { this.points = points; } } [Serializable] internal sealed class MapPoint { internal bool boolValue; // Want to serialize this value. internal float floatValue; // Want to serialize this value. internal MapPoint(bool boolValue, float floatValue) { this.boolValue = boolValue; this.floatValue = floatValue; } } } So the big question is, how do i serialize all of that correctly? And by correctly i mean without losing actual references, preserving null values and not creating unecessary duplicate classes. Do i need to put [SerializeField] on every single internal or private variable inside the vertices, polygons, triangles and the map? Also, do the triangles create on of those "classes loop" described in here? : http://docs.unity3d.com/Manual/script-Serialization.html I know one way around would be to store int[] values pointing to the vertices and triangles array instead of classes references, but i kind of want the reference instead if possible. I suppose i could use a SerializableClass with ints and then rebuild the references on deserialization, but would that be the best or even the easiest option? It would look like this : Code (CSharp): SerializableTriangle[] serializableTriangles; [Serializable] private sealed class SerializableTriangle { public int[] verticesID; public int[] neighborsID; } private void RebuildTriangles() { triangles = new Triangle[serializableTriangles.Length]; for (int i = 0; i < serializableTriangles.Length; i++) { SerializableTriangle t = serializableTriangles[i]; Vertex a = vertices[t.verticesID[0]]; Vertex b = vertices[t.verticesID[1]]; Vertex c = vertices[t.verticesID[2]]; triangles[i] = new Triangle(new Vertex[3] { a, b, c }); Triangle n1 = triangles[t.neighborsID[0]]; Triangle n2 = triangles[t.neighborsID[1]]; Triangle n3 = triangles[t.neighborsID[2]]; triangles[i].SetNeighbors(new Triangle[3] { n1, n2, n3 }); } } By doing that, i would have about twice the data size stored, because i would have to store the serializable triangles and then rebuild the actual triangles with them every time. Sorry for the (very) long post, but unity's serialization kind of confuses me. And thank you very much for your patience and help, in advance!
You definitely need [SerializeField] on private data, not sure about internal, just try it and see. I haven't tried it myself, I'm pretty sure unity's serializer doesn't support two dimensional arrays. Unity's serializer sometimes changes between versions. For example in Unity 4.3, structs weren't serialized, but I think now they are. Basically, any code you need serialized, you need to test the serialization yourself. Unity's serializer is very particular so if you don't test it, there's a good chance it's not going to work how you expect. For advance stuff, like multidimensional arrays, you probably need to make use of ISerializationCallbackReceiver.