123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- 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<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> 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;
- }
|