using System; using UnityEngine; [ExecuteInEditMode] [AddComponentMenu("NGUI/UI/NGUI Sprite")] public class UISprite : UIBasicSprite { public override Material material { get { return (!(this.mAtlas != null)) ? null : this.mAtlas.spriteMaterial; } } public UIAtlas atlas { get { return this.mAtlas; } set { if (this.mAtlas != value) { base.RemoveFromPanel(); this.mAtlas = value; this.mSpriteSet = false; this.mSprite = null; if (string.IsNullOrEmpty(this.mSpriteName) && this.mAtlas != null && this.mAtlas.spriteList.Count > 0) { this.SetAtlasSprite(this.mAtlas.spriteList[0]); this.mSpriteName = this.mSprite.name; } if (!string.IsNullOrEmpty(this.mSpriteName)) { string spriteName = this.mSpriteName; this.mSpriteName = string.Empty; this.spriteName = spriteName; this.MarkAsChanged(); } } } } public string spriteName { get { return this.mSpriteName; } set { if (string.IsNullOrEmpty(value)) { if (string.IsNullOrEmpty(this.mSpriteName)) { return; } this.mSpriteName = string.Empty; this.mSprite = null; this.mChanged = true; this.mSpriteSet = false; } else if (this.mSpriteName != value) { this.mSpriteName = value; this.mSprite = null; this.mChanged = true; this.mSpriteSet = false; } } } public bool isValid { get { return this.GetAtlasSprite() != null; } } [Obsolete("Use 'centerType' instead")] public bool fillCenter { get { return this.centerType != UIBasicSprite.AdvancedType.Invisible; } set { if (value != (this.centerType != UIBasicSprite.AdvancedType.Invisible)) { this.centerType = ((!value) ? UIBasicSprite.AdvancedType.Invisible : UIBasicSprite.AdvancedType.Sliced); this.MarkAsChanged(); } } } public override Vector4 border { get { UISpriteData atlasSprite = this.GetAtlasSprite(); if (atlasSprite == null) { return base.border; } return new Vector4((float)atlasSprite.borderLeft, (float)atlasSprite.borderBottom, (float)atlasSprite.borderRight, (float)atlasSprite.borderTop); } } public override float pixelSize { get { return (!(this.mAtlas != null)) ? 1f : this.mAtlas.pixelSize; } } public override int minWidth { get { if (this.type == UIBasicSprite.Type.Sliced || this.type == UIBasicSprite.Type.Advanced) { Vector4 vector = this.border * this.pixelSize; int num = Mathf.RoundToInt(vector.x + vector.z); UISpriteData atlasSprite = this.GetAtlasSprite(); if (atlasSprite != null) { num += atlasSprite.paddingLeft + atlasSprite.paddingRight; } return Mathf.Max(base.minWidth, ((num & 1) != 1) ? num : (num + 1)); } return base.minWidth; } } public override int minHeight { get { if (this.type == UIBasicSprite.Type.Sliced || this.type == UIBasicSprite.Type.Advanced) { Vector4 vector = this.border * this.pixelSize; int num = Mathf.RoundToInt(vector.y + vector.w); UISpriteData atlasSprite = this.GetAtlasSprite(); if (atlasSprite != null) { num += atlasSprite.paddingTop + atlasSprite.paddingBottom; } return Mathf.Max(base.minHeight, ((num & 1) != 1) ? num : (num + 1)); } return base.minHeight; } } public override Vector4 drawingDimensions { get { Vector2 pivotOffset = base.pivotOffset; float num = -pivotOffset.x * (float)this.mWidth; float num2 = -pivotOffset.y * (float)this.mHeight; float num3 = num + (float)this.mWidth; float num4 = num2 + (float)this.mHeight; if (this.GetAtlasSprite() != null && this.mType != UIBasicSprite.Type.Tiled) { int paddingLeft = this.mSprite.paddingLeft; int paddingBottom = this.mSprite.paddingBottom; int num5 = this.mSprite.paddingRight; int num6 = this.mSprite.paddingTop; int num7 = this.mSprite.width + paddingLeft + num5; int num8 = this.mSprite.height + paddingBottom + num6; float num9 = 1f; float num10 = 1f; if (num7 > 0 && num8 > 0 && (this.mType == UIBasicSprite.Type.Simple || this.mType == UIBasicSprite.Type.Filled)) { if ((num7 & 1) != 0) { num5++; } if ((num8 & 1) != 0) { num6++; } num9 = 1f / (float)num7 * (float)this.mWidth; num10 = 1f / (float)num8 * (float)this.mHeight; } if (this.mFlip == UIBasicSprite.Flip.Horizontally || this.mFlip == UIBasicSprite.Flip.Both) { num += (float)num5 * num9; num3 -= (float)paddingLeft * num9; } else { num += (float)paddingLeft * num9; num3 -= (float)num5 * num9; } if (this.mFlip == UIBasicSprite.Flip.Vertically || this.mFlip == UIBasicSprite.Flip.Both) { num2 += (float)num6 * num10; num4 -= (float)paddingBottom * num10; } else { num2 += (float)paddingBottom * num10; num4 -= (float)num6 * num10; } } Vector4 vector = (!(this.mAtlas != null)) ? Vector4.zero : (this.border * this.pixelSize); float num11 = vector.x + vector.z; float num12 = vector.y + vector.w; float x = Mathf.Lerp(num, num3 - num11, this.mDrawRegion.x); float y = Mathf.Lerp(num2, num4 - num12, this.mDrawRegion.y); float z = Mathf.Lerp(num + num11, num3, this.mDrawRegion.z); float w = Mathf.Lerp(num2 + num12, num4, this.mDrawRegion.w); return new Vector4(x, y, z, w); } } public override bool premultipliedAlpha { get { return this.mAtlas != null && this.mAtlas.premultipliedAlpha; } } public UISpriteData GetAtlasSprite() { if (!this.mSpriteSet) { this.mSprite = null; } if (this.mSprite == null && this.mAtlas != null) { if (!string.IsNullOrEmpty(this.mSpriteName)) { UISpriteData sprite = this.mAtlas.GetSprite(this.mSpriteName); if (sprite == null) { return null; } this.SetAtlasSprite(sprite); } if (this.mSprite == null && this.mAtlas.spriteList.Count > 0) { UISpriteData uispriteData = this.mAtlas.spriteList[0]; if (uispriteData == null) { return null; } this.SetAtlasSprite(uispriteData); if (this.mSprite == null) { Debug.LogError(this.mAtlas.name + " seems to have a null sprite!"); return null; } this.mSpriteName = this.mSprite.name; } } return this.mSprite; } protected void SetAtlasSprite(UISpriteData sp) { this.mChanged = true; this.mSpriteSet = true; if (sp != null) { this.mSprite = sp; this.mSpriteName = this.mSprite.name; } else { this.mSpriteName = ((this.mSprite == null) ? string.Empty : this.mSprite.name); this.mSprite = sp; } } public override void MakePixelPerfect() { if (!this.isValid) { return; } base.MakePixelPerfect(); if (this.mType == UIBasicSprite.Type.Tiled) { return; } UISpriteData atlasSprite = this.GetAtlasSprite(); if (atlasSprite == null) { return; } Texture mainTexture = this.mainTexture; if (mainTexture == null) { return; } if ((this.mType == UIBasicSprite.Type.Simple || this.mType == UIBasicSprite.Type.Filled || !atlasSprite.hasBorder) && mainTexture != null) { int num = Mathf.RoundToInt(this.pixelSize * (float)(atlasSprite.width + atlasSprite.paddingLeft + atlasSprite.paddingRight)); int num2 = Mathf.RoundToInt(this.pixelSize * (float)(atlasSprite.height + atlasSprite.paddingTop + atlasSprite.paddingBottom)); if ((num & 1) == 1) { num++; } if ((num2 & 1) == 1) { num2++; } base.width = num; base.height = num2; } } protected override void OnInit() { if (!this.mFillCenter) { this.mFillCenter = true; this.centerType = UIBasicSprite.AdvancedType.Invisible; } base.OnInit(); } protected override void OnUpdate() { base.OnUpdate(); if (this.mChanged || !this.mSpriteSet) { this.mSpriteSet = true; this.mSprite = null; this.mChanged = true; } } public override void OnFill(BetterList verts, BetterList uvs, BetterList cols) { Texture mainTexture = this.mainTexture; if (mainTexture == null) { return; } if (this.mSprite == null) { this.mSprite = this.atlas.GetSprite(this.spriteName); } if (this.mSprite == null) { return; } Rect rect = new Rect((float)this.mSprite.x, (float)this.mSprite.y, (float)this.mSprite.width, (float)this.mSprite.height); Rect rect2 = new Rect((float)(this.mSprite.x + this.mSprite.borderLeft), (float)(this.mSprite.y + this.mSprite.borderTop), (float)(this.mSprite.width - this.mSprite.borderLeft - this.mSprite.borderRight), (float)(this.mSprite.height - this.mSprite.borderBottom - this.mSprite.borderTop)); rect = NGUIMath.ConvertToTexCoords(rect, mainTexture.width, mainTexture.height); rect2 = NGUIMath.ConvertToTexCoords(rect2, mainTexture.width, mainTexture.height); int size = verts.size; base.Fill(verts, uvs, cols, rect, rect2); if (this.onPostFill != null) { this.onPostFill(this, size, verts, uvs, cols); } } [HideInInspector] [SerializeField] private UIAtlas mAtlas; [HideInInspector] [SerializeField] private string mSpriteName; [HideInInspector] [SerializeField] private bool mFillCenter = true; [NonSerialized] protected UISpriteData mSprite; [NonSerialized] private bool mSpriteSet; }