UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Sprite.vertices
# [Sprite](https://docs.unity3d.com/cn/2019.4/ScriptReference/Sprite.html).vertices public Vector2[] **vertices** ; ## 描述 返回包含精灵网格顶点位置的数组的副本。 ``` // 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); } } ```
da
2022年6月21日 11:23
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码