Returns a copy of the array containing Sprite mesh vertex positions.
// Obtain the vertices from the script and modify the position of one of them. Use OverrideGeometry() for this. //Attach this script to a Sprite GameObject //To see the vertices changing, make sure you have your Scene tab visible while in Play mode. //Press the "Draw Debug" Button in the Game tab during Play Mode to draw the shape. Switch back to the Scene tab to see the shape.
using UnityEngine;
public class Example : MonoBehaviour { private SpriteRenderer m_SpriteRenderer; private Rect buttonPos1; private Rect buttonPos2;
void Start() { m_SpriteRenderer = gameObject.GetComponent<SpriteRenderer>(); buttonPos1 = new Rect(10.0f, 10.0f, 200.0f, 30.0f); buttonPos2 = new Rect(10.0f, 50.0f, 200.0f, 30.0f); }
void OnGUI() { //Press this Button to show the Sprite triangles (in the Scene tab) if (GUI.Button(buttonPos1, "Draw Debug")) DrawDebug(); //Press this Button to edit the vertices obtained from the Sprite if (GUI.Button(buttonPos2, "Perform OverrideGeometry")) ChangeSprite(); }
// Show the Sprite triangles void DrawDebug() { Sprite sprite = m_SpriteRenderer.sprite;
ushort[] triangles = sprite.triangles; Vector2[] vertices = sprite.vertices; int a, b, c;
// draw the triangles using grabbed vertices for (int i = 0; i < triangles.Length; i = i + 3) { a = triangles[i]; b = triangles[i + 1]; c = triangles[i + 2];
//To see these you must view the game in the Scene tab while in Play mode Debug.DrawLine(vertices[a], vertices[b], Color.red, 100.0f); Debug.DrawLine(vertices[b], vertices[c], Color.red, 100.0f); Debug.DrawLine(vertices[c], vertices[a], Color.red, 100.0f); } }
// Edit the vertices obtained from the Sprite. Use OverrideGeometry to // submit the changes. void ChangeSprite() { //Fetch the Sprite and vertices from the SpriteRenderer Sprite sprite = m_SpriteRenderer.sprite; Vector2[] spriteVertices = sprite.vertices;
for (int i = 0; i < spriteVertices.Length; i++) { spriteVertices[i].x = Mathf.Clamp( (sprite.vertices[i].x - sprite.bounds.center.x - (sprite.textureRectOffset.x / sprite.texture.width) + sprite.bounds.extents.x) / (2.0f * sprite.bounds.extents.x) * sprite.rect.width, 0.0f, sprite.rect.width);
spriteVertices[i].y = Mathf.Clamp( (sprite.vertices[i].y - sprite.bounds.center.y - (sprite.textureRectOffset.y / sprite.texture.height) + sprite.bounds.extents.y) / (2.0f * sprite.bounds.extents.y) * sprite.rect.height, 0.0f, sprite.rect.height);
// Make a small change to the second vertex if (i == 2) { //Make sure the vertices stay inside the Sprite rectangle if (spriteVertices[i].x < sprite.rect.size.x - 5) { spriteVertices[i].x = spriteVertices[i].x + 5; } else spriteVertices[i].x = 0; } } //Override the geometry with the new vertices sprite.OverrideGeometry(spriteVertices, sprite.triangles); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.