Version: 2018.1 (switch to 2018.2b or 2017.4)
LanguageEnglish
  • C#
  • JS

Script language

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

Sprite.Create

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 static method Create(texture: Texture2D, rect: Rect, pivot: Vector2, pixelsPerUnit: float = 100.0f, extrude: uint = 0, meshType: SpriteMeshType = SpriteMeshType.Tight, border: Vector4 = Vector4.zero, generateFallbackPhysicsShape: bool = false): Sprite;
public static Sprite Create(Texture2D texture, Rect rect, Vector2 pivot, float pixelsPerUnit = 100.0f, uint extrude = 0, SpriteMeshType meshType = SpriteMeshType.Tight, Vector4 border = Vector4.zero, bool generateFallbackPhysicsShape = false);

Parameters

texture Texture from which to obtain the sprite graphic.
rect Rectangular section of the texture to use for the sprite.
pivot Sprite's pivot point relative to its graphic rectangle.
pixelsPerUnit The number of pixels in the sprite that correspond to one unit in world space.
extrude Amount by which the sprite mesh should be expanded outwards.
meshType Controls the type of mesh generated for the sprite.
border The border sizes of the sprite (X=left, Y=bottom, Z=right, W=top).
generateFallbackPhysicsShape Generates a default physics shape for the sprite.

Description

Create a new Sprite object.

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 and is specified in pixels. These are relative to the size generated by rect. 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.

#pragma strict
// Create a Sprite at start-up.
// Assign a texture to the sprite when the button is pressed.
public class spriteCreate extends MonoBehaviour {
	public var tex: Texture2D;
	private var mySprite: Sprite;
	private var sr: SpriteRenderer;
	function 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);
	}
	function Start() {
		mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
	}
	function OnGUI() {
		if (GUI.Button(new Rect(10, 10, 100, 30), "Add sprite")) {
			sr.sprite = mySprite;
		}
	}
}
// 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; } } }

Did you find this page useful? Please give it a rating: