- Home /
Issues when using Unity Serializer
Hello i'm using the 3rd part plugin "Unity Serializer" Which is available on the asset store and all people are pointing to it, my problem is that I can't get this plugin to serialize my class, i don't need to add the stuff in the editor ... I just need Storage.serializetostring(myObject). That's all.
So i downloaded the asset extracted it and i just imported the whydoidoit which i believe contains the necessary classes for serializing, After that i went to Visual Studio and marked the property of the class i wanted to serializable with "SerializeThis", but when i call Storage.serializeToString(myObject) it always return a constant string, I tried to create a new class with just properties of int and string thinking that the problem might be the properties of my class (GameObject, lists ...) but SerializeToString always returning the same value no matter what object is passed.
I think i should add some code snippets my C# code will be better than my English :D :
TestClass.cs:
using UnityEngine;
using System.Collections;
public class TestClass : MonoBehaviour
{
// Use this for initialization
public int score { get; set; }
public string Name { get; set; }
public string Message { get; set; }
}
SerializationHelper.cs : this class is static and allow me to use the Unity Serializer class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Serialization;
using Assets.Scripts;
public static class StorageHelper
{
public static void SaveLevel(Level level)
{
TestClass test = new TestClass();
test.score = 10;
test.Name = "reda";
test.Message = "Hello";
var repre = Storage.SerializeToString(test);
Debug.Log(repre);
}
}
If you guys know what's the problem here, please share your knowledge, also am new to here so if I did a mistake I will edit it, Thanks in advance.
Answer by Bunny83 · Apr 28, 2015 at 09:43 PM
I haven't really used this serializer yet, but what i can tell right away is that you're doing something fundamentally wrong ^^. Your class is derived from MonoBehaviour. That makes this class a component. Components can't be created with "new". You should actually get a warning about that.
Yeah... Also, don't use Unity Serializer. It was a decent asset years ago, but support has been dropped and the website with forum full of valuable information as well as the API has been down for a few weeks. Critical bugs were never corrected, once your project gets a little bit more complex, with lots of gameobject references between classes, you will get errors that corrupt your save files (been there, done that). However, the LevelSerializer.cs script is an excellent source for your own serialization efforts, if you decide to go down that route.
Top: The SerializeField function at the very bottom are nowhere used in US, but they nevertheless provide a good starting point. Combine it with ISerializationSurrogates and you have a simple and robust serialization system.
Your answer
Follow this Question
Related Questions
Using UniFileBrowser and LoginPro to upload file = zero bytes uploaded? 0 Answers
How to protect JSON file game data? 3 Answers
Is it possible to use DataContractJsonSerializer? 2 Answers
IO Exception: Sharing Violation on Path,IOExeption Sharing Violation on Path 0 Answers
Save and Load all spawned objects in their position using array 1 Answer