Version: 2022.2

Sprite.OverrideGeometry

切换到手册
public void OverrideGeometry (Vector2[] vertices, ushort[] triangles);

参数

vertices 精灵 Rect 空间中顶点位置的数组。
triangles 精灵网格三角形索引的数组。

描述

设置新的精灵几何形状。

顶点位置位于 Sprite.rect 空间中,范围为 Rect.zeroRect.size。轴心偏移和单位空间变换是自动完成的。

三角形数组的大小必须始终是 3 的倍数。 通过直接索引到相同顶点可以共享连接到三角形的顶点。

Sprite UVs are calculated automatically by mapping the provided geometry onto the Sprite's Texture.

The Sprite's mesh type will be changed to SpriteMeshType.Tight when the API is called.

See Also: Sprite.rect.

The script example below shows an example on how the API can be used.

// Switch a Sprite's geometry between a triangle and a quad.
using UnityEngine;

public class ExampleClass : MonoBehaviour { private SpriteRenderer m_SpriteRenderer; private Rect m_ButtonPos; void Start() { m_SpriteRenderer = gameObject.AddComponent<SpriteRenderer>(); // Create a blank Texture and Sprite to override later on. var texture2D = new Texture2D(64, 64); m_SpriteRenderer.sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero, 64);

m_ButtonPos = new Rect(10.0f, 10.0f, 200.0f, 30.0f); }

// Use OverrideGeometry to switch the Sprite's mesh to a triangle or quad void ChangeSprite() { Sprite o = m_SpriteRenderer.sprite; if (o.triangles.Length != 3) { var sv = new[] { new Vector2(0, 0), new Vector2(o.textureRect.width, 0), new Vector2(o.textureRect.width * 0.5f, o.textureRect.height), }; var indices = new ushort[] { 0, 1, 2 }; o.OverrideGeometry(sv, indices); } else { var sv = new[] { new Vector2(0, 0), new Vector2(o.textureRect.width, 0), new Vector2(o.textureRect.width, o.textureRect.height), new Vector2(0, o.textureRect.height), }; var indices = new ushort[] { 0, 1, 2, 2, 3, 0 }; o.OverrideGeometry(sv, indices); } }

void OnGUI() { if (GUI.Button(m_ButtonPos, "Perform OverrideGeometry")) ChangeSprite(); } }