Saving Prefabs Manually
Posted: Thu Aug 27, 2020 12:59 pm
Hello,
I have a problem when I save GameObjects. I save and load GameObjects using ES3Prefab component and it works, but the problem is my prefab hierarchy is large and ES3 saves all components on prefab so my save file becomes huge. Most of that data is unnecessary. I tried to save prefabs data manually, but was unable to load references to other prefabs.
I was testing with simple prefab with component which has public transform field. At runtime I instantiate two of those prefabs A and B. And add reference B to A.
Then I save both of those objects components into separate keys.
Then I want to instantiate what I have saved. I manually instantiate prefabs and load data into them.
But references are not loaded. As I understand it is because saved components ids doesn't exist in the scene.
Is there any possibility to save and load references manually.
I have a problem when I save GameObjects. I save and load GameObjects using ES3Prefab component and it works, but the problem is my prefab hierarchy is large and ES3 saves all components on prefab so my save file becomes huge. Most of that data is unnecessary. I tried to save prefabs data manually, but was unable to load references to other prefabs.
I was testing with simple prefab with component which has public transform field. At runtime I instantiate two of those prefabs A and B. And add reference B to A.
Then I save both of those objects components into separate keys.
Code: Select all
public void Save()
{
for (var i = 0; i < gameObjects.Length; i++)
{
var go = gameObjects[i];
var es3Prefab = go.GetComponent<ES3Prefab>().prefabId;
var reference = go.GetComponent<ReferenceToObject_Test>();
ES3.Save($"PrefabID_{i}", es3Prefab, @"Saves\ReferenceTest.es3");
ES3.Save($"Transform_{i}", go.transform, @"Saves\ReferenceTest.es3");
ES3.Save($"Reference_{i}", reference, @"Saves\ReferenceTest.es3");
}
}
Code: Select all
public void Load()
{
var keys = ES3.GetKeys(@"Saves\ReferenceTest.es3");
var prefabKeys = keys.Where(key => key.StartsWith("PrefabID_")).ToArray();
var transformKeys = keys.Where(key => key.StartsWith("Transform_")).ToArray();
var referenceKeys = keys.Where(key => key.StartsWith("Reference_")).ToArray();
for (var i = 0; i < prefabKeys.Length; i++)
{
var id = ES3.Load<long>(prefabKeys[i], @"Saves\ReferenceTest.es3");
var prefab = referenceMgr.prefabs.Where(es3Prefab => es3Prefab.prefabId == id)
.Select(es3Prefab => es3Prefab.gameObject).First();
var go = Instantiate(prefab);
ES3.LoadInto(transformKeys[i], @"Saves\ReferenceTest.es3", go.transform);
ES3.LoadInto(referenceKeys[i], @"Saves\ReferenceTest.es3", go.GetComponent<ReferenceToObject_Test>());
referenceMgr.Add(go, go.GetInstanceID());
}
}
Is there any possibility to save and load references manually.