I want to save a list of a custom Type. The Type is basically a "shell" that holds two GameObjects and an int. (A GameObject, a temporary GO that fades in, and a cost)
When I load the list, the GOs are gone, but the int is still there. It's also weird because there are 1 or 2 specific GOs that actually do get saved. I haven't figured out why that is either.
I've created the Type in the EasySave window. I do get "This type has no serializable properties." under the Properties area, which seems like it could be a problem.
It feels like it's saving references, and they got lost along the way. How do have ES3 save the actual GO rather than a reference?
Also fields of Unity reference types will be saved by reference. If you want to save the GameObject by value, you would need to save them (or a List of them) separately.
[System.Serializable]
public class TurretBlueprint
{
[SerializeField]
public GameObject prefab;
[SerializeField]
public GameObject fadeInPrefab;
[SerializeField]
public int cost;
public TurretBlueprint()
{
}
public TurretBlueprint(GameObject prefabGO, GameObject fadeInGO, int costValue)
{
prefab = prefabGO;
fadeInPrefab = fadeInGO;
cost = costValue;
}
}
How would I save the values rather than the references?
Also: why would it save like 1 or 2 of the prefabs but not others???
public void SavePrefabs()
{
ES3.Save<List<SaveObject>>("SaveObjectList", SaveObjects);
}
public void LoadPrefabs()
{
SaveObjects.Clear();
SaveObjects = ES3.Load<List<SaveObject>>("SaveObjectList");
if (SaveObjects.Count > 0)
{
foreach (var prefab in SaveObjects)
{
InstantiatePrefab(prefab._PrefabId, prefab._PrefabPosition, prefab._PrefabRotation);
}
}
}
public GameObject InstantiatePrefab(int PrefabId, Vector3 PrefabPosition, Quaternion PrefabRotation)
{
var go = Instantiate(gamePrefabs[PrefabId], PrefabPosition, PrefabRotation);
return go;
}
}
}
public class SaveObject
{
public int _PrefabId { get; set; }
public Vector3 _PrefabPosition { get; set; }
public Quaternion _PrefabRotation { get; set; }
}
Here is the save file:
{
"SaveObjectList" : {
"__type" : "System.Collections.Generic.List`1[[SaveObject, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]],mscorlib",
"value" : [
{
},{
},{
},{
}
]
}
}
robertstaceyfreeland wrote: ↑Wed Apr 27, 2022 3:48 pm
I think I understand where I went wrong. Obviously Es3 won't accomodate my type. So I broke it up into the components that are accepted.
ps Great Product
Thanks
Robert Freeland
Hi Robert,
Only fields are saved automatically. Your SaveObject class contains properties, not fields, so none of them will be serialised. Adding the [ES3Serializable] attribute to them will allow them to be serialised. For more information please see this guide: