Saving and loading list with SerializableDictionary

I am new in unity. I am using save and load system from this video How to make a Save & Load System in Unity | 2022 - YouTube I am trying to save in SerializableDictionary lists from multiple objects but always when I try to save it I get a lot of errors. Can someone help me?

If you define the following two types in a C# script:

 [System.Serializable]
 public struct Vector2iList {
     public List<Vector2i> value;
 }
 
 [System.Serializable]
 public struct Vector2iTable {
     public List<Vector2iList> value;
 }

Then you will be able to use these types like so, replace int with your type:

public class Test : MonoBehaviour
{
    public SerializedDictionary<int, Vector2iTable> polygons;
}

This will be completely serializable. How to access and edit polygons will become different, meaning you will have to change some of the code to match the new structure. You can edit the contents of polygons by accessing the value fields of Vector2iTable and Vector2iList.

this is one of the errors:
Tried to deserialize a SerializableDictionary, but the amount of keys (144) does not match the number of values (0) which indicates that something went wrong

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using int64 = System.Int64;
using Vector2i = ClipperLib.IntPoint;
using Vector2f = UnityEngine.Vector2;

public class DestructibleBlock : MonoBehaviour, IDataPersistence
{
private List<List> polygons;

//private List<List<Vector2f>> edgesList;

private List<EdgeCollider2D> colliders = new List<EdgeCollider2D>();

public List<List<Vector2i>> Polygons { get { return polygons; } }

private Mesh mesh;

private MeshRenderer meshRenderer;

public string test;
public string test1;
public string test2;
public string test3;
public string test4;

private void Awake()
{
    mesh = new Mesh();
    mesh.MarkDynamic();

    MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
    meshFilter.mesh = mesh;
    gameObject.transform.name.ToString();

    meshRenderer = gameObject.AddComponent<MeshRenderer>();
}

public void SetMaterial(Material material)
{
    meshRenderer.material = material;
}
 public void LoadData(GameData data)
{
   
}
public void SaveData( GameData data)
{
    if (data.polygons1.ContainsKey(name))
    {
        data.polygons1.Remove(name);
    }
     data.polygons1.Add(name, polygons1);
}


//public void UpdateGemetry(List<List<Vector2i>> inPolygons, float depth)
//{      
//    if (polygons != null)
//        polygons.Clear();
//    else
//        polygons = new List<List<Vector2i>>();

//    List<List<Vector2>> edgesList = new List<List<Vector2f>>();

//    int totalVertexCount = 0;
//    int edgeTriangleIndexCount = 0;

//    for (int i = 0; i < inPolygons.Count; i++)
//    {
//        Vector2i[] simplifiedPolygon = BlockSimplification.Execute(inPolygons*, edgesList);*

// if (simplifiedPolygon != null)
// {
// polygons.Add(new List(simplifiedPolygon));
// totalVertexCount += simplifiedPolygon.Length;
// }
// }
// for (int i = 0; i < edgesList.Count; i++)
// {
// int vertexCount = edgesList*.Count;*
// totalVertexCount += vertexCount * 2;
// edgeTriangleIndexCount += (vertexCount - 1) * 6;
// }
// Vector3[] vertices = new Vector3[totalVertexCount];
// List triangles = new List();
// int[] edgeTriangles = new int[edgeTriangleIndexCount];
// int vertexIndex = 0;
// int vertexOffset = 0;
// for (int i = 0; i < polygons.Count; i++)
// {
// List polygon = polygons*;*
// int vertexCount = polygon.Count;
// for (int j = vertexCount - 1; j >= 0; j–)
// {
// vertices[vertexIndex] = polygon[j].ToVector3f();
// vertexIndex++;
// }
// Triangulate.Execute(vertices, vertexOffset, vertexOffset + vertexCount, triangles);
// vertexOffset += vertexCount;
// }
// int edgeTriangleIndex = 0;
// for (int i = 0; i < edgesList.Count; i++)
// {
// List edgePoints = edgesList*;*
// int vertexCount = edgePoints.Count;
// Vector3 point;
// for (int j = 0; j < vertexCount - 1; j++)
// {
// point = edgePoints[j].ToVector3f();
// vertices[vertexIndex] = point;
// point.z += depth;
// vertices[vertexIndex + 1] = point;
// edgeTriangles[edgeTriangleIndex + 0] = vertexIndex;
// edgeTriangles[edgeTriangleIndex + 1] = vertexIndex + 2;
// edgeTriangles[edgeTriangleIndex + 2] = vertexIndex + 1;
// edgeTriangles[edgeTriangleIndex + 3] = vertexIndex + 2;
// edgeTriangles[edgeTriangleIndex + 4] = vertexIndex + 3;
// edgeTriangles[edgeTriangleIndex + 5] = vertexIndex + 1;
// vertexIndex += 2;
// edgeTriangleIndex += 6;
// }
// point = edgePoints[vertexCount - 1].ToVector3f();
// vertices[vertexIndex] = point;
// point.z += depth;
// vertices[vertexIndex + 1] = point;
// vertexIndex += 2;
// }
// triangles.AddRange(edgeTriangles);
// mesh.Clear();
// mesh.vertices = vertices;
// mesh.triangles = triangles.ToArray();
// mesh.RecalculateNormals();
// //mesh.MarkModified();
// UpdateColliders(edgesList);
//}
public void UpdateGeometryWithMoreVertices(List<List> inPolygons, float width, float height, float depth)
{
if (polygons != null)
polygons.Clear();
else
polygons = new List<List>();
List<List> edgesList = new List<List>();
int totalVertexCount = 0;
int edgeTriangleIndexCount = 0;
for (int i = 0; i < inPolygons.Count; i++)
{
Vector2i[] simplifiedPolygon = BlockSimplification.Execute(inPolygons*, edgesList);*
if (simplifiedPolygon != null)
{
polygons.Add(new List(simplifiedPolygon));
totalVertexCount += simplifiedPolygon.Length;
}
}
for (int i = 0; i < edgesList.Count; i++)
{
int vertexCount = edgesList*.Count;*
totalVertexCount += (vertexCount -1) * 4;
edgeTriangleIndexCount += (vertexCount - 1) * 6;
}
Vector3[] vertices = new Vector3[totalVertexCount];
Vector3[] normals = new Vector3[totalVertexCount];
Vector2f[] texCoords = new Vector2f[totalVertexCount];
List triangles = new List();
int[] edgeTriangles = new int[edgeTriangleIndexCount];
int vertexIndex = 0;
int vertexOffset = 0;

for (int i = 0; i < polygons.Count; i++)
{
List polygon = polygons*;*
int vertexCount = polygon.Count;
for (int j = vertexCount - 1; j >= 0; j–)
{
Vector3 point = polygon[j].ToVector3f();
vertices[vertexIndex] = point;
normals[vertexIndex] = new Vector3(0, 0, -1);
texCoords[vertexIndex] = new Vector2f(point.x / width, point.y / height);
vertexIndex++;
}
Triangulate.Execute(vertices, vertexOffset, vertexOffset + vertexCount, triangles);
vertexOffset += vertexCount;
}
int edgeTriangleIndex = 0;
int vertexOnEdgeIndex = vertexIndex;

for (int i = 0; i < edgesList.Count; i++)
{
List edgePoints = edgesList*;*
int vertexCount = edgePoints.Count;
string test2 = vertexCount.ToString();

Vector3 point1;
Vector3 point2;
for (int j = 0; j < vertexCount - 1; j++)
{
point1 = edgePoints[j].ToVector3f();
point2 = edgePoints[j + 1].ToVector3f();
vertices[vertexIndex + 0] = point1;
vertices[vertexIndex + 2] = point2;
point1.z += depth;
point2.z += depth;
vertices[vertexIndex + 1] = point1;
vertices[vertexIndex + 3] = point2;
Vector3 normal = (point2 - point1).normalized;
normal = new Vector3(normal.y, -normal.x);
normals[vertexIndex + 0] = normal;
normals[vertexIndex + 2] = normal;
normals[vertexIndex + 1] = normal;
normals[vertexIndex + 3] = normal;
edgeTriangles[edgeTriangleIndex + 0] = vertexIndex;
edgeTriangles[edgeTriangleIndex + 1] = vertexIndex + 2;
edgeTriangles[edgeTriangleIndex + 2] = vertexIndex + 1;
edgeTriangles[edgeTriangleIndex + 3] = vertexIndex + 2;
edgeTriangles[edgeTriangleIndex + 4] = vertexIndex + 3;
edgeTriangles[edgeTriangleIndex + 5] = vertexIndex + 1;
vertexIndex += 4;
edgeTriangleIndex += 6;

}
}
triangles.AddRange(edgeTriangles);

mesh.Clear();
mesh.vertices = vertices;
//mesh.normals = normals;
mesh.uv = texCoords;
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();
//mesh.MarkModified();
UpdateColliders(edgesList);
}
private void UpdateColliders(List<List> edgesList)
{
int colliderCount = colliders.Count;
int edgesCount = edgesList.Count;

if (colliderCount < edgesCount)
{
for (int i = edgesCount - colliderCount; i > 0; i–)
{
colliders.Add(gameObject.AddComponent());
}
}
else if (edgesCount < colliderCount)
{
for (int i = colliderCount - 1; i >= edgesCount; i–)
{
Destroy(colliders*);*
colliders.RemoveAt(i);
}
}
for (int i = 0; i < colliders.Count; i++)
{
colliders_.points = edgesList*.ToArray();
}
}
private void OnDrawGizmos()
{
//Vector2 origin = transform.parent.position;
//for (int i = 0; i < edgesList.Count; i++)
//{
// for (int j = 1; j < edgesList.Count; j++)
// {
// Debug.DrawLine(edgesList[j] + origin, edgesList[j - 1] + origin, Color.red);
// }
//}
}
}
[System.Serializable]
public struct Vector2iList {
public List value;
}*_

[System.Serializable]
public struct Vector2iTable {
public List value;
}
private Vector2iTable polygons;