Example #1
0
    private void CreateMeshMaskByImage()
    {
#if UNITY_EDITOR
        GameObject o = Selection.activeGameObject;
        if (o != null && o.GetComponent <Image>() != null)
        {
            Image  img  = o.GetComponent <Image>();
            string path = AssetDatabase.GetAssetPath(img.mainTexture);
            if (!string.IsNullOrEmpty(path))
            {
                TextureImporter         textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
                TextureImporterSettings cacheSettings   = new TextureImporterSettings();
                textureImporter.ReadTextureSettings(cacheSettings);

                //将Texture临时设置为可读写
                TextureImporterSettings tmp = new TextureImporterSettings();
                textureImporter.ReadTextureSettings(tmp);
                tmp.readable = true;
                textureImporter.SetTextureSettings(tmp);
                AssetDatabase.ImportAsset(path);

                SobelEdgeDetection sobel = new SobelEdgeDetection();

                Texture2D targetT2d = sobel.Detect(img.mainTexture as Texture2D);

                List <Vector2> vertices = EdgeUtil.GetPoints(targetT2d);

                for (int i = 0; i < vertices.Count; i++)
                {
                    Vector3 vec = vertices[i];
                    vec.x      -= targetT2d.width * 0.5f;
                    vec.y      -= targetT2d.height * 0.5f;
                    vertices[i] = vec;
                }

                //恢复Texture设置
                textureImporter.SetTextureSettings(cacheSettings);
                AssetDatabase.ImportAsset(path);

                MeshMask          mm        = o.GetComponent <MeshMask>();
                PolygonCollider2D polygon2d = o.GetComponent <PolygonCollider2D>();
                if (mm == null)
                {
                    mm = o.AddComponent <MeshMask>();
                }
                if (polygon2d == null)
                {
                    polygon2d = o.AddComponent <PolygonCollider2D>();
                }

                vertices = vertices.Select(p =>
                {
                    return(new Vector2(p.x / img.mainTexture.width * image.rectTransform.rect.width, p.y / img.mainTexture.height * image.rectTransform.rect.height));
                }).ToList();
                polygon2d.SetPath(0, vertices.ToArray());

                mm.vertices = polygon2d.GetPath(0).ToList();
                if (mm.vertices[0] == mm.vertices[vertices.Count - 1])
                {
                    mm.vertices.RemoveAt(mm.vertices.Count - 1);
                }

                Stopwatch sw = new Stopwatch();
                sw.Start();
                Triangulator tr = new Triangulator(mm.vertices.ToArray());
                mm.triangles = tr.Triangulate().ToList();
                sw.Stop();
                Debug.Log("三角化耗时:" + sw.ElapsedMilliseconds + "ms");

                AssetDatabase.Refresh();
            }
            else
            {
                Debug.LogError("Image组件还没设置图片");
            }
        }
        else
        {
            Debug.LogError("选中物体没有Image组件,请添加");
        }
#endif
    }
Example #2
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        dirty = false;
        rect  = m_Img.rectTransform.rect;
        EditorGUILayout.PropertyField(m_keepOriginSize, true);
        curSize  = m_Img.path.Count;
        showPath = EditorGUILayout.Foldout(showPath, "Path");
        if (showPath)
        {
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.BeginHorizontal();
            EditorGUI.indentLevel = 1;
            EditorGUILayout.LabelField("size", GUILayout.MaxWidth(200));
            int newSize = EditorGUILayout.IntField(curSize);
            if (EditorGUI.EndChangeCheck())
            {
                OnChange();
                if (newSize < 0)
                {
                    newSize = 0;
                }
                if (newSize < curSize)
                {
                    m_Img.path.RemoveRange(newSize, curSize - newSize);
                }
                else if (newSize > curSize)
                {
                    if (newSize > m_Img.path.Capacity)
                    {
                        m_Img.path.Capacity = newSize;
                    }
                    m_Img.path.AddRange(Enumerable.Repeat(new RectPoint(), newSize - curSize));
                }
            }
            EditorGUILayout.EndHorizontal();
            curSize = m_Img.path.Count;
            float f = EditorGUIUtility.labelWidth;
            EditorGUIUtility.labelWidth = 30;
            for (int i = 0; i < curSize; i++)
            {
                RectPointField(i, m_Img.path[i]);
            }
            EditorGUIUtility.labelWidth = f;
        }
        //EditorGUI.EndProperty();

        EditorGUI.indentLevel = 0;
        m_Img.doPolygonUpdate = true;
        //EditorGUILayout.PropertyField(m_edgeTex, true);
        Texture2D tex = EditorGUILayout.ObjectField("EdgeTexture", texMull, typeof(Texture2D), true) as Texture2D;

        if (tex != null)
        {
            string                  path            = AssetDatabase.GetAssetPath(tex.GetInstanceID());
            TextureImporter         textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            TextureImporterSettings cacheSettings   = new TextureImporterSettings();
            textureImporter.ReadTextureSettings(cacheSettings);

            TextureImporterSettings tmp = new TextureImporterSettings();
            textureImporter.ReadTextureSettings(tmp);
            tmp.readable = true;
            textureImporter.SetTextureSettings(tmp);
            AssetDatabase.ImportAsset(path);

            SobelEdgeDetection sobel = new SobelEdgeDetection();
            var targetT2d            = sobel.Detect(tex);

            //Rect rect = rectTransform.rect;

            m_Img.path = EdgeUtil.GetPoints(targetT2d, tex.width, tex.height).Select(v =>
            {
                return(new RectPoint(v.x - tex.width * 0.5f, v.y - tex.height * 0.5f));
            }).ToList();

            textureImporter.SetTextureSettings(cacheSettings);
            AssetDatabase.ImportAsset(path);
            AssetDatabase.Refresh();

            Debug.Log("Edge Detect Done");
            dirty = true;
        }
        this.serializedObject.ApplyModifiedProperties();

        if (dirty)
        {
            EditorUtility.SetDirty(target);
        }
    }