TheSniperFan / unityserializer-ng

Resurrection of the discontinued Unity Serializer by Mike Talbot from WhyDoIDoIt.com (Unity 5)
MIT License
61 stars 17 forks source link

Game sort of freezes when using LoadSavedLevelFromFile() #30

Closed stm91 closed 8 years ago

stm91 commented 8 years ago

The game successfully loads all gameobjects but seems to fail loading the shaders and gets stuck without throwing any errors.

It does not happen when using SaveGame() and LoadNow().

I know you don't intend to do much work on this other than what you need for your own projects but maybe you could point me in the right direction. =)

stm91 commented 8 years ago

replacing the original function in LevelSerializer.cs with this one fixed it. Not a pretty solution but works for now.

public static void LoadSavedLevelFromFile(string filename)
    {
        var x = File.OpenText(Application.persistentDataPath + "/" + filename);
        var data = (object)x.ReadToEnd();
        x.Close();
        byte[] levelData = null;
        if (data is byte[])
        {
            levelData = (byte[])data;
        }
        if (data is string)
        {
            var str = (string)data;
            if (str.StartsWith("NOCOMPRESSION"))
            {
                levelData = Convert.FromBase64String(str.Substring(13));
            }
            else
            {
                levelData = CompressionHelper.Decompress(str);
            }
        }
        if (levelData == null)
        {
            throw new ArgumentException("data parameter must be either a byte[] or a base64 encoded string");
        }
        //Create a level loader
        var l = new GameObject();
        var loader = l.AddComponent<LevelLoader>();
        loader.showGUI = true;
        var ld = UnitySerializer.Deserialize<LevelSerializer.LevelData>(levelData);
        loader.Data = ld;
        loader.DontDelete = false;
        //Get the loader to do its job
        loader.StartCoroutine(PerformLoad(loader, null));
    }
TheSniperFan commented 8 years ago

I'll look into it. Since I have an exam this Wednesday, it'll have to wait a bit though.

Feel free to post your findings/solutions/problems here so I have as much info as possible when I start.

stm91 commented 8 years ago

Nice, thanks =)

The initial problem of just loading from the file seems to be solved with my solution above but now i get crashes during loading if one of the gameObjects that was part of the save gets destroyed before loading.

stm91 commented 8 years ago

Alright, getting there. In levelLoader.cs at line 478, checking if the obj is null fixed the aforementioned issue.

foreach(var obj in flaggedObjects)
            {

                //old
                //obj.IsDeserializing = false;
                //obj.SendMessage("OnDeserialized", SendMessageOptions.DontRequireReceiver);

                //NEW
                if (obj != null)
                {
                    obj.IsDeserializing = false;
                    obj.SendMessage("OnDeserialized", SendMessageOptions.DontRequireReceiver);

                }
                //NEW
            }

Hope this helps. I'm not too familiar with the whole project yet so i'm not sure if my solutions are "okay" in the big picture.

TheSniperFan commented 8 years ago

Weird...I can't seem to reproduce it here. You'll need to give me instructions on how I can create a test scene, that's as simple as possible, while still reliably producing the problem.

How do I need to set my GameObjects up?

Currently I use two physics cubes, store all their info (including materials) and save/load them using this code.

    private void OnGUI() {
        if (GUILayout.Button("TEST SAVING")) {
            LevelSerializer.SerializeLevelToFile("test");
        }
        if (GUILayout.Button("TEST LOADING")) {
            LevelSerializer.LoadSavedLevelFromFile("test");
        }
    }

I'll need more details, if you want me to fix the issue.

stm91 commented 8 years ago

Thanks for taking the time. I'll see if i can manage to reproduce it in a simple test scene.

stm91 commented 8 years ago

Hey, sorry for not coming back to you earlier. I tried using your latest build and the original issue doesn't occur anymore (still some others remaining), so we can safely assume it was entirely my fault.

I'll close this issue for now and maybe create a new one if i find clearly reproducible errors.