using System; using System.Collections.Generic; using UnityEngine; [ExecuteInEditMode] [AddComponentMenu("NGUI/UI/NGUI Font")] public class UIFont : MonoBehaviour { public BMFont bmFont { get { return (!(this.mReplacement != null)) ? this.mFont : this.mReplacement.bmFont; } set { if (this.mReplacement != null) { this.mReplacement.bmFont = value; } else { this.mFont = value; } } } public int texWidth { get { return (!(this.mReplacement != null)) ? ((this.mFont == null) ? 1 : this.mFont.texWidth) : this.mReplacement.texWidth; } set { if (this.mReplacement != null) { this.mReplacement.texWidth = value; } else if (this.mFont != null) { this.mFont.texWidth = value; } } } public int texHeight { get { return (!(this.mReplacement != null)) ? ((this.mFont == null) ? 1 : this.mFont.texHeight) : this.mReplacement.texHeight; } set { if (this.mReplacement != null) { this.mReplacement.texHeight = value; } else if (this.mFont != null) { this.mFont.texHeight = value; } } } public bool hasSymbols { get { return (!(this.mReplacement != null)) ? (this.mSymbols != null && this.mSymbols.Count != 0) : this.mReplacement.hasSymbols; } } public List symbols { get { return (!(this.mReplacement != null)) ? this.mSymbols : this.mReplacement.symbols; } } public UIAtlas atlas { get { return (!(this.mReplacement != null)) ? this.mAtlas : this.mReplacement.atlas; } set { if (this.mReplacement != null) { this.mReplacement.atlas = value; } else if (this.mAtlas != value) { if (value == null) { if (this.mAtlas != null) { this.mMat = this.mAtlas.spriteMaterial; } if (this.sprite != null) { this.mUVRect = this.uvRect; } } this.mPMA = -1; this.mAtlas = value; this.MarkAsChanged(); } } } public Material material { get { if (this.mReplacement != null) { return this.mReplacement.material; } if (this.mAtlas != null) { return this.mAtlas.spriteMaterial; } if (this.mMat != null) { if (this.mDynamicFont != null && this.mMat != this.mDynamicFont.material) { this.mMat.mainTexture = this.mDynamicFont.material.mainTexture; } return this.mMat; } if (this.mDynamicFont != null) { return this.mDynamicFont.material; } return null; } set { if (this.mReplacement != null) { this.mReplacement.material = value; } else if (this.mMat != value) { this.mPMA = -1; this.mMat = value; this.MarkAsChanged(); } } } [Obsolete("Use UIFont.premultipliedAlphaShader instead")] public bool premultipliedAlpha { get { return this.premultipliedAlphaShader; } } public bool premultipliedAlphaShader { get { if (this.mReplacement != null) { return this.mReplacement.premultipliedAlphaShader; } if (this.mAtlas != null) { return this.mAtlas.premultipliedAlpha; } if (this.mPMA == -1) { Material material = this.material; this.mPMA = ((!(material != null) || !(material.shader != null) || !material.shader.name.Contains("Premultiplied")) ? 0 : 1); } return this.mPMA == 1; } } public bool packedFontShader { get { if (this.mReplacement != null) { return this.mReplacement.packedFontShader; } if (this.mAtlas != null) { return false; } if (this.mPacked == -1) { Material material = this.material; this.mPacked = ((!(material != null) || !(material.shader != null) || !material.shader.name.Contains("Packed")) ? 0 : 1); } return this.mPacked == 1; } } public Texture2D texture { get { if (this.mReplacement != null) { return this.mReplacement.texture; } Material material = this.material; return (!(material != null)) ? null : (material.mainTexture as Texture2D); } } public Rect uvRect { get { if (this.mReplacement != null) { return this.mReplacement.uvRect; } return (!(this.mAtlas != null) || this.sprite == null) ? new Rect(0f, 0f, 1f, 1f) : this.mUVRect; } set { if (this.mReplacement != null) { this.mReplacement.uvRect = value; } else if (this.sprite == null && this.mUVRect != value) { this.mUVRect = value; this.MarkAsChanged(); } } } public string spriteName { get { return (!(this.mReplacement != null)) ? this.mFont.spriteName : this.mReplacement.spriteName; } set { if (this.mReplacement != null) { this.mReplacement.spriteName = value; } else if (this.mFont.spriteName != value) { this.mFont.spriteName = value; this.MarkAsChanged(); } } } public bool isValid { get { return this.mDynamicFont != null || this.mFont.isValid; } } [Obsolete("Use UIFont.defaultSize instead")] public int size { get { return this.defaultSize; } set { this.defaultSize = value; } } public int defaultSize { get { if (this.mReplacement != null) { return this.mReplacement.defaultSize; } if (this.isDynamic || this.mFont == null) { return this.mDynamicFontSize; } return this.mFont.charSize; } set { if (this.mReplacement != null) { this.mReplacement.defaultSize = value; } else { this.mDynamicFontSize = value; } } } public UISpriteData sprite { get { if (this.mReplacement != null) { return this.mReplacement.sprite; } if (this.mSprite == null && this.mAtlas != null && !string.IsNullOrEmpty(this.mFont.spriteName)) { this.mSprite = this.mAtlas.GetSprite(this.mFont.spriteName); if (this.mSprite == null) { this.mSprite = this.mAtlas.GetSprite(base.name); } if (this.mSprite == null) { this.mFont.spriteName = null; } else { this.UpdateUVRect(); } int i = 0; int count = this.mSymbols.Count; while (i < count) { this.symbols[i].MarkAsChanged(); i++; } } return this.mSprite; } } public UIFont replacement { get { return this.mReplacement; } set { UIFont uifont = value; if (uifont == this) { uifont = null; } if (this.mReplacement != uifont) { if (uifont != null && uifont.replacement == this) { uifont.replacement = null; } if (this.mReplacement != null) { this.MarkAsChanged(); } this.mReplacement = uifont; if (uifont != null) { this.mPMA = -1; this.mMat = null; this.mFont = null; this.mDynamicFont = null; } this.MarkAsChanged(); } } } public bool isDynamic { get { return (!(this.mReplacement != null)) ? (this.mDynamicFont != null) : this.mReplacement.isDynamic; } } public Font dynamicFont { get { return (!(this.mReplacement != null)) ? this.mDynamicFont : this.mReplacement.dynamicFont; } set { if (this.mReplacement != null) { this.mReplacement.dynamicFont = value; } else if (this.mDynamicFont != value) { if (this.mDynamicFont != null) { this.material = null; } this.mDynamicFont = value; this.MarkAsChanged(); } } } public FontStyle dynamicFontStyle { get { return (!(this.mReplacement != null)) ? this.mDynamicFontStyle : this.mReplacement.dynamicFontStyle; } set { if (this.mReplacement != null) { this.mReplacement.dynamicFontStyle = value; } else if (this.mDynamicFontStyle != value) { this.mDynamicFontStyle = value; this.MarkAsChanged(); } } } private void Trim() { Texture texture = this.mAtlas.texture; if (texture != null && this.mSprite != null) { Rect rect = NGUIMath.ConvertToPixels(this.mUVRect, this.texture.width, this.texture.height, true); Rect rect2 = new Rect((float)this.mSprite.x, (float)this.mSprite.y, (float)this.mSprite.width, (float)this.mSprite.height); int xMin = Mathf.RoundToInt(rect2.xMin - rect.xMin); int yMin = Mathf.RoundToInt(rect2.yMin - rect.yMin); int xMax = Mathf.RoundToInt(rect2.xMax - rect.xMin); int yMax = Mathf.RoundToInt(rect2.yMax - rect.yMin); this.mFont.Trim(xMin, yMin, xMax, yMax); } } private bool References(UIFont font) { return !(font == null) && (font == this || (this.mReplacement != null && this.mReplacement.References(font))); } public static bool CheckIfRelated(UIFont a, UIFont b) { return !(a == null) && !(b == null) && ((a.isDynamic && b.isDynamic && a.dynamicFont.fontNames[0] == b.dynamicFont.fontNames[0]) || a == b || a.References(b) || b.References(a)); } private Texture dynamicTexture { get { if (this.mReplacement) { return this.mReplacement.dynamicTexture; } if (this.isDynamic) { return this.mDynamicFont.material.mainTexture; } return null; } } public void MarkAsChanged() { if (this.mReplacement != null) { this.mReplacement.MarkAsChanged(); } this.mSprite = null; UILabel[] array = NGUITools.FindActive(); int i = 0; int num = array.Length; while (i < num) { UILabel uilabel = array[i]; if (uilabel.enabled && NGUITools.GetActive(uilabel.gameObject) && UIFont.CheckIfRelated(this, uilabel.bitmapFont)) { UIFont bitmapFont = uilabel.bitmapFont; uilabel.bitmapFont = null; uilabel.bitmapFont = bitmapFont; } i++; } int j = 0; int count = this.symbols.Count; while (j < count) { this.symbols[j].MarkAsChanged(); j++; } } public void UpdateUVRect() { if (this.mAtlas == null) { return; } Texture texture = this.mAtlas.texture; if (texture != null) { this.mUVRect = new Rect((float)(this.mSprite.x - this.mSprite.paddingLeft), (float)(this.mSprite.y - this.mSprite.paddingTop), (float)(this.mSprite.width + this.mSprite.paddingLeft + this.mSprite.paddingRight), (float)(this.mSprite.height + this.mSprite.paddingTop + this.mSprite.paddingBottom)); this.mUVRect = NGUIMath.ConvertToTexCoords(this.mUVRect, texture.width, texture.height); if (this.mSprite.hasPadding) { this.Trim(); } } } private BMSymbol GetSymbol(string sequence, bool createIfMissing) { int i = 0; int count = this.mSymbols.Count; while (i < count) { BMSymbol bmsymbol = this.mSymbols[i]; if (bmsymbol.sequence == sequence) { return bmsymbol; } i++; } if (createIfMissing) { BMSymbol bmsymbol2 = new BMSymbol(); bmsymbol2.sequence = sequence; this.mSymbols.Add(bmsymbol2); return bmsymbol2; } return null; } public BMSymbol MatchSymbol(string text, int offset, int textLength) { int count = this.mSymbols.Count; if (count == 0) { return null; } textLength -= offset; for (int i = 0; i < count; i++) { BMSymbol bmsymbol = this.mSymbols[i]; int length = bmsymbol.length; if (length != 0 && textLength >= length) { bool flag = true; for (int j = 0; j < length; j++) { if (text[offset + j] != bmsymbol.sequence[j]) { flag = false; break; } } if (flag && bmsymbol.Validate(this.atlas)) { return bmsymbol; } } } return null; } public void AddSymbol(string sequence, string spriteName) { BMSymbol symbol = this.GetSymbol(sequence, true); symbol.spriteName = spriteName; this.MarkAsChanged(); } public void RemoveSymbol(string sequence) { BMSymbol symbol = this.GetSymbol(sequence, false); if (symbol != null) { this.symbols.Remove(symbol); } this.MarkAsChanged(); } public void RenameSymbol(string before, string after) { BMSymbol symbol = this.GetSymbol(before, false); if (symbol != null) { symbol.sequence = after; } this.MarkAsChanged(); } public bool UsesSprite(string s) { if (!string.IsNullOrEmpty(s)) { if (s.Equals(this.spriteName)) { return true; } int i = 0; int count = this.symbols.Count; while (i < count) { BMSymbol bmsymbol = this.symbols[i]; if (s.Equals(bmsymbol.spriteName)) { return true; } i++; } } return false; } [HideInInspector] [SerializeField] private Material mMat; [HideInInspector] [SerializeField] private Rect mUVRect = new Rect(0f, 0f, 1f, 1f); [HideInInspector] [SerializeField] private BMFont mFont = new BMFont(); [HideInInspector] [SerializeField] private UIAtlas mAtlas; [HideInInspector] [SerializeField] private UIFont mReplacement; [HideInInspector] [SerializeField] private List mSymbols = new List(); [HideInInspector] [SerializeField] private Font mDynamicFont; [HideInInspector] [SerializeField] private int mDynamicFontSize = 16; [HideInInspector] [SerializeField] private FontStyle mDynamicFontStyle; [NonSerialized] private UISpriteData mSprite; private int mPMA = -1; private int mPacked = -1; }