ObjectNet provides the facility to send and receive simple and complex structured over-network ( see DataStream ).


The main idea of DataStream is to write and read data without the need to write on each place, you can use some DataStream and encapsulate how much data you need.


To implement your own DataStream you can check the API document on NetworkEntity class.


The following piece of code example how to implement your own DataStream :



    public class DataToSync {       
        public Vector3 positionValue;
        public Color color;


  public DataToSync(Vector3 position, Color color) {

    this.positionValue = position;

    this.color = color;

  }
    }


    public class DataStreamExample : DataHandler<Vector3> {
       
        public override int Write(DataToSync data, ref byte[] buffer, ref int offset) {
            int result = base.Write(data.positionValue, ref buffer, ref offset, typeof(Vector3));
            result += base.Write(data.color, ref buffer, ref offset, typeof(Color));
            return result;
        }


        public override DataToSync Read(byte[] buffer, ref int offset) {
            return new DataToSync(this.Read<Vector3>(buffer, ref offset),
                                  this.Read<Color>(buffer, ref offset));
        }
    }


After your data stream is created you need to register into stream factory to be visible to ObjectNet engine.



    DataStream.RegisterGlobalStream<DataToSync, DataStreamExample>();



This code tells to ObjectNet that every time that someone try to send or receive a "DataToSync" object, the "DataStreamExample" must be used.