123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- using System;
- using UnityEngine;
- [ExecuteInEditMode]
- [AddComponentMenu("NGUI/UI/NGUI Unity2D Sprite")]
- public class UI2DSprite : UIBasicSprite
- {
- public Sprite sprite2D
- {
- get
- {
- return this.mSprite;
- }
- set
- {
- if (this.mSprite != value)
- {
- base.RemoveFromPanel();
- this.mSprite = value;
- this.nextSprite = null;
- base.CreatePanel();
- }
- }
- }
- public override Material material
- {
- get
- {
- return this.mMat;
- }
- set
- {
- if (this.mMat != value)
- {
- base.RemoveFromPanel();
- this.mMat = value;
- this.mPMA = -1;
- this.MarkAsChanged();
- }
- }
- }
- public override Shader shader
- {
- get
- {
- if (this.mMat != null)
- {
- return this.mMat.shader;
- }
- if (this.mShader == null)
- {
- this.mShader = Shader.Find("Unlit/Transparent Colored");
- }
- return this.mShader;
- }
- set
- {
- if (this.mShader != value)
- {
- base.RemoveFromPanel();
- this.mShader = value;
- if (this.mMat == null)
- {
- this.mPMA = -1;
- this.MarkAsChanged();
- }
- }
- }
- }
- public override Texture mainTexture
- {
- get
- {
- if (this.mSprite != null)
- {
- return this.mSprite.texture;
- }
- if (this.mMat != null)
- {
- return this.mMat.mainTexture;
- }
- return null;
- }
- }
- public override bool premultipliedAlpha
- {
- get
- {
- if (this.mPMA == -1)
- {
- Shader shader = this.shader;
- this.mPMA = ((!(shader != null) || !shader.name.Contains("Premultiplied")) ? 0 : 1);
- }
- return this.mPMA == 1;
- }
- }
- public override float pixelSize
- {
- get
- {
- return this.mPixelSize;
- }
- }
- 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.mSprite != null && this.mType != UIBasicSprite.Type.Tiled)
- {
- int num5 = Mathf.RoundToInt(this.mSprite.rect.width);
- int num6 = Mathf.RoundToInt(this.mSprite.rect.height);
- int num7 = Mathf.RoundToInt(this.mSprite.textureRectOffset.x);
- int num8 = Mathf.RoundToInt(this.mSprite.textureRectOffset.y);
- int num9 = Mathf.RoundToInt(this.mSprite.rect.width - this.mSprite.textureRect.width - this.mSprite.textureRectOffset.x);
- int num10 = Mathf.RoundToInt(this.mSprite.rect.height - this.mSprite.textureRect.height - this.mSprite.textureRectOffset.y);
- float num11 = 1f;
- float num12 = 1f;
- if (num5 > 0 && num6 > 0 && (this.mType == UIBasicSprite.Type.Simple || this.mType == UIBasicSprite.Type.Filled))
- {
- if ((num5 & 1) != 0)
- {
- num9++;
- }
- if ((num6 & 1) != 0)
- {
- num10++;
- }
- num11 = 1f / (float)num5 * (float)this.mWidth;
- num12 = 1f / (float)num6 * (float)this.mHeight;
- }
- if (this.mFlip == UIBasicSprite.Flip.Horizontally || this.mFlip == UIBasicSprite.Flip.Both)
- {
- num += (float)num9 * num11;
- num3 -= (float)num7 * num11;
- }
- else
- {
- num += (float)num7 * num11;
- num3 -= (float)num9 * num11;
- }
- if (this.mFlip == UIBasicSprite.Flip.Vertically || this.mFlip == UIBasicSprite.Flip.Both)
- {
- num2 += (float)num10 * num12;
- num4 -= (float)num8 * num12;
- }
- else
- {
- num2 += (float)num8 * num12;
- num4 -= (float)num10 * num12;
- }
- }
- float num13;
- float num14;
- if (this.mFixedAspect)
- {
- num13 = 0f;
- num14 = 0f;
- }
- else
- {
- Vector4 vector = this.border * this.pixelSize;
- num13 = vector.x + vector.z;
- num14 = vector.y + vector.w;
- }
- float x = Mathf.Lerp(num, num3 - num13, this.mDrawRegion.x);
- float y = Mathf.Lerp(num2, num4 - num14, this.mDrawRegion.y);
- float z = Mathf.Lerp(num + num13, num3, this.mDrawRegion.z);
- float w = Mathf.Lerp(num2 + num14, num4, this.mDrawRegion.w);
- return new Vector4(x, y, z, w);
- }
- }
- public override Vector4 border
- {
- get
- {
- return this.mBorder;
- }
- set
- {
- if (this.mBorder != value)
- {
- this.mBorder = value;
- this.MarkAsChanged();
- }
- }
- }
- protected override void OnUpdate()
- {
- if (this.nextSprite != null)
- {
- if (this.nextSprite != this.mSprite)
- {
- this.sprite2D = this.nextSprite;
- }
- this.nextSprite = null;
- }
- base.OnUpdate();
- if (this.mFixedAspect)
- {
- Texture mainTexture = this.mainTexture;
- if (mainTexture != null && this.mSprite != null)
- {
- int num = Mathf.RoundToInt(this.mSprite.rect.width);
- int num2 = Mathf.RoundToInt(this.mSprite.rect.height);
- int num3 = Mathf.RoundToInt(this.mSprite.textureRectOffset.x);
- int num4 = Mathf.RoundToInt(this.mSprite.textureRectOffset.y);
- int num5 = Mathf.RoundToInt(this.mSprite.rect.width - this.mSprite.textureRect.width - this.mSprite.textureRectOffset.x);
- int num6 = Mathf.RoundToInt(this.mSprite.rect.height - this.mSprite.textureRect.height - this.mSprite.textureRectOffset.y);
- num += num3 + num5;
- num2 += num6 + num4;
- float num7 = (float)this.mWidth;
- float num8 = (float)this.mHeight;
- float num9 = num7 / num8;
- float num10 = (float)num / (float)num2;
- if (num10 < num9)
- {
- float num11 = (num7 - num8 * num10) / num7 * 0.5f;
- base.drawRegion = new Vector4(num11, 0f, 1f - num11, 1f);
- }
- else
- {
- float num12 = (num8 - num7 / num10) / num8 * 0.5f;
- base.drawRegion = new Vector4(0f, num12, 1f, 1f - num12);
- }
- }
- }
- }
- public override void MakePixelPerfect()
- {
- base.MakePixelPerfect();
- if (this.mType == UIBasicSprite.Type.Tiled)
- {
- return;
- }
- Texture mainTexture = this.mainTexture;
- if (mainTexture == null)
- {
- return;
- }
- if ((this.mType == UIBasicSprite.Type.Simple || this.mType == UIBasicSprite.Type.Filled || !base.hasBorder) && mainTexture != null)
- {
- Rect rect = this.mSprite.rect;
- int num = Mathf.RoundToInt(rect.width);
- int num2 = Mathf.RoundToInt(rect.height);
- if ((num & 1) == 1)
- {
- num++;
- }
- if ((num2 & 1) == 1)
- {
- num2++;
- }
- base.width = num;
- base.height = num2;
- }
- }
- public override void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
- {
- Texture mainTexture = this.mainTexture;
- if (mainTexture == null)
- {
- return;
- }
- Rect rect = (!(this.mSprite != null)) ? new Rect(0f, 0f, (float)mainTexture.width, (float)mainTexture.height) : this.mSprite.textureRect;
- Rect inner = rect;
- Vector4 border = this.border;
- inner.xMin += border.x;
- inner.yMin += border.y;
- inner.xMax -= border.z;
- inner.yMax -= border.w;
- float num = 1f / (float)mainTexture.width;
- float num2 = 1f / (float)mainTexture.height;
- rect.xMin *= num;
- rect.xMax *= num;
- rect.yMin *= num2;
- rect.yMax *= num2;
- inner.xMin *= num;
- inner.xMax *= num;
- inner.yMin *= num2;
- inner.yMax *= num2;
- int size = verts.size;
- base.Fill(verts, uvs, cols, rect, inner);
- if (this.onPostFill != null)
- {
- this.onPostFill(this, size, verts, uvs, cols);
- }
- }
- [HideInInspector]
- [SerializeField]
- private Sprite mSprite;
- [HideInInspector]
- [SerializeField]
- private Material mMat;
- [HideInInspector]
- [SerializeField]
- private Shader mShader;
- [HideInInspector]
- [SerializeField]
- private Vector4 mBorder = Vector4.zero;
- [HideInInspector]
- [SerializeField]
- private bool mFixedAspect;
- [HideInInspector]
- [SerializeField]
- private float mPixelSize = 1f;
- public Sprite nextSprite;
- [NonSerialized]
- private int mPMA = -1;
- }
|