private InstructionWindow GetInstructionWindow(MInstruction instruction)
        {
            InstructionWindow window = new InstructionWindow()
            {
                StartIndex  = -1,
                EndIndex    = -1,
                Instruction = instruction
            };

            for (int i = 0; i < this.record.Frames.Count; i++)
            {
                CoSimulationFrame frame = this.record.Frames[i];

                if (frame.Instructions.Contains(instruction.ID))
                {
                    if (window.StartIndex == -1)
                    {
                        window.StartIndex = i;
                    }


                    if (window.EndIndex == -1 || window.EndIndex < i)
                    {
                        window.EndIndex = i;
                    }


                    MSimulationResult result = frame.MergedResult;

                    if (result.Events != null && result.Events.Count > 0)
                    {
                        foreach (MSimulationEvent simEvent in result.Events)
                        {
                            if (simEvent.Reference == instruction.ID)
                            {
                                window.Events.Add(i);
                                window.RawEvents.Add(simEvent);
                                break;
                            }
                        }
                    }
                }
            }



            return(window);
        }
        private void OnGUI()
        {
            if (GUI.Button(new Rect(sliderOffsetX, Screen.height - 45, 100, 40), "Start recording"))
            {
                this.SourceAvatar.CoSimulator.Recording = true;
            }

            if (record != null)
            {
                if (GUI.Button(new Rect(sliderOffsetX + 220, Screen.height - 45, 100, 40), "Save"))
                {
                    string outputPath = EditorUtility.SaveFilePanel("Select the output file", "", "record", "cosimRecord");

                    try
                    {
                        byte[] data = MMICSharp.Common.Communication.Serialization.SerializeBinary <CoSimulationRecord>(this.record);

                        System.IO.File.WriteAllBytes(outputPath, data);
                    }
                    catch (System.Exception)
                    {
                    }
                }
            }

            if (GUI.Button(new Rect(sliderOffsetX + 330, Screen.height - 45, 100, 40), "Load"))
            {
                string filePath = EditorUtility.OpenFilePanel("Select the file to be loaded", "", "cosimRecord");

                try
                {
                    byte[] data = System.IO.File.ReadAllBytes(filePath);


                    this.record             = MMICSharp.Common.Communication.Serialization.DeserializeBinary <CoSimulationRecord>(data);
                    this.instructionWindows = new List <InstructionWindow>();

                    //Disable the simulation controller
                    GameObject.FindObjectOfType <SimulationController>().enabled = false;


                    //Add the instruction windows
                    foreach (MInstruction instruction in this.record.Instructions)
                    {
                        InstructionWindow w = this.GetInstructionWindow(instruction);

                        if (w.StartIndex == -1)
                        {
                            continue;
                        }

                        if (w.EndIndex == -1)
                        {
                            w.EndIndex = this.record.Frames.Count - 1;
                        }


                        this.instructionWindows.Add(w);
                    }
                }
                catch (System.Exception)
                {
                }
            }


            if (this.SourceAvatar.CoSimulator != null && this.SourceAvatar.CoSimulator.Recording)
            {
                if (GUI.Button(new Rect(sliderOffsetX + 110, Screen.height - 45, 100, 40), "Stop"))
                {
                    this.record = this.SourceAvatar.CoSimulator.GetRecord();
                    this.SourceAvatar.CoSimulator.Recording = false;
                    this.SourceAvatar.GetComponentInParent <SimulationController>().enabled = false;



                    //Create the instrution windows



                    foreach (MInstruction instruction in this.record.Instructions)
                    {
                        InstructionWindow w = this.GetInstructionWindow(instruction);

                        if (w.StartIndex == -1)
                        {
                            continue;
                        }

                        if (w.EndIndex == -1)
                        {
                            w.EndIndex = this.record.Frames.Count - 1;
                        }


                        this.instructionWindows.Add(w);
                    }
                }
            }

            //Only display if a record has been loaded -> To do provide a way to load records from the file system
            if (this.record != null)
            {
                //Update the slider
                sliderValue = GUI.HorizontalSlider(new Rect(sliderOffsetX, Screen.height - 60, Screen.width - (sliderOffsetX * 2), 20), sliderValue, 0, (float)this.record.Frames.Count);

                //Estimate the current index
                frameIndex = (int)sliderValue;

                //Get the current frame
                currentFrame = this.record.Frames[frameIndex];

                //The amount of elements in the present frame
                this.HierachyElements = currentFrame.Results.Count + currentFrame.CoSimulationSolverResults.Count;

                //Handle if out of range
                if (currentHierachyIndex < -1 || currentHierachyIndex > HierachyElements)
                {
                    currentHierachyIndex = -1;
                }


                currentInstruction = null;


                if (currentHierachyIndex == -1)
                {
                    this.SourceAvatar.AssignPostureValues(currentFrame.MergedResult.Posture);
                }
                else if (currentHierachyIndex == 0)
                {
                    this.SourceAvatar.AssignPostureValues(currentFrame.Initial);
                }
                else
                {
                    if (currentHierachyIndex <= currentFrame.Results.Count)
                    {
                        this.SourceAvatar.AssignPostureValues(currentFrame.Results[currentHierachyIndex - 1].Posture);
                        currentInstruction = GetInstructionByID(currentFrame.Instructions[currentHierachyIndex - 1]);
                    }

                    else
                    {
                        this.SourceAvatar.AssignPostureValues(currentFrame.CoSimulationSolverResults[currentHierachyIndex - currentFrame.Instructions.Count - 1].Posture);
                    }
                }



                GUI.color = Color.red;

                this.ShowInstructionWindows();
            }
        }