private Mesh SpriteToMesh(SpriteRenderer spriteRenderer)
        {
            var sprite = spriteRenderer.sprite;

            Mesh result = new Mesh();

            result.hideFlags = HideFlags.HideAndDontSave;
            result.Clear();

            //verts
            var verts   = SpriteUtility.GetSpriteMesh(sprite, false);
            var verts3d = new Vector3[verts.Length];

            for (int i = 0; i < verts.Length; i++)
            {
                verts3d [i] = verts [i];
            }
            result.vertices = verts3d;

            // uvs
            result.uv = SpriteUtility.GetSpriteUVs(sprite, false);

            // colors and normals
            var colors  = new Color[result.vertices.Length];
            var normals = new Vector3[result.vertices.Length];

            for (int i = 0; i < colors.Length; i++)
            {
                colors [i]  = spriteRenderer.color;
                normals [i] = Vector3.back;
            }
            result.colors  = colors;
            result.normals = normals;

            // indicies
            var indicies2d = SpriteUtility.GetSpriteIndices(sprite, false);
            var indicies   = new int[indicies2d.Length];

            for (int i = 0; i < indicies.Length; i++)
            {
                indicies [i] = indicies2d [i];
            }

            // finish it up
            result.SetIndices(indicies, MeshTopology.Triangles, 0);
            return(result);
        }
Exemple #2
0
    public static Mesh CreateSpriteMesh(Transform owner, Sprite sprite)
    {
        if (owner != null && sprite != null)
        {
            // Unparent the skin temporarily before adding the mesh
            Transform parent = owner.parent;
            owner.parent = null;

            // Reset the rotation before creating the mesh so the UV's will align properly
            Quaternion localRotation = owner.localRotation;
            owner.localRotation = Quaternion.identity;

            // Reset the scale before creating the mesh so the UV's will align properly
            Vector3 localScale = owner.localScale;
            owner.localScale = Vector3.one;

            Vector2[] vertices2D = SpriteUtility.GetSpriteMesh(sprite, false);
            int[]     indices    = SpriteUtility.GetSpriteIndices(sprite, false).Select(element => (int)element).ToArray();

            // Create the Vector3 vertices
            Vector3[] vertices = new Vector3[vertices2D.Length];
            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0);
            }

            Mesh mesh = new Mesh();
            mesh.vertices  = vertices;
            mesh.triangles = indices;
            Vector2[] uvs = SpriteUtility.GetSpriteUVs(sprite, false);
            mesh.uv = uvs;
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();

            // Reset the rotations of the object
            owner.localRotation = localRotation;
            owner.localScale    = localScale;
            owner.parent        = parent;

            return(mesh);
        }

        return(null);
    }