tk2dTextMesh.NumTotalCharacters C# (CSharp) Method

NumTotalCharacters() public method

Returns the number of characters excluding texture gradient escape codes.
public NumTotalCharacters ( ) : int
return int
    public int NumTotalCharacters()
    {
        InitInstance();

        bool _useInlineStyling = useInlineStyling;
        int numChars = 0;
        for (int i = 0; i < _formattedText.Length; ++i)
        {
            int idx = _formattedText[i];

            if (_fontInst.useDictionary)
            {
                if (!_fontInst.charDict.ContainsKey(idx)) idx = 0;
            }
            else
            {
                if (idx >= _fontInst.chars.Length) idx = 0; // should be space
            }

            if (idx == '\n')
            {
                continue;
            }
            else if (_useInlineStyling)
            {
                if (idx == '^')
                {
                    if (i+1 < _formattedText.Length)
                    {
                        i++;
                        if (_formattedText[i] != '^')
                        {
                            continue;
                        }
                    }
                }
            }

            ++numChars;
        }
        return numChars;
    }

Usage Example

示例#1
0
    public override void OnInspectorGUI()
    {
        tk2dTextMesh textMesh = (tk2dTextMesh)target;

        tk2dGuiUtility.LookLikeControls(80, 50);

        // maybe cache this if its too slow later
        if (allFonts == null || allFontNames == null)
        {
            tk2dGenericIndexItem[]      indexFonts    = tk2dEditorUtility.GetOrCreateIndex().GetFonts();
            List <tk2dGenericIndexItem> filteredFonts = new List <tk2dGenericIndexItem>();
            foreach (var f in indexFonts)
            {
                if (!f.managed)
                {
                    filteredFonts.Add(f);
                }
            }

            allFonts     = filteredFonts.ToArray();
            allFontNames = new string[allFonts.Length];
            for (int i = 0; i < allFonts.Length; ++i)
            {
                allFontNames[i] = allFonts[i].AssetName;
            }
        }

        if (allFonts != null && allFonts.Length > 0)
        {
            if (textMesh.font == null)
            {
                textMesh.font = allFonts[0].GetAsset <tk2dFont>().data;
            }

            int    currId = -1;
            string guid   = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(textMesh.font));
            for (int i = 0; i < allFonts.Length; ++i)
            {
                if (allFonts[i].dataGUID == guid)
                {
                    currId = i;
                }
            }

            int newId = EditorGUILayout.Popup("Font", currId, allFontNames);
            if (newId != currId)
            {
                UndoableAction(tm => tm.font = allFonts[newId].GetAsset <tk2dFont>().data);
                GUI.changed = true;
            }

            EditorGUILayout.BeginHorizontal();
            int newMaxChars = Mathf.Clamp(EditorGUILayout.IntField("Max Chars", textMesh.maxChars), 1, 16000);
            if (newMaxChars != textMesh.maxChars)
            {
                UndoableAction(tm => tm.maxChars = newMaxChars);
            }

            if (GUILayout.Button("Fit", GUILayout.MaxWidth(32.0f)))
            {
                UndoableAction(tm => tm.maxChars = tm.NumTotalCharacters());
                GUI.changed = true;
            }
            EditorGUILayout.EndHorizontal();

            bool newFormatting = EditorGUILayout.BeginToggleGroup("Formatting", textMesh.formatting);
            if (newFormatting != textMesh.formatting)
            {
                UndoableAction(tm => tm.formatting = newFormatting);
                GUI.changed = true;
            }

            GUILayout.BeginHorizontal();
            ++EditorGUI.indentLevel;
            if (textMesh.wordWrapWidth == 0)
            {
                EditorGUILayout.PrefixLabel("Word Wrap");
                if (GUILayout.Button("Enable", EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
                {
                    UndoableAction(tm => tm.wordWrapWidth = (tm.wordWrapWidth == 0) ? 500 : tm.wordWrapWidth);
                    GUI.changed = true;
                }
            }
            else
            {
                int newWordWrapWidth = EditorGUILayout.IntField("Word Wrap", textMesh.wordWrapWidth);
                if (newWordWrapWidth != textMesh.wordWrapWidth)
                {
                    UndoableAction(tm => tm.wordWrapWidth = newWordWrapWidth);
                }

                if (GUILayout.Button("Disable", EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
                {
                    UndoableAction(tm => tm.wordWrapWidth = 0);
                    GUI.changed = true;
                }
            }
            --EditorGUI.indentLevel;
            GUILayout.EndHorizontal();
            EditorGUILayout.EndToggleGroup();

            GUILayout.BeginHorizontal();
            bool newInlineStyling = EditorGUILayout.Toggle("Inline Styling", textMesh.inlineStyling);
            if (newInlineStyling != textMesh.inlineStyling)
            {
                UndoableAction(tm => tm.inlineStyling = newInlineStyling);
            }
            if (textMesh.inlineStyling)
            {
                showInlineStylingHelp = GUILayout.Toggle(showInlineStylingHelp, "?", EditorStyles.miniButton, GUILayout.ExpandWidth(false));
            }
            GUILayout.EndHorizontal();

            if (textMesh.inlineStyling && showInlineStylingHelp)
            {
                Color bg = GUI.backgroundColor;
                GUI.backgroundColor = new Color32(154, 176, 203, 255);
                string message = "Inline style commands\n\n" +
                                 "^cRGBA - set color\n" +
                                 "^gRGBARGBA - set top and bottom colors\n" +
                                 "      RGBA = single digit hex values (0 - f)\n\n" +
                                 "^CRRGGBBAA - set color\n" +
                                 "^GRRGGBBAARRGGBBAA - set top and bottom colors\n" +
                                 "      RRGGBBAA = 2 digit hex values (00 - ff)\n\n" +
                                 ((textMesh.font.textureGradients && textMesh.font.gradientCount > 0) ?
                                  "^0-9 - select gradient\n" : "") +
                                 "^^ - print ^";
                tk2dGuiUtility.InfoBox(message, tk2dGuiUtility.WarningLevel.Info);
                GUI.backgroundColor = bg;
            }

            GUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Text");
            string newText = EditorGUILayout.TextArea(textMesh.text, textAreaStyle, GUILayout.Height(64));
            if (newText != textMesh.text)
            {
                UndoableAction(tm => tm.text = newText);
                GUI.changed = true;
            }
            GUILayout.EndHorizontal();

            if (textMesh.NumTotalCharacters() > textMesh.maxChars)
            {
                tk2dGuiUtility.InfoBox("Number of printable characters in text mesh exceeds MaxChars on this text mesh. " +
                                       "The text will be clipped at " + textMesh.maxChars.ToString() + " characters.", tk2dGuiUtility.WarningLevel.Error);
            }

            TextAnchor newTextAnchor = (TextAnchor)EditorGUILayout.EnumPopup("Anchor", textMesh.anchor);
            if (newTextAnchor != textMesh.anchor)
            {
                UndoableAction(tm => tm.anchor = newTextAnchor);
            }

            bool newKerning = EditorGUILayout.Toggle("Kerning", textMesh.kerning);
            if (newKerning != textMesh.kerning)
            {
                UndoableAction(tm => tm.kerning = newKerning);
            }

            float newSpacing = EditorGUILayout.FloatField("Spacing", textMesh.Spacing);
            if (newSpacing != textMesh.Spacing)
            {
                UndoableAction(tm => tm.Spacing = newSpacing);
            }

            float newLineSpacing = EditorGUILayout.FloatField("Line Spacing", textMesh.LineSpacing);
            if (newLineSpacing != textMesh.LineSpacing)
            {
                UndoableAction(tm => tm.LineSpacing = newLineSpacing);
            }

#if UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2
            int sortingOrder = EditorGUILayout.IntField("Sorting Order In Layer", targetTextMeshes[0].SortingOrder);
            if (sortingOrder != targetTextMeshes[0].SortingOrder)
            {
                tk2dUndo.RecordObjects(targetTextMeshes, "Sorting Order In Layer");
                foreach (tk2dTextMesh s in targetTextMeshes)
                {
                    s.SortingOrder = sortingOrder;
                }
            }
#else
            if (renderers.Length > 0)
            {
                string sortingLayerName = tk2dEditorUtility.SortingLayerNamePopup("Sorting Layer", renderers[0].sortingLayerName);
                if (sortingLayerName != renderers[0].sortingLayerName)
                {
                    tk2dUndo.RecordObjects(renderers, "Sorting Layer");
                    foreach (Renderer r in renderers)
                    {
                        r.sortingLayerName = sortingLayerName;
                        EditorUtility.SetDirty(r);
                    }
                }

                int sortingOrder = EditorGUILayout.IntField("Order In Layer", targetTextMeshes[0].SortingOrder);
                if (sortingOrder != targetTextMeshes[0].SortingOrder)
                {
                    tk2dUndo.RecordObjects(targetTextMeshes, "Order In Layer");
                    tk2dUndo.RecordObjects(renderers, "Order In Layer");
                    foreach (tk2dTextMesh s in targetTextMeshes)
                    {
                        s.SortingOrder = sortingOrder;
                    }
                }
            }
#endif

            Vector3 newScale = EditorGUILayout.Vector3Field("Scale", textMesh.scale);
            if (newScale != textMesh.scale)
            {
                UndoableAction(tm => tm.scale = newScale);
            }

            if (textMesh.font.textureGradients && textMesh.font.gradientCount > 0)
            {
                GUILayout.BeginHorizontal();
                EditorGUILayout.PrefixLabel("TextureGradient");

                // Draw gradient scroller
                bool drawGradientScroller = true;
                if (drawGradientScroller)
                {
                    textMesh.textureGradient = textMesh.textureGradient % textMesh.font.gradientCount;

                    gradientScroll = EditorGUILayout.BeginScrollView(gradientScroll, GUILayout.ExpandHeight(false));
                    Rect r = GUILayoutUtility.GetRect(textMesh.font.gradientTexture.width, textMesh.font.gradientTexture.height, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false));
                    GUI.DrawTexture(r, textMesh.font.gradientTexture);

                    Rect hr = r;
                    hr.width /= textMesh.font.gradientCount;
                    hr.x     += hr.width * textMesh.textureGradient;
                    float     ox        = hr.width / 8;
                    float     oy        = hr.height / 8;
                    Vector3[] rectVerts = { new Vector3(hr.x + 0.5f + ox, hr.y + oy, 0), new Vector3(hr.x + hr.width - ox, hr.y + oy, 0), new Vector3(hr.x + hr.width - ox, hr.y + hr.height - 0.5f - oy, 0), new Vector3(hr.x + ox, hr.y + hr.height - 0.5f - oy, 0) };
                    Handles.DrawSolidRectangleWithOutline(rectVerts, new Color(0, 0, 0, 0.2f), new Color(0, 0, 0, 1));

                    if (GUIUtility.hotControl == 0 && Event.current.type == EventType.MouseDown && r.Contains(Event.current.mousePosition))
                    {
                        int newTextureGradient = (int)(Event.current.mousePosition.x / (textMesh.font.gradientTexture.width / textMesh.font.gradientCount));
                        if (newTextureGradient != textMesh.textureGradient)
                        {
                            UndoableAction(delegate(tk2dTextMesh tm) {
                                if (tm.useGUILayout && tm.font != null && newTextureGradient < tm.font.gradientCount)
                                {
                                    tm.textureGradient = newTextureGradient;
                                }
                            });
                        }
                        GUI.changed = true;
                    }

                    EditorGUILayout.EndScrollView();
                }


                GUILayout.EndHorizontal();
            }

            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button("HFlip"))
            {
                UndoableAction(delegate(tk2dTextMesh tm) {
                    Vector3 s = tm.scale;
                    s.x      *= -1.0f;
                    tm.scale  = s;
                });
                GUI.changed = true;
            }
            if (GUILayout.Button("VFlip"))
            {
                UndoableAction(delegate(tk2dTextMesh tm) {
                    Vector3 s = tm.scale;
                    s.y      *= -1.0f;
                    tm.scale  = s;
                });
                GUI.changed = true;
            }

            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button("Bake Scale"))
            {
                tk2dScaleUtility.Bake(textMesh.transform);
                GUI.changed = true;
            }

            GUIContent pixelPerfectButton = new GUIContent("1:1", "Make Pixel Perfect");
            if (GUILayout.Button(pixelPerfectButton))
            {
                if (tk2dPixelPerfectHelper.inst)
                {
                    tk2dPixelPerfectHelper.inst.Setup();
                }
                UndoableAction(tm => tm.MakePixelPerfect());
                GUI.changed = true;
            }

            EditorGUILayout.EndHorizontal();

            if (textMesh.font && !textMesh.font.inst.isPacked)
            {
                bool newUseGradient = EditorGUILayout.Toggle("Use Gradient", textMesh.useGradient);
                if (newUseGradient != textMesh.useGradient)
                {
                    UndoableAction(tm => tm.useGradient = newUseGradient);
                }

                if (textMesh.useGradient)
                {
                    Color newColor = EditorGUILayout.ColorField("Top Color", textMesh.color);
                    if (newColor != textMesh.color)
                    {
                        UndoableAction(tm => tm.color = newColor);
                    }

                    Color newColor2 = EditorGUILayout.ColorField("Bottom Color", textMesh.color2);
                    if (newColor2 != textMesh.color2)
                    {
                        UndoableAction(tm => tm.color2 = newColor2);
                    }
                }
                else
                {
                    Color newColor = EditorGUILayout.ColorField("Color", textMesh.color);
                    if (newColor != textMesh.color)
                    {
                        UndoableAction(tm => tm.color = newColor);
                    }
                }
            }

            if (GUI.changed)
            {
                foreach (tk2dTextMesh tm in targetTextMeshes)
                {
                    tm.ForceBuild();
                    EditorUtility.SetDirty(tm);
                }
            }
        }
    }
All Usage Examples Of tk2dTextMesh::NumTotalCharacters