Custom Behaviour

ObjectNet provides the facility to create custom behavior and apply it to NetworkObjects.

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

The following piece of code example of how to implement your behavior :

    public struct DataToSync {       
        public Vector3 positionValue;
        public Color color;
    }

    public class BehaviourExample : NetworkEntity<DataToSync, IDataStream> {

        private DataToSync currentValue;

        public BehaviourExample() : base() {
           
        }


        public BehaviourExample(INetworkElement networkObject) : base(networkObject) {
        }


        public override void ComputeActive() {
            this.currentValue.positionValue = this.GetNetworkObject().GetGameObject().transform.position;
            this.currentValue.color         = this.GetNetworkObject().GetGameObject().GetComponent<Renderer>().materials[0].color;
        }


        public override void ComputePassive() {
            this.GetNetworkObject().GetGameObject().transform.position = this.currentValue.positionValue;
            this.GetNetworkObject().GetGameObject().GetComponent<Renderer>().materials[0].color = this.currentValue.color;
        }


        public override DataToSync GetPassiveArguments() {
            return this.currentValue;
        }


        public override void SynchonizePassive(DataToSync data) {
            this.currentValue.color         = data.color;
            this.currentValue.positionValue = data.positionValue;
        }


        public override void SynchonizeActive(IDataStream writer) {
            writer.Write(this.currentValue.positionValue);
            writer.Write(this.currentValue.color);           
        }


        public override void Extract(IDataStream reader) {
            this.currentValue.positionValue = reader.Read<Vector3>();
            this.currentValue.color         = reader.Read<Color>();
        }
    }

After your behavior is created you need to register it on all objects that you need to synchronize information.

    public void Awake() {
       this.GetNetworkElement().RegisterBehavior(new BehaviourExample());
    }

This code tells ObjectNet to include this behavior during the network synchronization procedure.