texture | 要从其获取精灵图形的纹理。 |
rect | 用于精灵的纹理的矩形部分。 |
pivot | 精灵的轴心点(相对于其图形矩形而言)。 |
pixelsPerUnit | 对应世界空间中一个单位的精灵中的像素数。 |
extrude | 精灵网格应向外扩展的数量。 |
meshType | 控制为精灵生成的网格类型。 |
border | 精灵的边框大小(X=左边框,Y=下边框,Z=右边框,W=上边框)。 |
generateFallbackPhysicsShape | 为精灵生成默认的物理形状。 |
创建新的 Sprite 对象。
Sprite.Create 创建一个可在游戏应用程序中使用的新 Sprite。
需要加载纹理并将其赋值给 Create,
以便控制新 Sprite 的外观。在下面的脚本示例中,当按下按钮时,
将显示一个新的 Sprite。新精灵在 Start 中创建。\
\
第二个参数 rect
定义使用的子纹理。rect
参数
以纹理的像素定义。Rect(50.0f, 10.0f, 200.0f, 140.0f) 将创建一个如下所述的矩形:
从左到右的范围为 50.0f 到 50.0f + 200.0f = 250.0f。从下到上的范围
为 10.0f 到 10.0f + 140.0f = 150.0f。
第三个参数 pivot
用于确定 Sprite 的中心。这是相对于 rect
的 /Vector2/,其中 Vector2(0.0f, 0.0f) 表示左下角,
Vector2(1.0f, 1.0f) 表示右上角。pixelsPerUnit 值控制
精灵的大小。将该值减少到每世界 100 像素以下
会增大精灵的大小。extrude
值定义围绕 Sprite
的像素数。
如果 Sprite 包含在图集中,这会很有用。meshType
用于选择
使用 FullRect
还是 border
。最后,border
确定
Sprite 的矩形大小。提供 Sprite 时,可以包含其周围的空白。\
\
另请参阅:SpriteRenderer 类。
// 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.