Search Unity

Serialize an EditorWindow

Discussion in 'Editor & General Support' started by zhutianlun810, Oct 8, 2019.

  1. zhutianlun810

    zhutianlun810

    Joined:
    Sep 17, 2017
    Posts:
    171
    Hello,

    I am looking for a solution to serialize Action. This is a tutorial I found:
    https://schemingdeveloper.com/2014/12/04/serializing-delegates-unity/
    I am following the instruction above and then I met some problem.
    This is the class which contains an Action:
    Code (CSharp):
    1. [Serializable]
    2. public class ConnectionPoint: ISerializationCallbackReceiver
    3. {
    4.     public byte[] dataOnClickCo;
    5.     public Rect rect;
    6.  
    7.     public ConnectionPointType type;
    8.  
    9.     public TreeNode node;
    10.  
    11.     public GUIStyle style;
    12.  
    13.     public Action<ConnectionPoint> OnClickConnectionPoint;
    14. ...(Some Methods)
    15.  
    16.     public void OnBeforeSerialize() {
    17.         using (var stream = new MemoryStream()) {
    18.             //Write to a memory stream
    19.             (new BinaryFormatter()).Serialize(stream, OnClickConnectionPoint);
    20.             stream.Flush();
    21.             //Store the information to a byte array
    22.             dataOnClickCo = stream.ToArray();
    23.         }
    24.      
    25.     }
    26.  
    27.     public void OnAfterDeserialize()
    28.     {
    29.         using (var stream = new MemoryStream(dataOnClickCo)) {
    30.             OnClickConnectionPoint = (Action<ConnectionPoint>)(new BinaryFormatter()).Deserialize(stream);
    31.         }
    32.     }
    33. }
    34.  
    And the action is from my EditorWindow class:

    [Serializable]
    Code (CSharp):
    1. public class NodeBasedEditor : EditorWindow{
    2.  
    3. ...
    4.  
    5.     public void OnClickConnectPoint(ConnectionPoint inPoint)
    6.     {
    7.         selectedInPoint = inPoint;
    8.  
    9.         if (selectedOutPoint != null)
    10.         {
    11.             if (selectedOutPoint.node != selectedInPoint.node)
    12.             {
    13.                 CreateConnection();
    14.                 ClearConnectionSelection();
    15.             }
    16.             else
    17.             {
    18.                 ClearConnectionSelection();
    19.             }
    20.         }
    21.     }
    22. }
    23.  
    Then I got the following error:
    SerializationException: Type UnityEditor.EditorWindow in assembly UnityEditor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable.

    And I found the OnBeforeSerialize() is called automatically for some reason?
     
    Last edited: Oct 8, 2019