How we can implement our own Serialization system for serializing components, MonoBehaviours, Game Objects, ScriptableObjects, …?
Unity serialization system does not supports serialization of Dictionary and such useful stuff, so how we can add support for custom types?
For example, i want to serialize the Boolean value myself, with my own serialization implementation, how can i do it?
A workaround for Serializing Dictionary is to serialize the keys and values separately as Lists, but i want to do the serialization of the dictionary as unity does for Lists.
The example you posted is using the workaround i was said.
I want to control the serialization, like serializing to Binary.
EDIT: I want to Really serialize the dictionary, not the fake of it.
Im not sure what you mean. If you want to do your own serialization you will need to do some very heavy lifting-code using pure .NET.
The way you have worded the question however leads me to believe you dont fully understand C# generic collections so Im not sure thats where you want to start.
The way you have written this makes it sound like you think serialization is somehow the same as what draws the values in the inspector? It is not, that serializaiton is just what saves it to the disk (or loads it). The inspector code is what reads the types and decides how to draw it.
The question is what are you trying to do? If you want stuff serialized like unity does, then just serialize it using .NET and then make a custom inspector. Otherwise if you spefically need to do some process to the data as it serializses (in which case the inspector has nothing to do with what you want?) then you need to write your own serializaiton, which is nothing to do with unity and you will already know how to do using pure c#. If your not sure where to start with this, then take it from me, this is not what you should be doing. EDIT: If for some reason you still think you need this: https://docs.microsoft.com/en-us/dotnet/standard/serialization/custom-serialization
EDIT2: Honestly though I have used unity for over 7 years, and I can tell you that circumventing its serialization system will lead to headaches. Unity already has enough problems handling its serialization and not losing values when you depend on it, if you try and force it not to its going to be hell. Bare in mind assets are not the same as scene objects so you will need to sort out serialization for project bound assets too etc… Food for thought.
Excuse me for bad explanation.
I mean, i want to serialize any dictionary type and show it in the inspector, so it needs a custom serialization, while Unity can handle this Serialization, i don’t know why they aren’t adding support for dictionaries, but anyway, i want to add support for custom types, such as dictionary, if i want to display, i know how to do it using PropertyDrawers and Custom Editors, but what about serialization?
Again what i sent to you will serialize any dictionary type. You are talking about serialization as if it is drawing in the inspector.
Let me explain again:
Serializing is the process of turning the data into some standard data and saving it on the harddrive, usually binary or text.
The inspector simply draws stuff under the hood using property drawers etc to draw some of the “basic” data types.
If you want it to do this for dicitonaries, you need to add a property drawer or custom editor.
I believe i have explained it well but just in case it still has not made sense: you can view custom data types such as custom dictionaries, or even dicitonaries that contain dicitonaries, using a custom editor or property drawer. That is the way every single asset does it.
The Script you sent me is serializing keys and values separately, not as Key Value pairs.
Unity uses YAML for serializing data, so as i said, i am looking for a solution to control the serialization of a specified type, such as Converters in the JSON.Net library.
I have made serialization libraries and i know how data is serialized and deserialized, also, i have added support for custom types to my serialization libraries, for example, if my library does not supports your custom data, you can add support for it, in this case, unity is missing dictionary type, so i want to add this data type support to unity.
The plugins that you have stated are using the same method that is used in the script you have posted, but i am looking for something similar to Converter in the JSON.Net library.
Right, you probably should have just said that in the first place?
Also if you understand serialization well, perhaps dont write such confusing posts that make it seem like your yet another new-to-scripting user who is confusing serialization with the OnInspectorGUI functionality.
Yes I know you have made serialization libraries, thats also why I am still confused as to what you dont understand how to do? I am aware the script did different pairs, I assumed you would upgrade it not just copy and paste it! Obviously it will need you to edit it to use the format you like (such as keyvalue pair or whatever you want)?
I havent stated any actual plugins so not sure what your talking about, but I can tell you that if you want to change the way unity serializes stuff automatically, its unlikely to be possible without some access to internal callbacks.
Either way, it would still be done by an editor script, I am not sure how many ways I can say this.
I understand what you are trying to do. What I am saying is that even if you want to write your own YAML serializer , its code will still reside in an editor script in order to get the relevant callbacks.
The confusing question is because of my language, my native language is not English, so i can speak well.
Let me explain it by code, i want to make unity to serialize dictionary:
public class DictionarySerializer : IUnitySerializer {
public void Serialize (ISerializer serializer, object value) {
IEnumerator e = (value as IEnumerable).GetEnumerator ();
Type valueType = value.GetType ();
Type keyValuePairType = typeof(KeyValuePair<,>).MakeGenericType (value.GetGenericArguments ());
PropertyInfo keyProperty = keyValuePairType .GetProperty ( "Key" );
PropertyInfo valueProperty = keyValuePairType .GetProperty ( "Value" );
while (e.MoveNext()) {
Serialize ( keyProperty.GetValue ( value, null ) );
Serialize ( valueProperty.GetValue ( value, null ) );
}
}
}
By doing this i would be able to serialize any dictionary value and then i could show it in the custom editor without using any keys or values collection:
public IDictionary _dictionary;
void OnEnable () {
_dictionary = (target as MyClass).dictionary;
}
public void OnInspectorGUI () {
foreach ( var item in _dictionary ) {
item.Key = EditorGUILayout.TextField ( item.Key );
item.Value = EditorGUILayout.TextField ( item.Value );
}
}
This is definitely doable, I have not tried myself but I remember that there is a GitHub repo somewhere that has a bunch of custom serialization examples for unity, although I cannot for the life of me remember the link to the repo. I recommend searching through GitHub though because there is definitely a few there that are pretty functional!
If I find what the repo was called I will post back in here, but if anyone else reading this knows the repo I am talking about please commend in this thread with the details!