123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- [AddComponentMenu("NGUI/UI/Atlas")]
- public class UIAtlas : MonoBehaviour
- {
- public Material spriteMaterial
- {
- get
- {
- return (!(this.mReplacement != null)) ? this.material : this.mReplacement.spriteMaterial;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.spriteMaterial = value;
- }
- else if (this.material == null)
- {
- this.mPMA = 0;
- this.material = value;
- }
- else
- {
- this.MarkAsChanged();
- this.mPMA = -1;
- this.material = value;
- this.MarkAsChanged();
- }
- }
- }
- public bool premultipliedAlpha
- {
- get
- {
- if (this.mReplacement != null)
- {
- return this.mReplacement.premultipliedAlpha;
- }
- if (this.mPMA == -1)
- {
- Material spriteMaterial = this.spriteMaterial;
- this.mPMA = ((!(spriteMaterial != null) || !(spriteMaterial.shader != null) || !spriteMaterial.shader.name.Contains("Premultiplied")) ? 0 : 1);
- }
- return this.mPMA == 1;
- }
- }
- public List<UISpriteData> spriteList
- {
- get
- {
- if (this.mReplacement != null)
- {
- return this.mReplacement.spriteList;
- }
- if (this.mSprites.Count == 0)
- {
- this.Upgrade();
- }
- return this.mSprites;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.spriteList = value;
- }
- else
- {
- this.mSprites = value;
- }
- }
- }
- public Texture texture
- {
- get
- {
- return (!(this.mReplacement != null)) ? ((!(this.material != null)) ? null : this.material.mainTexture) : this.mReplacement.texture;
- }
- }
- public float pixelSize
- {
- get
- {
- return (!(this.mReplacement != null)) ? this.mPixelSize : this.mReplacement.pixelSize;
- }
- set
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.pixelSize = value;
- }
- else
- {
- float num = Mathf.Clamp(value, 0.25f, 4f);
- if (this.mPixelSize != num)
- {
- this.mPixelSize = num;
- this.MarkAsChanged();
- }
- }
- }
- }
- public UIAtlas replacement
- {
- get
- {
- return this.mReplacement;
- }
- set
- {
- UIAtlas uiatlas = value;
- if (uiatlas == this)
- {
- uiatlas = null;
- }
- if (this.mReplacement != uiatlas)
- {
- if (uiatlas != null && uiatlas.replacement == this)
- {
- uiatlas.replacement = null;
- }
- if (this.mReplacement != null)
- {
- this.MarkAsChanged();
- }
- this.mReplacement = uiatlas;
- if (uiatlas != null)
- {
- this.material = null;
- }
- this.MarkAsChanged();
- }
- }
- }
- public UISpriteData GetSprite(string name)
- {
- if (this.mReplacement != null)
- {
- return this.mReplacement.GetSprite(name);
- }
- if (!string.IsNullOrEmpty(name))
- {
- if (this.mSprites.Count == 0)
- {
- this.Upgrade();
- }
- if (this.mSprites.Count == 0)
- {
- return null;
- }
- if (this.mSpriteIndices.Count != this.mSprites.Count)
- {
- this.MarkSpriteListAsChanged();
- }
- int num;
- if (this.mSpriteIndices.TryGetValue(name, out num))
- {
- if (num > -1 && num < this.mSprites.Count)
- {
- return this.mSprites[num];
- }
- this.MarkSpriteListAsChanged();
- return (!this.mSpriteIndices.TryGetValue(name, out num)) ? null : this.mSprites[num];
- }
- else
- {
- int i = 0;
- int count = this.mSprites.Count;
- while (i < count)
- {
- UISpriteData uispriteData = this.mSprites[i];
- if (!string.IsNullOrEmpty(uispriteData.name) && name == uispriteData.name)
- {
- this.MarkSpriteListAsChanged();
- return uispriteData;
- }
- i++;
- }
- }
- }
- return null;
- }
- public string GetRandomSprite(string startsWith)
- {
- if (this.GetSprite(startsWith) == null)
- {
- List<UISpriteData> spriteList = this.spriteList;
- List<string> list = new List<string>();
- foreach (UISpriteData uispriteData in spriteList)
- {
- if (uispriteData.name.StartsWith(startsWith))
- {
- list.Add(uispriteData.name);
- }
- }
- return (list.Count <= 0) ? null : list[UnityEngine.Random.Range(0, list.Count)];
- }
- return startsWith;
- }
- public void MarkSpriteListAsChanged()
- {
- this.mSpriteIndices.Clear();
- int i = 0;
- int count = this.mSprites.Count;
- while (i < count)
- {
- this.mSpriteIndices[this.mSprites[i].name] = i;
- i++;
- }
- }
- public void SortAlphabetically()
- {
- this.mSprites.Sort((UISpriteData s1, UISpriteData s2) => s1.name.CompareTo(s2.name));
- }
- public BetterList<string> GetListOfSprites()
- {
- if (this.mReplacement != null)
- {
- return this.mReplacement.GetListOfSprites();
- }
- if (this.mSprites.Count == 0)
- {
- this.Upgrade();
- }
- BetterList<string> betterList = new BetterList<string>();
- int i = 0;
- int count = this.mSprites.Count;
- while (i < count)
- {
- UISpriteData uispriteData = this.mSprites[i];
- if (uispriteData != null && !string.IsNullOrEmpty(uispriteData.name))
- {
- betterList.Add(uispriteData.name);
- }
- i++;
- }
- return betterList;
- }
- public BetterList<string> GetListOfSprites(string match)
- {
- if (this.mReplacement)
- {
- return this.mReplacement.GetListOfSprites(match);
- }
- if (string.IsNullOrEmpty(match))
- {
- return this.GetListOfSprites();
- }
- if (this.mSprites.Count == 0)
- {
- this.Upgrade();
- }
- BetterList<string> betterList = new BetterList<string>();
- int i = 0;
- int count = this.mSprites.Count;
- while (i < count)
- {
- UISpriteData uispriteData = this.mSprites[i];
- if (uispriteData != null && !string.IsNullOrEmpty(uispriteData.name) && string.Equals(match, uispriteData.name, StringComparison.OrdinalIgnoreCase))
- {
- betterList.Add(uispriteData.name);
- return betterList;
- }
- i++;
- }
- string[] array = match.Split(new char[]
- {
- ' '
- }, StringSplitOptions.RemoveEmptyEntries);
- for (int j = 0; j < array.Length; j++)
- {
- array[j] = array[j].ToLower();
- }
- int k = 0;
- int count2 = this.mSprites.Count;
- while (k < count2)
- {
- UISpriteData uispriteData2 = this.mSprites[k];
- if (uispriteData2 != null && !string.IsNullOrEmpty(uispriteData2.name))
- {
- string text = uispriteData2.name.ToLower();
- int num = 0;
- for (int l = 0; l < array.Length; l++)
- {
- if (text.Contains(array[l]))
- {
- num++;
- }
- }
- if (num == array.Length)
- {
- betterList.Add(uispriteData2.name);
- }
- }
- k++;
- }
- return betterList;
- }
- private bool References(UIAtlas atlas)
- {
- return !(atlas == null) && (atlas == this || (this.mReplacement != null && this.mReplacement.References(atlas)));
- }
- public static bool CheckIfRelated(UIAtlas a, UIAtlas b)
- {
- return !(a == null) && !(b == null) && (a == b || a.References(b) || b.References(a));
- }
- public void MarkAsChanged()
- {
- if (this.mReplacement != null)
- {
- this.mReplacement.MarkAsChanged();
- }
- UISprite[] array = NGUITools.FindActive<UISprite>();
- int i = 0;
- int num = array.Length;
- while (i < num)
- {
- UISprite uisprite = array[i];
- if (UIAtlas.CheckIfRelated(this, uisprite.atlas))
- {
- UIAtlas atlas = uisprite.atlas;
- uisprite.atlas = null;
- uisprite.atlas = atlas;
- }
- i++;
- }
- UIFont[] array2 = Resources.FindObjectsOfTypeAll(typeof(UIFont)) as UIFont[];
- int j = 0;
- int num2 = array2.Length;
- while (j < num2)
- {
- UIFont uifont = array2[j];
- if (UIAtlas.CheckIfRelated(this, uifont.atlas))
- {
- UIAtlas atlas2 = uifont.atlas;
- uifont.atlas = null;
- uifont.atlas = atlas2;
- }
- j++;
- }
- UILabel[] array3 = NGUITools.FindActive<UILabel>();
- int k = 0;
- int num3 = array3.Length;
- while (k < num3)
- {
- UILabel uilabel = array3[k];
- if (uilabel.bitmapFont != null && UIAtlas.CheckIfRelated(this, uilabel.bitmapFont.atlas))
- {
- UIFont bitmapFont = uilabel.bitmapFont;
- uilabel.bitmapFont = null;
- uilabel.bitmapFont = bitmapFont;
- }
- k++;
- }
- }
- private bool Upgrade()
- {
- if (this.mReplacement)
- {
- return this.mReplacement.Upgrade();
- }
- if (this.mSprites.Count == 0 && this.sprites.Count > 0 && this.material)
- {
- Texture mainTexture = this.material.mainTexture;
- int width = (!(mainTexture != null)) ? 512 : mainTexture.width;
- int height = (!(mainTexture != null)) ? 512 : mainTexture.height;
- for (int i = 0; i < this.sprites.Count; i++)
- {
- UIAtlas.Sprite sprite = this.sprites[i];
- Rect outer = sprite.outer;
- Rect inner = sprite.inner;
- if (this.mCoordinates == UIAtlas.Coordinates.TexCoords)
- {
- NGUIMath.ConvertToPixels(outer, width, height, true);
- NGUIMath.ConvertToPixels(inner, width, height, true);
- }
- UISpriteData uispriteData = new UISpriteData();
- uispriteData.name = sprite.name;
- uispriteData.x = Mathf.RoundToInt(outer.xMin);
- uispriteData.y = Mathf.RoundToInt(outer.yMin);
- uispriteData.width = Mathf.RoundToInt(outer.width);
- uispriteData.height = Mathf.RoundToInt(outer.height);
- uispriteData.paddingLeft = Mathf.RoundToInt(sprite.paddingLeft * outer.width);
- uispriteData.paddingRight = Mathf.RoundToInt(sprite.paddingRight * outer.width);
- uispriteData.paddingBottom = Mathf.RoundToInt(sprite.paddingBottom * outer.height);
- uispriteData.paddingTop = Mathf.RoundToInt(sprite.paddingTop * outer.height);
- uispriteData.borderLeft = Mathf.RoundToInt(inner.xMin - outer.xMin);
- uispriteData.borderRight = Mathf.RoundToInt(outer.xMax - inner.xMax);
- uispriteData.borderBottom = Mathf.RoundToInt(outer.yMax - inner.yMax);
- uispriteData.borderTop = Mathf.RoundToInt(inner.yMin - outer.yMin);
- this.mSprites.Add(uispriteData);
- }
- this.sprites.Clear();
- return true;
- }
- return false;
- }
- [HideInInspector]
- [SerializeField]
- private Material material;
- [HideInInspector]
- [SerializeField]
- private List<UISpriteData> mSprites = new List<UISpriteData>();
- [HideInInspector]
- [SerializeField]
- private float mPixelSize = 1f;
- [HideInInspector]
- [SerializeField]
- private UIAtlas mReplacement;
- [HideInInspector]
- [SerializeField]
- private UIAtlas.Coordinates mCoordinates;
- [HideInInspector]
- [SerializeField]
- private List<UIAtlas.Sprite> sprites = new List<UIAtlas.Sprite>();
- private int mPMA = -1;
- private Dictionary<string, int> mSpriteIndices = new Dictionary<string, int>();
- [Serializable]
- private class Sprite
- {
- public bool hasPadding
- {
- get
- {
- return this.paddingLeft != 0f || this.paddingRight != 0f || this.paddingTop != 0f || this.paddingBottom != 0f;
- }
- }
- public string name = "Unity Bug";
- public Rect outer = new Rect(0f, 0f, 1f, 1f);
- public Rect inner = new Rect(0f, 0f, 1f, 1f);
- public bool rotated;
- public float paddingLeft;
- public float paddingRight;
- public float paddingTop;
- public float paddingBottom;
- }
- private enum Coordinates
- {
- Pixels,
- TexCoords
- }
- }
|