From Schmid.wiki
Jump to: navigation, search

Making Non-serializable Classes Serializable

If you have a non-serializable class from another framework, e.g. UnityEngine.Vector3, you can make it serializable by wrapping it in a serializable class, like this:

using UnityEngine;                                                                     
using System;
                                                                                       
[Serializable]
public class SerializableVector3 {
                                                                                       
    public float x, y, z;
    
    /** Construct from UnityEngine.Vector3 */                                          
    public SerializableVector3(Vector3 v) {
        x = v.x; 
        y = v.y;                                                                       
        z = v.z;
    }   
    
    /** Implicit type conversion. */                                                   
    public static implicit operator SerializableVector3(Vector3 v) {                   
        return new SerializableVector3(v);                                             
    }
        
    /** Implicit type conversion. */                                                   
    public static implicit operator Vector3(SerializableVector3 sv) {                  
        return new Vector3(sv.x, sv.y, sv.z);                                          
    }
    
    public override string ToString() {                                                
        return "[" + x + "," + y + "," + z + "]";                                      
    }                                                                                  
}  

Serialization of Generic Dictionaries

Assuming a one-to-one mapping, you can do this:

* Serialize Keys as a List<KeyType>.
* Serialize Values as a List<ValueType>.