Unity Sprite Mesh Generation

Howdy,

How does Unity generate the meshes for sprites that are marked SpriteMeshType.Tight?

I’m trying to do this at runtime, but I keep ending up with quads.

Essentially, I’m taking an image, building a mask, applying the masked texture (after alpha is applied from said mask) to a texture, then creating a sprite at runtime.

Is this even possible with the built in features? If not, any hints on where I might look to achieve this?

Thanks,
R

It can only be done in the editor; there’s no option to do that at runtime. Also sprite meshes aren’t exposed, so if you want to do that manually you would have to use regular mesh objects.

–Eric

Ah, thanks Eric. I was afraid that wast he case.

That’s too bad, I was hoping to use that to build colliders for the alpha’d shape, but I guess I’ll have to find another method.

Hi

You should be able to achieve what you want with

Sprite.Create(Texture2D texture, Rect rect, Vector2 pivot, float pixelsPerUnit, int extrude, SpriteMeshType meshType)

Leo,

That’s the code I’m currently using (including passing the mesh type).
Still only generates a quad at run time, ignoring the sprite’s alpha.

Hi,

Not sure what might be the problem can you share your code?

Here’s the simple test that I did

        Texture2D t = new Texture2D(256,256);
        Vector2 v = Vector2.zero;
        for(int i = 0; i <256;++i, v.x +=1)
        {
            v.y = 0;
            for(int j = 0; j < 256; ++j, v.y +=1)
            {
                Color c = new Color(1,1,1,1);
                if(v.sqrMagnitude > 400)
                {
                    c.a = 0;
                }
                t.SetPixel(i,j,c);
            }
        }

        SpriteRenderer sp = gameObject.AddComponent<SpriteRenderer>();
        sp.sprite = Sprite.Create (t, new Rect(0,0,256,256),Vector2.zero, 100,0,SpriteMeshType.Tight);

Leo,

Thanks for the response. It got me thinking to challenge the assumptions I was making in my previous response, and indeed, while I was using a shader that masked out the main texture based on a second texture, the alpha of the main texture was not being modified, so that leads me to assume that SpriteMeshCreator relies on the main texture alpha? Either wait, I got it working. Thanks!