Hi i would like to see some working example of Entity serialization and deserialization.
Here is my current not working code. I would apreciate any help.
public class SaveSystem : MonoBehaviour
{
ReferencedUnityObjects g;
private void Update()
{
if (Input.GetMouseButtonDown(1))
{
save();
}
if (Input.GetMouseButtonDown(2))
{
load();
}
}
public void save()
{
EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
using (var writer = new StreamBinaryWriter(Application.dataPath + "/save"))
{
SerializeUtilityHybrid.Serialize(entityManager, writer, out g);
print("save");
}
}
public void load()
{
EntityManager main = World.DefaultGameObjectInjectionWorld.EntityManager;
World world = new World("svet");
EntityManager entityManager = world.EntityManager;
using (var reader = new StreamBinaryReader(Application.dataPath + "/save"))
{
SerializeUtilityHybrid.Deserialize(entityManager, reader, g);
print("load");
}
}
}
Glad you figured it out. In the future though you’re likely to get more help with ECS questions when posting in its own forum where all the ECS people hang out.
It would be fair to explain how you did it. When people find your thread years later it provides no value for them. So if you find a solution to your own question it is considered forum-netiquette to answer your own question and not just say “it works now”. A forum is meant for exchange of information. If you expect information just to flow in your direction you should not expect other people be willing to help you. So please consider to elaborate on your “solution” and help other people in the future.
using UnityEngine;
using Unity.Entities;
using Unity.Entities.Serialization;
public class SaveSystem : MonoBehaviour
{
ReferencedUnityObjects g;
Data dataHold;
private void Start()
{
dataHold = new Data();
g = new ReferencedUnityObjects();
}
private void Update()
{
if (Input.GetMouseButtonDown(1))
{
save();
}
if (Input.GetMouseButtonDown(2))
{
load();
}
}
public void save()
{
EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
using (var writer = new StreamBinaryWriter(Application.dataPath + "/save"))
{
SerializeUtilityHybrid.Serialize(entityManager, writer, out g);
print("save");
}
dataHold.Array = g.Array;
var data = JsonUtility.ToJson(dataHold);
PlayerPrefs.SetString("Data", data);
}
public void load()
{
var data = PlayerPrefs.GetString("Data");
dataHold = JsonUtility.FromJson<Data>(data);
g.Array = dataHold.Array;
EntityManager main = World.DefaultGameObjectInjectionWorld.EntityManager;
World world = new World("svet");
EntityManager entityManager = world.EntityManager;
using (var reader = new StreamBinaryReader(Application.dataPath + "/save"))
{
SerializeUtilityHybrid.Deserialize(entityManager, reader, g);
print("load");
}
main.MoveEntitiesFrom(entityManager);
}
}
public class Data
{
public Object[] Array;
}