예제 #1
0
    private void CreateMeshMaskByPolygonCollider2D()
    {
#if UNITY_EDITOR
        //把顶点归一化
        vertices = polygon2d.GetPath(0).ToList();
        if (vertices[0] == vertices[vertices.Count - 1])
        {
            vertices.RemoveAt(vertices.Count - 1);
        }

        Stopwatch sw = new Stopwatch();
        sw.Start();
        TriangleMath tr = new TriangleMath(vertices.ToArray());
        triangles = tr.Triangulate().ToList();
        sw.Stop();
        UnityEngine.Debug.Log("三角化耗时:" + sw.ElapsedMilliseconds + "ms");
#endif
    }
예제 #2
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;
                    //vec.x /= targetT2d.width;
                    //vec.y /= targetT2d.height;
                    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();
                TriangleMath tr = new TriangleMath(mm.vertices.ToArray());
                mm.triangles = tr.Triangulate().ToList();
                sw.Stop();
                UnityEngine.Debug.Log("三角化耗时:" + sw.ElapsedMilliseconds + "ms");

                AssetDatabase.Refresh();
            }
            else
            {
                UnityEngine.Debug.LogError("Image组件还没设置图片");
            }
        }
        else
        {
            UnityEngine.Debug.LogError("选中物体需要有Image组件才能生成PolygonImage");
        }
#endif
    }