void OnEnable()
        {
            Application.runInBackground = true;
            EditorApplication.update   += Update;
            if (VarTracer.Instance != null)
            {
                if (VarTracer.Instance.Graphs.Count == 0)
                {
                    VarTracer.AddChannel();
                }

                bool constainsCamera = VarTracer.Instance.groups.ContainsKey("Camera");
                if (!constainsCamera || VarTracer.Instance.groups["Camera"].VariableDict.Count == 0)
                {
                    //VarTracerHandler.DefineVariable("CameraV_X", "Camera");
                    //VarTracerHandler.DefineVariable("CameraV_Y", "Camera");
                    //VarTracerHandler.DefineVariable("CameraV_Z", "Camera");
                    //VarTracerHandler.DefineVariable("CameraV_T", "Camera");

                    VarTracerHandler.DefineVariable("PlayerV_X", "Player");
                    VarTracerHandler.DefineVariable("PlayerV_Y", "Player");
                    VarTracerHandler.DefineVariable("PlayerV_Z", "Player");
                    VarTracerHandler.DefineVariable("CameraV_T", "Camera");

                    VarTracerHandler.DefineVariable("FPS", "System");

                    VarTracerHandler.DefineEvent("JUMP", "Camera");
                    VarTracerHandler.DefineEvent("ATTACK", "Camera");

                    //VarTracerHandler.DefineVariable("NpcV_X", "Npc");
                    //VarTracerHandler.DefineVariable("NpcV_Y", "Npc");
                    //VarTracerHandler.DefineVariable("NpcV_Z", "Npc");
                    //VarTracerHandler.DefineVariable("NpcV_T", "Npc");
                }
            }
            VarTracer.AddChannel();
        }
        public static void DrawGraphs(Rect rect, EditorWindow window)
        {
            if (VarTracer.Instance)
            {
                CreateLineMaterial();

                mLineMaterial.SetPass(0);

                int graph_index = 0;

                //use this to get the starting y position for the GL rendering
                Rect find_y = EditorGUILayout.BeginVertical(GUIStyle.none);
                EditorGUILayout.EndVertical();

                int currentFrameIndex = VarTracerNet.Instance.GetCurrentFrameFromTimestamp(VarTracerUtils.GetTimeStamp());
                if (m_isPaused)
                {
                    currentFrameIndex = VarTracerNet.Instance.GetCurrentFrameFromTimestamp(VarTracerUtils.StopTimeStamp);
                }

                float scrolled_y_pos = y_offset - mGraphViewScrollPos.y;
                if (Event.current.type == EventType.Repaint)
                {
                    GL.PushMatrix();
                    float start_y = find_y.y;
                    GL.Viewport(new Rect(0, 0, rect.width, rect.height - start_y));
                    GL.LoadPixelMatrix(0, rect.width, rect.height - start_y, 0);

                    //Draw grey BG
                    GL.Begin(GL.QUADS);
                    GL.Color(new Color(0.2f, 0.2f, 0.2f));

                    foreach (KeyValuePair <string, VarTracerGraphItData> kv in VarTracer.Instance.Graphs)
                    {
                        float height = kv.Value.GetHeight();

                        GL.Vertex3(x_offset, scrolled_y_pos, 0);
                        GL.Vertex3(x_offset + mWidth, scrolled_y_pos, 0);
                        GL.Vertex3(x_offset + mWidth, scrolled_y_pos + height, 0);
                        GL.Vertex3(x_offset, scrolled_y_pos + height, 0);

                        scrolled_y_pos += (height + y_gap);
                    }
                    GL.End();

                    scrolled_y_pos = y_offset - mGraphViewScrollPos.y;
                    //Draw Lines
                    GL.Begin(GL.LINES);

                    foreach (KeyValuePair <string, VarTracerGraphItData> kv in VarTracer.Instance.Graphs)
                    {
                        graph_index++;
                        float height = kv.Value.GetHeight();
                        DrawGraphGridLines(scrolled_y_pos, mWidth, height, graph_index == mMouseSelectedGraphNum);

                        foreach (KeyValuePair <string, VarTracerDataInternal> entry in kv.Value.mData)
                        {
                            VarTracerDataInternal g = entry.Value;

                            float y_min   = kv.Value.GetMin(entry.Key);
                            float y_max   = kv.Value.GetMax(entry.Key);
                            float y_range = Mathf.Max(y_max - y_min, 0.00001f);

                            //draw the 0 line
                            if (y_min != 0.0f)
                            {
                                GL.Color(Color.white);
                                float y = scrolled_y_pos + height * (1 - (0.0f - y_min) / y_range);
                                Plot(x_offset, y, x_offset + mWidth, y);
                            }

                            GL.Color(g.mColor);

                            float previous_value = 0, value = 0;
                            int   dataInfoIndex = 0, frameIndex = 0;
                            for (int i = 0; i <= currentFrameIndex; i++)
                            {
                                int dataCount = g.mDataInfos.Count;
                                if (dataCount != 0)
                                {
                                    int   lastFrame = g.mDataInfos[dataCount - 1].FrameIndex;
                                    float lastValue = g.mDataInfos[dataCount - 1].Value;
                                    frameIndex = g.mDataInfos[dataInfoIndex].FrameIndex;

                                    if (dataInfoIndex >= 1)
                                    {
                                        value = g.mDataInfos[dataInfoIndex - 1].Value;
                                    }

                                    if (dataInfoIndex == 0 && i < frameIndex)
                                    {
                                        value = 0;
                                    }

                                    if (i >= frameIndex)
                                    {
                                        while (g.mDataInfos[dataInfoIndex].FrameIndex == frameIndex && dataInfoIndex < dataCount - 1)
                                        {
                                            dataInfoIndex++;
                                        }
                                    }

                                    if (i > lastFrame)
                                    {
                                        value = lastValue;
                                    }
                                }
                                else
                                {
                                    value = 0;
                                }

                                if (i >= 1)
                                {
                                    float x0 = x_offset + (i - 1) * kv.Value.XStep - kv.Value.ScrollPos.x;
                                    if (x0 <= x_offset - kv.Value.XStep)
                                    {
                                        continue;
                                    }
                                    if (x0 >= mWidth + x_offset)
                                    {
                                        break;
                                    }
                                    float y0 = scrolled_y_pos + height * (1 - (previous_value - y_min) / y_range);

                                    if (i == 1)
                                    {
                                        x0 = x_offset;
                                        y0 = scrolled_y_pos + height;
                                    }

                                    float x1 = x_offset + i * kv.Value.XStep - kv.Value.ScrollPos.x;
                                    float y1 = scrolled_y_pos + height * (1 - (value - y_min) / y_range);

                                    if (m_isDrawLine)
                                    {
                                        Plot(x0, y0, x1, y1);
                                    }
                                    else
                                    {
                                        Plot(x0, y0, x0 + 1, y0 + 1);
                                    }
                                }
                                previous_value = value;
                            }
                        }
                        scrolled_y_pos += (height + y_gap);
                    }
                    GL.End();

                    scrolled_y_pos = y_offset - mGraphViewScrollPos.y;
                    scrolled_y_pos = ShowEventLabel(scrolled_y_pos);
                    GL.PopMatrix();

                    GL.Viewport(new Rect(0, 0, rect.width, rect.height));
                    GL.LoadPixelMatrix(0, rect.width, rect.height, 0);
                }

                float allGraphHieght = (VarTracerConst.DefaultChannelHieght + y_gap) * VarTracer.Instance.Graphs.Count - y_gap - mGraphViewScrollPos.y;
                Rect  rec            = new Rect(10, allGraphHieght, 100, 1000);
                GUILayout.BeginArea(rec);
                GUILayout.BeginHorizontal();
                if (GUILayout.Button("+", GUILayout.Width(20), GUILayout.Height(20)))
                {
                    VarTracer.AddChannel();
                }

                if (GUILayout.Button("-", GUILayout.Width(20), GUILayout.Height(20)))
                {
                    VarTracer.RemoveChannel();
                }

                GUILayout.EndHorizontal();
                GUILayout.EndArea();

                mGraphViewScrollPos = EditorGUILayout.BeginScrollView(mGraphViewScrollPos, GUIStyle.none);
                GUILayout.Label("", GUILayout.Height((VarTracerConst.DefaultChannelHieght + y_gap) * VarTracer.Instance.Graphs.Count + y_offset + m_controlScreenHeight));
                graph_index = 0;
                mWidth      = window.position.width - VarTracerConst.NavigationAreaRWidth;
                foreach (KeyValuePair <string, VarTracerGraphItData> kv in VarTracer.Instance.Graphs)
                {
                    graph_index++;
                    float height = kv.Value.GetHeight();
                    float width  = currentFrameIndex * kv.Value.XStep;
                    if (width < mWidth)
                    {
                        width = mWidth - x_offset;
                    }
                    else
                    {
                        if (!m_isPaused)
                        {
                            kv.Value.ScrollPos = new Vector2(width - mWidth, kv.Value.ScrollPos.y);
                        }
                    }

                    GUIStyle s = new GUIStyle();
                    s.fixedHeight  = height + y_gap;
                    s.stretchWidth = true;
                    Rect r = EditorGUILayout.BeginVertical(s);

                    //skip subgraph title if only one, and it's the same.
                    NameLabel.normal.textColor = Color.white;

                    r.height = height + 50;
                    r.width  = width;
                    r.x      = x_offset - 35;
                    r.y      = (height + y_gap) * (graph_index - 1) - 10;

                    if (kv.Value.mData.Count > 0)
                    {
                        GUILayout.BeginArea(r);
                        GUILayout.BeginVertical();
                        for (int i = 0; i < VarTracerConst.Graph_Grid_Row_Num + 1; i++)
                        {
                            GUILayout.Space(6);
                            EditorGUILayout.LabelField("");
                        }
                        GUILayout.BeginHorizontal();
                        kv.Value.ScrollPos = GUILayout.BeginScrollView(kv.Value.ScrollPos, GUILayout.Width(mWidth), GUILayout.Height(0));
                        GUILayout.Label("", GUILayout.Width(width), GUILayout.Height(0));
                        GUILayout.EndScrollView();
                        GUILayout.EndHorizontal();
                        GUILayout.EndVertical();
                        GUILayout.EndArea();
                    }

                    r.width = mWidth + 35;
                    ////Respond to mouse input!
                    if (Event.current.type == EventType.MouseDrag && r.Contains(Event.current.mousePosition - Event.current.delta))
                    {
                        if (Event.current.button == 0)
                        {
                            kv.Value.ScrollPos = new Vector2(kv.Value.ScrollPos.x + Event.current.delta.x, kv.Value.ScrollPos.y);
                        }
                        window.Repaint();
                    }
                    else if (Event.current.type == EventType.MouseDown && r.Contains(Event.current.mousePosition))
                    {
                        mMouseSelectedGraphNum = graph_index;
                    }

                    EditorGUILayout.EndVertical();
                }
                EditorGUILayout.EndScrollView();
            }
        }