Can anyone give me an example of how to Serialize a Custom Data Type?

Options
I have learned c# through Unity and thus have not had any experience with serialization. I'm trying to serialize a basic class to get a feel for how to do this.

The class that I am trying to serialize is called BaseMovement and is just two Vector2s.

The first Vector2 is called CurrentPosition and the second is called Desired Position. I would like to serialize this class and send it over the network. I realize that Vector2s themselves are supported but in this scenario I'm just trying to learn how to do this for the future so I can send more complicated classes. Can someone provide me with an example of how I would go about serializing this class so I can get a better of idea of how this is done?

Thank you!

Comments

  • This is an example from my previous project,
    it's here to help you see how I've made it. Hopefully it can help you
    using System;
    using ExitGames.Client.Photon;
    using UnityEngine;
    
    public class Edge
    {
        #region Serializable
    
        // 3 times 4 bytes (int are 4 bytes)
        public static readonly byte[] memEdge = new byte[3 * 4];
    
        /// <summary>
        /// Register this class to be serializable by Photon Engine
        /// </summary>
        public static void RegisterSerializable()
        {
            PhotonPeer.RegisterType(typeof(Edge), (byte)'E', SerializeEdge, DeserializeEdge);
        }
    
        public static short SerializeEdge(StreamBuffer outStream, object customobject)
        {
            Edge c = (Edge)customobject;
            lock (memEdge) {
                byte[] bytes = memEdge;
                int index = 0;
                Protocol.Serialize(c.x, bytes, ref index);
                Protocol.Serialize(c.y, bytes, ref index);
                Protocol.Serialize((int)c.type, bytes, ref index);
                outStream.Write(bytes, 0, 3 * 4);
            }
    
            return 3 * 4;
        }
    
        private static object DeserializeEdge(StreamBuffer inStream, short length)
        {
            Edge c = new Edge();
            lock (memEdge) {
                inStream.Read(memEdge, 0, 3 * 4);
                int index = 0;
                Protocol.Deserialize(out c.x, memEdge, ref index);
                Protocol.Deserialize(out c.y, memEdge, ref index);
                Protocol.Deserialize(out int outType, memEdge, ref index);
                c.type = (EdgeType)outType;
            }
    
            return c;
        }
    
        #endregion
    
        public int x;
        public int y;
        public EdgeType type;
    
        public enum EdgeType : int
        {
            Straight,
            TopLeft, TopRight, BottomLeft, BottomRight,
            Unknown
        }
    
        private Edge() { }
    
        public Edge(int x, int y, EdgeType type)
        {
            this.x = x;
            this.y = y;
            this.type = type;
        }
    }
    
    

    Then I call Edge.RegisterSerializable() early as possible (in some Awake method on my game manager)