Version: 2021.2
言語: 日本語
public static Sprite Create (Texture2D texture, Rect rect, Vector2 pivot, float pixelsPerUnit, uint extrude);
public static Sprite Create (Texture2D texture, Rect rect, Vector2 pivot, float pixelsPerUnit, uint extrude, SpriteMeshType meshType);
public static Sprite Create (Texture2D texture, Rect rect, Vector2 pivot, float pixelsPerUnit, uint extrude, SpriteMeshType meshType, Vector4 border, bool generateFallbackPhysicsShape);
public static Sprite Create (Texture2D texture, Rect rect, Vector2 pivot, float pixelsPerUnit, uint extrude, SpriteMeshType meshType, Vector4 border);
public static Sprite Create (Texture2D texture, Rect rect, Vector2 pivot);
public static Sprite Create (Texture2D texture, Rect rect, Vector2 pivot, float pixelsPerUnit);

パラメーター

texture Sprite のグラフィックスとして適用させるテクスチャ
rect Sprite に適用させるテクスチャの Rect 領域
pivot グラフィックスの Rect に対するピボット地点の相対位置
pixelsPerUnit ワールド空間座標の 1 単位分に相当する、スプライトのピクセル数。
extrude スプライトメッシュが外側に拡張する量
meshType スプライトのために生成されるメッシュタイプの制御
border スプライトのサイズ (X=左、Y=下、Z=右、W=上)
generateFallbackPhysicsShape Generates a default physics shape for the sprite.

説明

Sprite オブジェクトを新規に作成します

Sprite.Create creates a new Sprite which can be used in game applications. A texture needs to be loaded and assigned to Create in order to control how the new Sprite will look. In the script example below a new Sprite is displayed when the button is pressed. The new sprite is created in Start.

The second argument rect defines the sub-texture used. The rect argument is defined in pixels of the texture. A Rect(50.0f, 10.0f, 200.0f, 140.0f) would create a left to right range from 50.0f to 50.0f + 200.0f = 250.0f. The bottom to top range would be 10.0f to 10.0f + 140.0f = 150.0f. The third argument pivot determines what becomes the center of the Sprite. This is a Vector2 relative to the rect where Vector2(0.0f, 0.0f) is the bottom left and Vector2(1.0f, 1.0f) is the top right. The pixelsPerUnit value controls the size of the sprite. Reducing this below 100 pixels per world increases the size of the sprite. The extrude value defines the number of pixels which surround the Sprite. This is useful if the Sprite is included in an atlas. meshType selects whether FullRect or Tight is used. Finally border determines the rectangle size of the Sprite. The Sprite can be provided spaces around it.

See Also: SpriteRenderer class.

// Create a Sprite at start-up.
// Assign a texture to the sprite when the button is pressed.

using UnityEngine;

public class spriteCreate : MonoBehaviour { public Texture2D tex; private Sprite mySprite; private SpriteRenderer sr;

void Awake() { sr = gameObject.AddComponent<SpriteRenderer>() as SpriteRenderer; sr.color = new Color(0.9f, 0.9f, 0.9f, 1.0f);

transform.position = new Vector3(1.5f, 1.5f, 0.0f); }

void Start() { mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f); }

void OnGUI() { if (GUI.Button(new Rect(10, 10, 100, 30), "Add sprite")) { sr.sprite = mySprite; } } }