vertices | 精灵 Rect 空间中顶点位置的数组。 |
triangles | 精灵网格三角形索引的数组。 |
设置新的精灵几何形状。
顶点位置位于 Sprite.rect 空间中,范围为 Rect.zero
到 Rect.size。轴心偏移和单位空间变换是自动完成的。
三角形数组的大小必须始终是 3 的倍数。
通过直接索引到相同顶点可以共享连接到三角形的顶点。
通过将提供的几何形状映射到精灵纹理来自动计算精灵 UV。
另请参阅:rect。
下面的脚本示例
使用了 Sprite Editor - Polygon Resizing 中显示的多边形。在本例中,
精灵有 8 条边。将最右侧的顶点移动到精灵内部 50 像素处。
此为第三个顶点。
// 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>(); // Create a blank Texture and Sprite to override later on. var texture2D = new Texture2D(64, 64); spriteR.sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero, 100);
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; 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 3rd vertex if (i == 2) sv[i].x = sv[i].x - 50; }
o.OverrideGeometry(sv, o.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.