OverrideGeometry not working.

I’m trying to modify a sprite’s geometry but whatever parameters I set, OverrideGeometry always throws this exception:

Invalid vertex array. Some vertices are outside of the Sprite rectangle: (1358.000000, 579.000000).
UnityEngine.Sprite:OverrideGeometry(Vector2[ ], UInt16[ ])

To discard any errors on my algorithm, I modified it to make an exact copy of the original vertices and rectangle size, but even coping the original values causes the exception to be thrown.

I did some research and found out this bug was found on Unity 2017.2.0p2,and it was reported to be fixed on 2018.2… however I’m using 2019.2, and still experiencing the same problem.

However… the problem is that you need to handle two different metrics, this is a problem not because you need to do some calculations to make this function work, but because you if you do a simple A=B and then B=A you get an error, and that should not happen. The value returned by Sprite.vertices gives you world units while OverrideGeometry requires pixels. That inconsistency can be the source for lots frustration, complaints and errors.

Hi!

Even after reading this I’m still having problems dealing with the sprite vertices, and I always get this same error.

Have you found any solutions?

Below answer correct. It’s also a good idea to clamp within sprite.rect width/height to avoid potential error message.

I’ve got the same error, took me a while to figure out it was because the negative values (due to world units).

In case someone is having the same error, this is how you convert vertices from units to pixels

Vector2[] verts = sprite.vertices;
for (int i = 0; i < verts.Length; ++i)
    verts[i] = (verts[i] * sprite.pixelsPerUnit) + sprite.pivot;
sprite.OverrideGeometry(verts, sprite.triangles);
4 Likes