void OnGUI()
        {
            if (m_Mesh == null)
            {
                GUILayout.Label("Select a GameObject in the scene with a MeshRenderer");
                return;
            }
            Event e = Event.current;

            m_MousePos    = e.mousePosition;
            m_MousePos.x -= m_ViewPort.x;
            m_MousePos.y -= m_ViewPort.y;
            var UV = (m_MousePos - m_Offset) / m_Width;

            UV.y = 1.0f - UV.y;

            var TexArea = new Rect(m_Offset.x, m_Offset.y, m_Width, m_Width);

            if (e.type == EventType.Layout)
            {
                m_Texture = m_Submesh.m_Material.mainTexture;
            }

            GUILayout.BeginHorizontal();
            for (int i = 0; i < m_Submeshes.Count; i++)
            {
                if (SelectButton("Submesh_" + i, m_Submesh == m_Submeshes[i]))
                {
                    m_Submesh = m_Submeshes[i];
                }
            }
            if (GUILayout.Toggle(m_ShowTriangleList, "TriangleList", "Button", GUILayout.ExpandWidth(false)))
            {
                m_ShowTriangleList = true;
                SceneView.RepaintAll();
            }
            else
            {
                m_ShowTriangleList = false;
                SceneView.RepaintAll();
            }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();

            DrawZoomGUI();
            m_DrawLines     = GUILayout.Toggle(m_DrawLines, "DrawLines", "Button", GUILayout.Width(100));
            m_DrawTriangles = GUILayout.Toggle(m_DrawTriangles, "DrawTriangles", "Button", GUILayout.Width(100));
            if (GUILayout.Button("" + m_TextureMode))
            {
                while (true)
                {
                    m_TextureMode = m_TextureMode + 1;
                    if ((int)m_TextureMode > 6)
                    {
                        m_TextureMode = 0;
                        break;
                    }

                    if (("" + m_TextureMode).Length > 2)
                    {
                        break;
                    }
                }
            }

            bool tmpWrap = GUILayout.Toggle(m_ShowWrap, "Wrap", "Button", GUILayout.Width(50));

            if (tmpWrap != m_ShowWrap)
            {
                if (e.button == 0)
                {
                    m_ShowWrap = tmpWrap;
                }
                else
                {
                    m_WrapCount = 5;
                }
            }
            if (GUILayoutUtility.GetLastRect().Contains(e.mousePosition) && e.type == EventType.ScrollWheel)
            {
                if (e.delta.y < 0)
                {
                    m_WrapCount = Mathf.Max(m_WrapCount - 1, 0);
                }
                else
                {
                    m_WrapCount = Mathf.Min(m_WrapCount + 1, 5000);
                }
            }

            m_ClearBackGround = GUILayout.Toggle(m_ClearBackGround, "ClearBK", "Button", GUILayout.Width(60));
            m_BackGroundColor = EditorGUILayout.ColorField(m_BackGroundColor, GUILayout.Width(50));

            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            var oldColor = GUI.color;

            for (int i = 0; i < m_UV.Count; i++)
            {
                if (m_UV[i] == null)
                {
                    GUI.color = new Color(1, 0.6f, 0.6f, 0.8f);
                    GUILayout.Label("No uv" + i, "Button");
                }
                else
                {
                    GUI.color = oldColor;
                    if (SelectButton("uv" + i, m_CurrentUVSet == i))
                    {
                        m_CurrentUVSet = i;
                    }
                }
            }
            GUI.color = oldColor;
            GUILayout.Label("Front:", GUILayout.Width(50));
            m_FrontFaceColor = EditorGUILayout.ColorField(m_FrontFaceColor, GUILayout.Width(50));
            GUILayout.Label("Back:", GUILayout.Width(50));
            m_BackFaceColor = EditorGUILayout.ColorField(m_BackFaceColor, GUILayout.Width(50));

            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            GUILayout.Label("Offset: " + m_Offset);
            GUILayout.Label("viewSize: " + m_ViewPort);

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            var R = GUILayoutUtility.GetRect(50, 10000, 50, 10000);

            if (m_ShowTriangleList)
            {
                DrawTriangleList();
            }
            GUILayout.EndHorizontal();
            if (e.type != EventType.Layout)
            {
                m_ViewPort = R;
            }

            if (e.type == EventType.MouseDrag && GUIUtility.hotControl == 0)
            {
                m_Offset += e.delta;
                MyRepaint();
            }
            if (e.type == EventType.MouseMove)
            {
                Repaint();
            }

            if (e.type == EventType.Repaint)
            {
                Drawing.EditorWindowViewport(position, m_ViewPort);
                if (m_ClearBackGround)
                {
                    GL.Clear(true, true, m_BackGroundColor);
                }
                if (m_Texture != null)
                {
                    var old = m_Texture.filterMode;

                    if (m_TextureMode != TextureMode.HideTexture)
                    {
                        if (((int)m_TextureMode & 4) != 0)
                        {
                            m_Texture.filterMode = FilterMode.Bilinear;
                        }
                        else
                        {
                            m_Texture.filterMode = FilterMode.Point;
                        }
                        if (m_ShowWrap)
                        {
                            Rect Rtmp   = TexArea;
                            var  center = Rtmp.center;
                            Rtmp.width  *= 1 + m_WrapCount * 2;
                            Rtmp.height *= 1 + m_WrapCount * 2;
                            Rtmp.center  = center;
                            GUI.color    = new Color(1, 1, 1, 0.5f * m_BackGroundColor.a);
                            Graphics.DrawTexture(Rtmp, m_Texture, new Rect(-m_WrapCount, -m_WrapCount, 1 + m_WrapCount * 2, 1 + m_WrapCount * 2), 0, 0, 0, 0, GUI.color, m_TextureMapAlphaOnly);
                        }
                        GUI.color = new Color(1, 1, 1, /*0.5f */ m_BackGroundColor.a);
                        Graphics.DrawTexture(TexArea, m_Texture, new Rect(0, 0, 1, 1), 0, 0, 0, 0, GUI.color, m_TextureMapNormal);
                    }
                    m_Texture.filterMode = old;
                }

                Drawing.BeginGL(new Color(1, 1, 1, 0.2f), GL.LINES);
                if (m_ShowWrap)
                {
                    for (int i = -m_WrapCount; i <= m_WrapCount + 1; i++)
                    {
                        GL.Vertex(new Vector2(m_Offset.x + m_Width * i, 0));
                        GL.Vertex(new Vector2(m_Offset.x + m_Width * i, 10000));
                        GL.Vertex(new Vector2(0, m_Offset.y + m_Width * i));
                        GL.Vertex(new Vector2(10000, m_Offset.y + m_Width * i));
                    }
                }
                GL.Color(new Color(1, 1, 0, 0.2f));
                for (int i = 0; i < 2; i++)
                {
                    GL.Vertex(new Vector2(m_Offset.x + m_Width * i, 0));
                    GL.Vertex(new Vector2(m_Offset.x + m_Width * i, 10000));
                    GL.Vertex(new Vector2(0, m_Offset.y + m_Width * i));
                    GL.Vertex(new Vector2(10000, m_Offset.y + m_Width * i));
                }
                GL.End();

                if (m_CurrentUVSet >= 0)
                {
                    if (m_DrawLines)
                    {
                        var tmpRand = new System.Random(5);
                        Drawing.BeginGL(Color.red, GL.LINES);
                        for (int i = 0; i < m_Submesh.m_Indices.Length; i += 3)
                        {
                            GL.Color(new Color((float)tmpRand.NextDouble(), (float)tmpRand.NextDouble(), (float)tmpRand.NextDouble()));
                            var P1 = m_UV[m_CurrentUVSet][m_Submesh.m_Indices[i + 0]];
                            var P2 = m_UV[m_CurrentUVSet][m_Submesh.m_Indices[i + 1]];
                            var P3 = m_UV[m_CurrentUVSet][m_Submesh.m_Indices[i + 2]];
                            P1.y = 1.0f - P1.y;
                            P2.y = 1.0f - P2.y;
                            P3.y = 1.0f - P3.y;
                            P1   = Vector2.Scale(P1, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);
                            P2   = Vector2.Scale(P2, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);
                            P3   = Vector2.Scale(P3, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);
                            GL.Vertex(P1);
                            GL.Vertex(P2);
                            GL.Vertex(P2);
                            GL.Vertex(P3);
                            GL.Vertex(P3);
                            GL.Vertex(P1);
                        }
                        GL.End();
                    }

                    if (m_DrawTriangles)
                    {
                        var tmpRand = new System.Random(5);
                        Drawing.BeginGL(new Color(1, 0, 0, 0.2f), GL.TRIANGLES);
                        for (int i = 0; i < m_Submesh.m_Indices.Length; i += 3)
                        {
                            var P1 = m_UV[m_CurrentUVSet][m_Submesh.m_Indices[i + 0]];
                            var P2 = m_UV[m_CurrentUVSet][m_Submesh.m_Indices[i + 1]];
                            var P3 = m_UV[m_CurrentUVSet][m_Submesh.m_Indices[i + 2]];
                            P1.y = 1.0f - P1.y;
                            P2.y = 1.0f - P2.y;
                            P3.y = 1.0f - P3.y;
                            P1   = Vector2.Scale(P1, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);
                            P2   = Vector2.Scale(P2, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);
                            P3   = Vector2.Scale(P3, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);
                            var FColor = m_FrontFaceColor;
                            FColor  *= 0.5f + (float)(tmpRand.NextDouble() * 0.5);
                            FColor.a = m_FrontFaceColor.a;
                            var BColor = m_BackFaceColor;
                            BColor  *= 0.5f + (float)(tmpRand.NextDouble() * 0.5);
                            BColor.a = m_BackFaceColor.a;
                            GL.Color(FColor);
                            GL.Vertex(P1);
                            GL.Vertex(P2);
                            GL.Vertex(P3);

                            GL.Color(BColor);

                            GL.Vertex(P2);
                            GL.Vertex(P1);
                            GL.Vertex(P3);
                        }
                        GL.End();
                    }

                    if (m_SelectedTriangle >= 0 && m_SelectedTriangle < m_Triangles.Length)
                    {
                        Drawing.BeginGL(new Color(1, 0, 0, 0.2f), GL.TRIANGLES);

                        var P1 = m_UV[m_CurrentUVSet][m_Triangles[m_SelectedTriangle * 3 + 0]];
                        var P2 = m_UV[m_CurrentUVSet][m_Triangles[m_SelectedTriangle * 3 + 1]];
                        var P3 = m_UV[m_CurrentUVSet][m_Triangles[m_SelectedTriangle * 3 + 2]];
                        P1.y = 1.0f - P1.y;
                        P2.y = 1.0f - P2.y;
                        P3.y = 1.0f - P3.y;
                        P1   = Vector2.Scale(P1, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);
                        P2   = Vector2.Scale(P2, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);
                        P3   = Vector2.Scale(P3, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);

                        GL.Color(new Color(0, 0, 1, 0.5f));

                        GL.Vertex(P2);
                        GL.Vertex(P1);
                        GL.Vertex(P3);

                        GL.Color(new Color(1, 0, 1, 0.5f));

                        GL.Vertex(P1);
                        GL.Vertex(P2);
                        GL.Vertex(P3);

                        GL.End();
                    }

                    if (m_SelectedVertex >= 0 && m_Submesh != null && m_Submesh.m_Indices != null)
                    {
                        m_CurrentUVSet   = Mathf.Clamp(m_CurrentUVSet, 0, m_UV.Count - 1);
                        m_SelectedVertex = Mathf.Clamp(m_SelectedVertex, 0, m_Submesh.m_Indices.Length - 1);
                        var P1 = m_UV[m_CurrentUVSet][m_Submesh.m_Indices[m_SelectedVertex]];
                        P1.y = 1.0f - P1.y;
                        P1   = Vector2.Scale(P1, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);

                        Drawing.BeginGL(new Color(1, 0, 0, 0.2f), GL.QUADS);

                        GL.Color(new Color(1, 1, 1, 0.7f));

                        GL.Vertex(P1 - Vector2.up * 5 + Vector2.right * 5);
                        GL.Vertex(P1 + Vector2.up * 5 + Vector2.right * 5);
                        GL.Vertex(P1 + Vector2.up * 5 - Vector2.right * 5);
                        GL.Vertex(P1 - Vector2.up * 5 - Vector2.right * 5);

                        GL.End();
                    }
                    if (m_SelectedVertTriangle >= 0 && m_Submesh != null && m_Submesh.m_Indices != null)
                    {
                        m_CurrentUVSet         = Mathf.Clamp(m_CurrentUVSet, 0, m_UV.Count - 1);
                        m_SelectedVertTriangle = Mathf.Clamp(m_SelectedVertTriangle, 0, m_Submesh.m_Indices.Length / 3 - 1);
                        var P1 = m_UV[m_CurrentUVSet][m_Submesh.m_Indices[m_SelectedVertTriangle * 3 + 0]];
                        var P2 = m_UV[m_CurrentUVSet][m_Submesh.m_Indices[m_SelectedVertTriangle * 3 + 1]];
                        var P3 = m_UV[m_CurrentUVSet][m_Submesh.m_Indices[m_SelectedVertTriangle * 3 + 2]];
                        P1.y = 1.0f - P1.y;
                        P2.y = 1.0f - P2.y;
                        P3.y = 1.0f - P3.y;
                        P1   = Vector2.Scale(P1, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);
                        P2   = Vector2.Scale(P2, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);
                        P3   = Vector2.Scale(P3, new Vector2(TexArea.width, TexArea.height)) + new Vector2(TexArea.x, TexArea.y);

                        Drawing.BeginGL(new Color(1, 0, 0, 0.2f), GL.QUADS);

                        GL.Color(new Color(1, 1, 1, 0.7f));

                        GL.Vertex(P1 - Vector2.up * 5 + Vector2.right * 5);
                        GL.Vertex(P1 + Vector2.up * 5 + Vector2.right * 5);
                        GL.Vertex(P1 + Vector2.up * 5 - Vector2.right * 5);
                        GL.Vertex(P1 - Vector2.up * 5 - Vector2.right * 5);

                        GL.Vertex(P2 - Vector2.up * 5 + Vector2.right * 5);
                        GL.Vertex(P2 + Vector2.up * 5 + Vector2.right * 5);
                        GL.Vertex(P2 + Vector2.up * 5 - Vector2.right * 5);
                        GL.Vertex(P2 - Vector2.up * 5 - Vector2.right * 5);

                        GL.Vertex(P3 - Vector2.up * 5 + Vector2.right * 5);
                        GL.Vertex(P3 + Vector2.up * 5 + Vector2.right * 5);
                        GL.Vertex(P3 + Vector2.up * 5 - Vector2.right * 5);
                        GL.Vertex(P3 - Vector2.up * 5 - Vector2.right * 5);

                        GL.End();
                    }
                }

                if (e.control)
                {
                    Drawing.BeginGL(new Color(0, 1, 0, 0.5f), GL.LINES);
                    GL.Vertex(m_MousePos + Vector2.up * 7);
                    GL.Vertex(m_MousePos - Vector2.up * 7);
                    GL.Vertex(m_MousePos + Vector2.right * 7);
                    GL.Vertex(m_MousePos - Vector2.right * 7);
                    m_CurrentIndex = FindClosestVertex(UV);
                    if (m_CurrentIndex >= 0)
                    {
                        var p = m_UV[m_CurrentUVSet][m_CurrentIndex];
                        p.y = 1.0f - p.y;
                        var pDraw = p * m_Width + m_Offset;

                        GL.Color(new Color(1, 1, 1, 0.8f));
                        GL.Vertex(pDraw + Vector2.up * 70);
                        GL.Vertex(pDraw - Vector2.up * 70);
                        GL.Vertex(pDraw + Vector2.right * 70);
                        GL.Vertex(pDraw - Vector2.right * 70);
                        GL.Vertex(m_MousePos);
                        GL.Vertex(pDraw);

                        GL.Color(new Color(1, 1, 1, 0.3f));
                        foreach (var vert in Drawing.GetCircleLinePoints(m_MousePos, (pDraw - m_MousePos).magnitude, 60))
                        {
                            GL.Vertex(vert);
                        }
                        GL.End();
                        GUI.color = new Color(1, 1, 1, 0.8f);
                        GUI.Label(new Rect(m_MousePos.x + 10, m_MousePos.y - 25 - 2, 200, 25), "closest U:" + p.x.ToString("0.0000") + " V:" + p.y.ToString("0.0000"), "box");
                    }
                    else
                    {
                        GL.End();
                    }

                    GUI.Label(new Rect(m_MousePos.x + 10, m_MousePos.y + 2, 200, 25), "mouse U:" + UV.x.ToString("0.0000") + " V:" + UV.y.ToString("0.0000"), "box");
                    EditorGUIUtility.AddCursorRect(m_ViewPort, (MouseCursor)(-1));
                    SceneView.RepaintAll();
                }
            }
            else if (e.type == EventType.KeyDown)
            {
                if (m_Submesh != null && m_Submesh.m_Indices != null)
                {
                    if (e.keyCode == KeyCode.UpArrow)
                    {
                        if (m_SelectedVertex >= 0)
                        {
                            m_SelectedVertex = Mathf.Clamp(m_SelectedVertex - 3, 0, m_Submesh.m_Indices.Length - 1);
                        }
                        if (m_SelectedVertTriangle >= 0)
                        {
                            m_SelectedVertTriangle = Mathf.Clamp(m_SelectedVertTriangle - 1, 0, m_Submesh.m_Indices.Length / 3 - 1);
                        }
                    }
                    else if (e.keyCode == KeyCode.DownArrow)
                    {
                        if (m_SelectedVertex >= 0)
                        {
                            m_SelectedVertex = Mathf.Clamp(m_SelectedVertex + 3, 0, m_Submesh.m_Indices.Length - 1);
                        }
                        if (m_SelectedVertTriangle >= 0)
                        {
                            m_SelectedVertTriangle = Mathf.Clamp(m_SelectedVertTriangle + 1, 0, m_Submesh.m_Indices.Length / 3 - 1);
                        }
                    }
                    else if (e.keyCode == KeyCode.LeftArrow)
                    {
                        if (m_SelectedVertex >= 0)
                        {
                            m_SelectedVertex = Mathf.Clamp(m_SelectedVertex - 1, 0, m_Submesh.m_Indices.Length - 1);
                        }
                    }
                    else if (e.keyCode == KeyCode.RightArrow)
                    {
                        if (m_SelectedVertex >= 0)
                        {
                            m_SelectedVertex = Mathf.Clamp(m_SelectedVertex + 1, 0, m_Submesh.m_Indices.Length - 1);
                        }
                    }
                    MyRepaint();
                }
            }
            if (GUI.changed)
            {
                MyRepaint();
            }
        }
 void UpdateMesh()
 {
     if (m_TempCollider != null)
     {
         DestroyImmediate(m_TempCollider);
     }
     m_Mesh = null;
     try
     {
         var GO = Selection.activeGameObject;
         if (GO == null)
         {
             return;
         }
         // do not tinker with prefab assets.
         if (PrefabUtility.IsPartOfPrefabAsset(GO))
         {
             return;
         }
         var MF = GO.GetComponent <MeshFilter>();
         if (MF != null)
         {
             m_Mesh = MF.sharedMesh;
         }
         else
         {
             var SMR = GO.GetComponent <SkinnedMeshRenderer>();
             if (SMR != null)
             {
                 m_Mesh = SMR.sharedMesh;
             }
         }
         if (m_Mesh == null)
         {
             return;
         }
         m_Renderer = GO.GetComponent <Renderer>();
         if (GO.GetComponent <MeshCollider>() == null)
         {
             m_TempCollider           = GO.AddComponent <MeshCollider>();
             m_TempCollider.hideFlags = HideFlags.HideAndDontSave | HideFlags.HideInInspector;
         }
         if (m_Renderer == null)
         {
             return;
         }
         var materials = m_Renderer.sharedMaterials;
         m_Submeshes.Clear();
         for (int i = 0; i < m_Mesh.subMeshCount; i++)
         {
             var SM = new UV_SubMesh();
             m_Submeshes.Add(SM);
             SM.m_Indices  = m_Mesh.GetIndices(i);
             SM.m_Material = materials[i % materials.Length];
         }
         m_Triangles = m_Mesh.triangles;
         m_Vertices  = m_Mesh.vertices;
         m_Normals   = m_Mesh.normals;
         m_UV.Clear();
         if (m_Mesh.uv != null && m_Mesh.uv.Length > 0)
         {
             m_UV.Add(m_Mesh.uv);
         }
         else
         {
             m_UV.Add(null);
         }
         if (m_Mesh.uv2 != null && m_Mesh.uv2.Length > 0)
         {
             m_UV.Add(m_Mesh.uv2);
         }
         else
         {
             m_UV.Add(null);
         }
         if (m_Mesh.uv3 != null && m_Mesh.uv3.Length > 0)
         {
             m_UV.Add(m_Mesh.uv3);
         }
         else
         {
             m_UV.Add(null);
         }
         if (m_Mesh.uv4 != null && m_Mesh.uv4.Length > 0)
         {
             m_UV.Add(m_Mesh.uv4);
         }
         else
         {
             m_UV.Add(null);
         }
         m_CurrentUVSet = -1;
         for (int i = 0; i < m_UV.Count; i++)
         {
             if (m_UV[i] != null)
             {
                 m_CurrentUVSet = i;
                 break;
             }
         }
         m_Submesh              = m_Submeshes[0];
         m_Texture              = m_Submesh.m_Material.mainTexture;
         m_SelectedVertex       = -1;
         m_SelectedVertTriangle = -1;
     }
     finally
     {
         MyRepaint();
     }
 }