using UnityEngine;
[ExecuteInEditMode]
[AddComponentMenu("NGUI/UI/NGUI Unity2D Sprite (Tight)")]
public class UI2DSpriteTight : UI2DSprite
{
protected override void Awake()
{
base.Awake();
if (sprite2D != null && (!sprite2D.packed || sprite2D.packingMode != SpritePackingMode.Tight))
{
Debug.LogWarning(GetType() + " should generally only be used on tightly packed sprites!");
}
if (type != Type.Simple)
{
type = Type.Simple;
Debug.LogWarning(GetType() + " does not support complex sprite types! Forcing simple type.");
}
}
public override void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color> cols)
{
var spr = sprite2D;
if (spr == null) return;
var colFlat = drawingColor;
var sprUVs = spr.uv;
var sprVerts = spr.vertices;
var tris = spr.triangles;
var texRect = spr.rect;
var flip = FlipScalar;
var size
= new Vector2
(mWidth, mHeight
); var scale
= new Vector2
(size
.x / texRect
.width * flip
.x, size
.y / texRect
.height * flip
.y) * spr
.pixelsPerUnit; var pivotOff = -size / 2F + Vector2.Scale(size, pivotOffset);
if (FixedAspect) scale.x = scale.y = Mathf.Min(scale.x, scale.y);
for (int i = 0; i < tris.Length; ++i)
{
var index = tris[i];
var vertBase = Vector2.Scale(sprVerts[index], scale);
var vert = vertBase - pivotOff;
var uv = sprUVs[index];
var col = mApplyGradient ? (Color.Lerp(mGradientBottom, mGradientTop, (vertBase.y + size.y * 0.5F) / size.y) * color).GammaToLinearSpace() : colFlat;
verts.Add(vert);
uvs.Add(uv);
cols.Add(col);
if (i % 3 == 0)
{
verts.Add(vert);
uvs.Add(uv);
cols.Add(col);
}
}
if (onPostFill != null)
onPostFill(this, verts.size, verts, uvs, cols);
}
private Vector2 FlipScalar
{
get
{
switch (mFlip)
{
case Flip
.Horizontally: return new Vector2
(-1F, 1F
); case Flip
.Vertically: return new Vector2
(1F,
-1F
); case Flip
.Both: return new Vector2
(-1F,
-1F
); default: return Vector2.one;
}
}
}
public override Type type
{
get
{
return Type.Simple;
}
set
{
if (base.type != Type.Simple) base.type = Type.Simple;
}
}
private bool? fixedAspect = null;
protected bool FixedAspect
{
get
{
if (fixedAspect == null)
{
var valueField = GetType().BaseType.GetField("mFixedAspect", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var value = valueField.GetValue(this) as bool?;
fixedAspect = (value == null) ? false : value;
}
return fixedAspect.Value;
}
}
#if UNITY_EDITOR
private Sprite lastSprite;
protected override void OnUpdate()
{
base.OnUpdate();
if (mType != Type.Simple) mType = Type.Simple;
if (Application.isPlaying && sprite2D != lastSprite)
{
lastSprite = sprite2D;
OnInit();
}
}
#endif
}