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 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 spriteList = this.spriteList; List list = new List(); 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 GetListOfSprites() { if (this.mReplacement != null) { return this.mReplacement.GetListOfSprites(); } if (this.mSprites.Count == 0) { this.Upgrade(); } BetterList betterList = new BetterList(); 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 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 betterList = new BetterList(); 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(); 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(); 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 mSprites = new List(); [HideInInspector] [SerializeField] private float mPixelSize = 1f; [HideInInspector] [SerializeField] private UIAtlas mReplacement; [HideInInspector] [SerializeField] private UIAtlas.Coordinates mCoordinates; [HideInInspector] [SerializeField] private List sprites = new List(); private int mPMA = -1; private Dictionary mSpriteIndices = new Dictionary(); [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 } }