texture | 要从其获取精灵图形的纹理。 |
rect | 用于精灵的纹理的矩形部分。 |
pivot | 精灵的轴心点(相对于其图形矩形而言)。 |
pixelsPerUnit | 对应世界空间中一个单位的精灵中的像素数。 |
extrude | 精灵网格应向外扩展的数量。 |
meshType | 控制为精灵生成的网格类型。 |
border | 精灵的边框大小(X=左边框,Y=下边框,Z=右边框,W=上边框)。 |
generateFallbackPhysicsShape | 为精灵生成默认的物理形状。 |
创建新的 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 slicing of the Sprite and
is usually used to define Sprite tiling behaviour. The values are in pixel units.
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; } } }
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.