Legacy Documentation: Version 5.6 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Sprite.OverrideGeometry

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public function OverrideGeometry(vertices: Vector2[], triangles: ushort[]): void;
public void OverrideGeometry(Vector2[] vertices, ushort[] triangles);

Parameters

vertices Array of vertex positions in Sprite Rect space.
triangles Array of sprite mesh triangle indices.

Description

Sets up new Sprite geometry.

Vertex positions are in Sprite.rect space meaning from Rect.zero to Rect.size. Pivot offset and transformation to unit space is done automatically.

The size of the triangle array must always be a multiple of 3. The vertices connected to the triangle can be shared by simply indexing into the same vertex.

Sprite UVs are calculated automatically by mapping the provided geometry onto the Sprite texture.

See Also: rect.

In the script example below the polygon shown in the Sprite Editor - Polygon Resizing is used. In this case the sprite has 8 sides. The right-most vertex is moved 50 pixels into the sprite. This is the third vertex.

#pragma strict
// Obtain the vertices from the script and modify the position
// of one of them. Use OverrideGeometry() for this.
private var spriteR: SpriteRenderer;
private var buttonPos1: Rect;
private var buttonPos2: Rect;
function Start() {
	spriteR = 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);
}
function OnGUI() {
	if (GUI.Button(buttonPos1, "Draw Debug"))
		DrawDebug();
	if (GUI.Button(buttonPos2, "Perform OverrideGeometry"))
		ChangeSprite();
}
// Show the sprite triangles
function DrawDebug() {
	var sprite: Sprite = spriteR.sprite;
	//        ushort[]  t = sprite.triangles;
	var t: int[] = sprite.triangles;
	var v: Vector2[] = sprite.vertices;
	var abc: int;
	// draw the triangles using grabbed vertices
	for (var i: int = 0; i < t.Length; i = i + 3) {
		a = t[i];
		b = t[i + 1];
		c = t[i + 2];
		Debug.DrawLine(v[a], v[b], Color.white, 100.0f);
		Debug.DrawLine(v[b], v[c], Color.white, 100.0f);
		Debug.DrawLine(v[c], v[a], Color.white, 100.0f);
	}
}
// submit the changes.
function ChangeSprite() {
	var o: Sprite = spriteR.sprite;
	var sv: Vector2[] = o.vertices;
	for (var i: int = 0; i < sv.Length; i++) {
		sv[i].x = Mathf.Clamp((o.vertices[i].x - o.bounds.center.x - (o.textureRectOffset.x / o.texture.width) + o.bounds.extents.x) / (2.0f * o.bounds.extents.x) * o.rect.width, 0.0f, o.rect.width);
		sv[i].y = Mathf.Clamp((o.vertices[i].y - o.bounds.center.y - (o.textureRectOffset.y / o.texture.height) + o.bounds.extents.y) / (2.0f * o.bounds.extents.y) * o.rect.height, 0.0f, o.rect.height);
		// make a small change to the left-most vertex
		if (i == 2)
			sv[i].x = sv[i].x - 50;
	}
	o.OverrideGeometry(sv, o.triangles);
}
// Obtain the vertices from the script and modify the position
// of one of them. Use OverrideGeometry() for this.
using UnityEngine;

public class ExampleClass : MonoBehaviour { private SpriteRenderer spriteR; private Rect buttonPos1; private Rect buttonPos2;

void Start() { spriteR = 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() { if (GUI.Button(buttonPos1, "Draw Debug")) DrawDebug();

if (GUI.Button(buttonPos2, "Perform OverrideGeometry")) ChangeSprite(); }

// Show the sprite triangles void DrawDebug() { Sprite sprite = spriteR.sprite; // ushort[] t = sprite.triangles; int[] t = sprite.triangles; Vector2[] v = sprite.vertices; int a, b, c;

// draw the triangles using grabbed vertices for (int i = 0; i < t.Length; i = i + 3) { a = t[i]; b = t[i + 1]; c = t[i + 2]; Debug.DrawLine(v[a], v[b], Color.white, 100.0f); Debug.DrawLine(v[b], v[c], Color.white, 100.0f); Debug.DrawLine(v[c], v[a], Color.white, 100.0f); } }

// Edit the vertices obtained from the sprite. Use OverrideGeometry to // submit the changes. void ChangeSprite() { Sprite o = spriteR.sprite; Vector2[] sv = o.vertices;

for (int i = 0; i < sv.Length; i++) { sv[i].x = Mathf.Clamp( (o.vertices[i].x - o.bounds.center.x - (o.textureRectOffset.x / o.texture.width) + o.bounds.extents.x) / (2.0f * o.bounds.extents.x) * o.rect.width, 0.0f, o.rect.width);

sv[i].y = Mathf.Clamp( (o.vertices[i].y - o.bounds.center.y - (o.textureRectOffset.y / o.texture.height) + o.bounds.extents.y) / (2.0f * o.bounds.extents.y) * o.rect.height, 0.0f, o.rect.height);

// make a small change to the left-most vertex if (i == 2) sv[i].x = sv[i].x - 50; }

o.OverrideGeometry(sv, o.triangles); } }