// Update is called once per frame
    void Update()
    {
        if (Time.time > nextFragmentTime)
        {
            nextFragmentTime = Time.time + fragmentSendPeriod;

            Color32[] currentImage = MiscUtils.getCurrentCameraImage(cameraRenderTexture, readerTexture);

            currentImage = MathUtils.downSampleImg(currentImage, 2);

            // Protocol:
            //  byte  0: fragment Counter (unsigned)
            //  bytes 1, 2, 3, 4: current total reward (float)
            //  bytes 5..: fragment data
            byte[] buffer = MathUtils.ImageSliceToByteVector(currentImage,
                                                             fragmentCounter * packetLength,
                                                             (fragmentCounter + 1) * packetLength,
                                                             packetLength + 5,
                                                             5);

            buffer[0] = (byte)fragmentCounter;

            float[] totalReward = new float[] { rewardManager.GetTotalReward() };
            System.Buffer.BlockCopy(totalReward,
                                    0,
                                    buffer,
                                    1,             // offset in sent array
                                    4);

            SendBuffer(buffer, targetDestination);
            fragmentCounter = (fragmentCounter + 1) % nFragments;
        }
    }
    private byte[] GetCurrentFragment(int fragmentCounter)
    {
        Color32[] currentImage = MiscUtils.getCurrentCameraImage(cameraRenderTexture, readerTexture);
        currentImage = MathUtils.downSampleImg(currentImage, downSampleFactor);

        // Protocol (hard coded):
        //  byte  0: frame counter
        //  byte  1: fragment ID (unsigned)
        //  bytes 2, 3, 4, 5: current total reward (float)
        //  bytes 6..: fragment data
        //  byte -1: checksum
        byte[] buffer = MathUtils.ImageSliceToByteVector(currentImage,
                                                         fragmentCounter * imgFragmentLength,
                                                         (fragmentCounter + 1) * imgFragmentLength,
                                                         7 + imgFragmentLength + 1,
                                                         7);

        buffer[0] = _frameCounter;
        buffer[1] = (byte)fragmentCounter;

        buffer[2] = (byte)agentRemoteControl.currentAction;

        float[] totalReward = new float[] { rewardManager.GetTotalReward() };
        System.Buffer.BlockCopy(totalReward,
                                0,
                                buffer,
                                3,         // offset in sent array
                                4);

        byte checksum = (byte)MathUtils.GetArraySum(buffer, 0, buffer.Length - 1, 256);

        buffer[buffer.Length - 1] = checksum;
        return(buffer);
    }
 void OnGUI()
 {
     GUI.Label(new Rect(position.x, position.y, 400, 50),
               "Reward: " + rewardManager.GetTotalReward());
     GUI.Label(new Rect(position.x, position.y + 20, 400, 50),
               "Current reward rate: " + rewardManager.GetRewardRate() + " / sec");
     GUI.Label(new Rect(position.x, position.y + 40, 400, 50),
               "Current input action: " + inputListener.currentInputAction);
 }