tk2dTextMesh.GetMeshDimensionsForString C# (CSharp) Method

GetMeshDimensionsForString() public method

Calculates the mesh dimensions for the given string and returns a width and height.
public GetMeshDimensionsForString ( string str ) : Vector2
str string
return Vector2
    public Vector2 GetMeshDimensionsForString(string str)
    {
        bool _useInlineStyling = useInlineStyling;
        float maxWidth = 0.0f;

        float cursorX = 0.0f;
        float cursorY = 0.0f;

        int target = 0;
        for (int i = 0; i < str.Length && target < _maxChars; ++i)
        {
            int idx = str[i];
            if (idx == '\n')
            {
                maxWidth = Mathf.Max(cursorX, maxWidth);
                cursorX = 0.0f;
                cursorY -= (_fontInst.lineHeight + lineSpacing) * _scale.y;
                continue;
            }
            else if (_useInlineStyling)
            {
                if (idx == '^')
                {
                    if (i+1 < str.Length)
                    {
                        i++;
                        if (str[i] != '^')
                        {
                            continue;
                        }
                    }
                }
            }

            // Get the character from dictionary / array
            tk2dFontChar chr;
            if (_fontInst.useDictionary)
            {
                if (!_fontInst.charDict.ContainsKey(idx)) idx = 0;
                chr = _fontInst.charDict[idx];
            }
            else
            {
                if (idx >= _fontInst.chars.Length) idx = 0; // should be space
                chr = _fontInst.chars[idx];
            }

            cursorX += (chr.advance + spacing) * _scale.x;
            if (_kerning && i < str.Length - 1)
            {
                foreach (var k in _fontInst.kerning)
                {
                    if (k.c0 == str[i] && k.c1 == str[i+1])
                    {
                        cursorX += k.amount * _scale.x;
                        break;
                    }
                }
            }

            ++target;
        }

        maxWidth = Mathf.Max(cursorX, maxWidth);
        cursorY -= (_fontInst.lineHeight + lineSpacing) * _scale.y;

        return new Vector2(maxWidth, cursorY);
    }

Usage Example

    public void start(string str, Color color, Transform pointer, float posX = 0.0f, float posY = 100.0f)
    {
        if (text == null)
        {
            text = gameObject.transform.Find("effect").GetComponent <tk2dTextMesh>();
        }

        text.maxChars = str.Length;
        text.text     = str;
        text.color    = color;
        _color        = color;

        text.Commit();

        _v = text.GetMeshDimensionsForString(str);

        _pos = pointer.transform.position;

        _pos.x -= _v.x * 0.5f;
        _pos.x += posX;

        tf.position = GameManager.me.inGameGUICamera.ScreenToWorldPoint(GameManager.me.gameCamera.WorldToScreenPoint(_pos));;

        init(pointer, posX - _v.x * 0.5f, posY, true);

        _isStart = true;
        animation.Play();

        if (PerformanceManager.isLowPc)
        {
            return;
        }

        StartCoroutine(onCompleteEffectCT());
    }