private void AlignAndSetText(string t_text, float t_fontSpacing, HorzAlignment t_horizontalAlignment, VertAlignment t_verticalAlignment) { _horizontalAlignment = t_horizontalAlignment; _verticalAlignment = t_verticalAlignment; SetVerticalAligment(t_verticalAlignment); _fontSpacing = t_fontSpacing; SetText(t_text, true); }
public void UpdateFont(Sprite t_fontSprite, float t_pixelsToUnits, string t_text, string t_fontSet, int t_fontWidth, int t_fontHeight, float t_fontSpacing, SFont.HorzAlignment t_horizontalAlignment, SFont.VertAlignment t_verticalAlignment, int t_orderInLayer, string t_sortingLayer, Color t_colorTint) { if (_parentTransform) { //check basic requirements to make a font //check constraints on input if (t_pixelsToUnits < 0) { t_pixelsToUnits *= -1.0f; } else if (Mathf.Approximately(t_pixelsToUnits, 0.0f)) { t_pixelsToUnits = 1.0f; } t_fontSet = t_fontSet.Trim(); if (t_fontWidth < 0) { t_fontWidth *= -1; } if (t_fontHeight < 0) { t_fontHeight *= -1; } //check enums if (t_horizontalAlignment != SFont.HorzAlignment.Left && t_horizontalAlignment != SFont.HorzAlignment.Center && t_horizontalAlignment != SFont.HorzAlignment.Right) { t_horizontalAlignment = SFont.HorzAlignment.Left; } if (t_verticalAlignment != SFont.VertAlignment.Top && t_verticalAlignment != SFont.VertAlignment.Center && t_verticalAlignment != SFont.VertAlignment.Bottom) { t_verticalAlignment = SFont.VertAlignment.Top; } if (!(t_fontSprite == null) && t_pixelsToUnits > 0.0f && t_fontSet != "" && t_fontWidth > 0 && t_fontHeight > 0) { _basicFontSettingSet = true; //check if there are changes made to the font that will cause it to need to be recut if (t_fontSprite != _fontSprite || t_pixelsToUnits != _pixelsToUnits || t_fontSet != _fontSet || t_fontWidth != _fontWidth || t_fontHeight != _fontHeight || _spriteFontObject == null) { _fontSprite = t_fontSprite; _pixelsToUnits = t_pixelsToUnits; _fontSet = t_fontSet; _fontWidth = t_fontWidth; _fontHeight = t_fontHeight; _colorTint = t_colorTint; if (_spriteFontObject == null) { _spriteFontObject = new GameObject("_spriteFontObject"); _spriteFontRoot = new GameObject("_spriteFontRoot"); if (_debug) { _spriteFontObject.hideFlags = HideFlags.DontSave; _spriteFontRoot.hideFlags = HideFlags.DontSave; } else { _spriteFontObject.hideFlags = HideFlags.HideAndDontSave; _spriteFontRoot.hideFlags = HideFlags.HideAndDontSave; } _spriteFontObject.transform.position = _parentTransform.position; _spriteFontRoot.transform.position = _parentTransform.position; _spriteFontObject.transform.parent = _spriteFontRoot.transform; _spriteFontRoot.transform.rotation = _parentTransform.rotation; _spriteFontRoot.transform.localScale = _parentTransform.localScale; } else { ClearDigits(); ClearSprites(); } if (_characterSprites == null) { _characterSprites = new Sprite[_fontSet.Length]; } else { System.Array.Resize(ref _characterSprites, _fontSet.Length); } CutSprites(); _orderInLayer = t_orderInLayer; _sortingLayer = t_sortingLayer; _isAlignmentChanged = true; AlignAndSetText(t_text, t_fontSpacing, t_horizontalAlignment, t_verticalAlignment); } else if (_fontSpacing != t_fontSpacing || _horizontalAlignment != t_horizontalAlignment) { //only the alignment and text needs to be set or reset _isAlignmentChanged = true; ClearDigits(); _orderInLayer = t_orderInLayer; _sortingLayer = t_sortingLayer; AlignAndSetText(t_text, t_fontSpacing, t_horizontalAlignment, t_verticalAlignment); if (t_colorTint != _colorTint) { _colorTint = t_colorTint; ColorSprites(); } } else if (_text != t_text || _orderInLayer != t_orderInLayer || _sortingLayer != t_sortingLayer) { //only the text needs to be set or reset if (_orderInLayer != t_orderInLayer || _sortingLayer != t_sortingLayer) { _orderInLayer = t_orderInLayer; _sortingLayer = t_sortingLayer; SetText(t_text, true); } else { _orderInLayer = t_orderInLayer; _sortingLayer = t_sortingLayer; SetText(t_text); } if (t_colorTint != _colorTint) { _colorTint = t_colorTint; ColorSprites(); } } else if (_verticalAlignment != t_verticalAlignment) { _isAlignmentChanged = true; if (t_colorTint != _colorTint) { _colorTint = t_colorTint; ColorSprites(); } SetVerticalAligment(t_verticalAlignment); } else if (_colorTint != t_colorTint) { //change sprite colors _colorTint = t_colorTint; ColorSprites(); } } else { ClearDigits(); ClearSprites(); _fontSprite = t_fontSprite; _pixelsToUnits = t_pixelsToUnits; _fontSet = t_fontSet; _fontWidth = t_fontWidth; _fontHeight = t_fontHeight; _text = t_text; _fontSpacing = t_fontSpacing; _horizontalAlignment = t_horizontalAlignment; _verticalAlignment = t_verticalAlignment; _orderInLayer = t_orderInLayer; _sortingLayer = t_sortingLayer; _basicFontSettingSet = false; } } }
public void UpdateFont(Sprite t_fontSprite, float t_pixelsToUnits, string t_text, string t_fontSet, int t_fontWidth, int t_fontHeight, float t_fontSpacing, SFont.HorzAlignment t_horizontalAlignment, SFont.VertAlignment t_verticalAlignment, Color t_colorTint, int t_orderInLayer = 0, string t_sortingLayer = "", bool isNotPrefab = true, int t_fontSets = 0) { _fontSprite = t_fontSprite; _pixelsToUnits = t_pixelsToUnits; _text = t_text; _fontSet = t_fontSet; _fontWidth = t_fontWidth; _fontHeight = t_fontHeight; _fontSpacing = t_fontSpacing; _horizontalAlignment = t_horizontalAlignment; _verticalAlignment = t_verticalAlignment; _orderInLayer = t_orderInLayer; _sortingLayer = t_sortingLayer; _fontSets = t_fontSets; _colorTint = t_colorTint; if (SFConstants.FontSetsArray[_fontSets] != "Custom Value") { if (_fontSets < SFConstants.fsArray.Length) { _fontSet = SFConstants.fsArray[_fontSets]; } } if (!(_fontSprite == null)) { //check if the fontsprite ends with the fontHeight and fontWidth int lastIndexOfX = _fontSprite.name.LastIndexOf("x"); int lastIndexOf_ = _fontSprite.name.LastIndexOf("_"); if (_fontHeight == 0) { if (lastIndexOfX > -1 && lastIndexOf_ > -1) { string heightStr = _fontSprite.name.Substring(lastIndexOfX + 1, _fontSprite.name.Length - (lastIndexOfX + 1)); int iFontHeight = 0; if (int.TryParse(heightStr, out iFontHeight)) { _fontHeight = iFontHeight; } } } if (_fontWidth == 0) { if (lastIndexOfX > -1 && lastIndexOf_ > -1) { string widthStr = _fontSprite.name.Substring(lastIndexOf_ + 1, lastIndexOfX - (lastIndexOf_ + 1)); int iFontWidth = 0; if (int.TryParse(widthStr, out iFontWidth)) { _fontWidth = iFontWidth; } } } } if (_pixelsToUnits < 0) { _pixelsToUnits *= -1.0f; } else if (Mathf.Approximately(_pixelsToUnits, 0.0f)) { _pixelsToUnits = 1.0f; } _fontSet = _fontSet.Trim(); if (_fontWidth < 0) { _fontWidth *= -1; } if (_fontHeight < 0) { _fontHeight *= -1; } if (_isVisible && isNotPrefab) { if (_sFont == null) { _sFont = ScriptableObject.CreateInstance <SFont>(); _sFont.parentTransform = transform; _sFont.hideFlags = HideFlags.DontSave; } _sFont.UpdateFont(_fontSprite, _pixelsToUnits, _text, _fontSet, _fontWidth, _fontHeight, _fontSpacing, _horizontalAlignment, _verticalAlignment, _orderInLayer, _sortingLayer, _colorTint); } }
public static GameObject Create(Sprite t_fontSprite = null, float t_pixelsToUnits = 100.0f, string t_text = "", string t_fontSet = "", int t_fontWidth = 0, int t_fontHeight = 0, float t_fontSpacing = 0.0f, SFont.HorzAlignment t_horizontalAlignment = SFont.HorzAlignment.Left, SFont.VertAlignment t_verticalAlignment = SFont.VertAlignment.Top, Color?t_colorTint = null, int t_orderInLayer = 0, string t_sortingLayer = "") { GameObject newSpriteFont = new GameObject("SpriteFont", typeof(SpriteFont)); Color _tmpColor = t_colorTint.HasValue? t_colorTint.Value : Color.white; newSpriteFont.GetComponent <SpriteFont>().UpdateFont(t_fontSprite, t_pixelsToUnits, t_text, t_fontSet, t_fontWidth, t_fontHeight, t_fontSpacing, t_horizontalAlignment, t_verticalAlignment, _tmpColor, t_orderInLayer, t_sortingLayer, true, SFConstants.FontSetsArray.Length - 1); return(newSpriteFont); }