0
So I’m developing a visual programming system in FSM
, and for serialization, I used Json.Net
, until then, all right, it worked correctly, however, I had to serialize UnityObjects
And things got complicated, I wanted to serialize just the reference in the editor, but he tried to serialize the object.
So I created a JsonConverter
and added to the JsonSettings
, as it should be, however, when serializing, everything worked well, and he serialized, but to deserializar
, he doesn’t even call the method, I believe it’s due to being Serializing a 'int'
, but I’ve tried every conceivable way, and I can’t, I hope someone can shed some light on this.
Code:
public class UObjectConverter : JsonConverter
{
public override bool CanConvert (Type objectType)
{
var uobj = typeof(UnityEngine.Object);
return objectType == uobj || uobj.IsAssignableFrom(objectType);
}
public override void WriteJson (JsonWriter writer, object value, JsonSerializer serializer)
{
var uobj = value as UnityEngine.Object;
int id;
if (uobj == null)
id = 0;
else
id = uobj.GetInstanceID ();
var type = typeof(Object);
if (uobj != null)
type = uobj.GetType ();
writer.WriteValue(id);
}
public override object ReadJson (JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
int id = (int)((long)reader.Value);
if (id == 0)
return null;
else
return MainClass.GetObject(id);
}
}
Well, here is an example of code, before let me explain, within the class 'Aiscriptdata' there is the list with the 'States', in these 'States', there is basically a list with 'Actions' and the name, each 'Action' is basically a type with a virtual method 'Execute()', and derivatives can use their own Fields and etc, when you want to reference a variable or value, you use the class 'Value', which can refer to a 'Variable', or have an 'Object' of own value. 'Variable' has an id('string') and a value('Object'), in addition to a type('Type'). The other list in Aiscriptdata is about 'Variable', basically that’s it. The type 'Darkjson' is just a type I created that uses the Jsonconvert class.
Code:
public AIScriptData script;
public string json;
public void Save()
{
// Até aqui tudo bem, ele serializa corretamente
json = DarkJson.Serialize(script);
}
public void Load()
{
// Aqui ocorre o problema, ele simplesmente deserializa um inteiro
// ao inves do transform, como deveria ser.
script = DarkJson.Deserialize<AIScriptData>(json);
}
Add an example with a Json and the call from
ReadJson()
that you are running.– Leandro Angelo
Added Example Code.
– DiaDeTedio