Setting a color to selected string letters in inspector

Hi, I’m building a dialogue system and I was wondering if it was possible through editor scripting to set certain parts of a string to a certain color. Specifically by selecting them through the click drag function like in word for example.
So the idea is first I’ll add a color picked, but then I need to somehow be able to click on that color picker and set the color of only the highlighted section of characters in the string.

From there I’ll record the first and last character and update the non-editor script accordingly to remember the letters but that’s not really relevant.

Again to be specific, I just want to be able to color in the highlighted part of a string.

Thank you.

Hey @Zuccinin I just went ahead and presumed you are using Text Mesh Pro (now built in to Unity). I put together this editor window script that does what you want. I’m sure you’ll be able to adapt it to your needs. You can see how it can be extended to add any tags to the selected text - not just color.

To use it, you dock the window somewhere and select a Text Mesh Pro object in the inspector. Set the color with the color picker, then select the text you want to color in the text box and press the + button. It’ll grab the selected text and add tags around the text.

using UnityEngine;
using System.Reflection;
using UnityEditor;
using TMPro;

namespace Zuccinin
{
    public class TextUtilityWindow : EditorWindow
    {
        [MenuItem("Zuccinin/Text Utility", false, 0)]
        public static void OpenTextUtilityWindow()
        {
            EditorWindow.GetWindow(typeof(TextUtilityWindow), false, "Text Utilities");
        }

        Color textColor = Color.white;
        TMP_Text textObject;

        private void OnGUI()
        {
            if(Selection.activeTransform != null)
            {
                textObject = Selection.activeTransform.GetComponent<TMP_Text>();
            }
            else
            {
                textObject = null;
            }

            DrawTextArea();

            EditorGUILayout.BeginHorizontal();
            textColor = EditorGUILayout.ColorField(textColor);
            if (GUILayout.Button("+", EditorStyles.toolbarButton, GUILayout.Width(25), GUILayout.Height(15)))
            {
                AddColor();
            }
            EditorGUILayout.EndHorizontal();
        }

        void DrawTextArea()
        {
            //DIALOGUE ENTRY TEXT AREA
            if(textObject == null)
            {
                EditorGUILayout.LabelField("SELECT A TEXT OBJECT IN THE INSPECTOR.");
            }
            else
            {
                string s = textObject.text;
                EditorGUI.BeginChangeCheck();

                s = EditorGUILayout.TextArea(s);

                if (EditorGUI.EndChangeCheck())
                {
                    Undo.SetCurrentGroupName("Changed Dialogue Text");
                    textObject.text = s;
                    Undo.CollapseUndoOperations(Undo.GetCurrentGroup());
                }
            }
        }

        void AddTag(string prefix, string suffix)
        {
            if (textObject == null) return;

            TextEditor textEditor = typeof(EditorGUI)
                .GetField("activeEditor", BindingFlags.Static | BindingFlags.NonPublic)
                .GetValue(null) as TextEditor;

            if (textEditor != null)
            {
                string selectedText = textEditor.SelectedText;
                int index = (textEditor.selectIndex < textEditor.cursorIndex) ? textEditor.selectIndex : textEditor.cursorIndex;
                int length = selectedText.Length;

                string originalText = textObject.text;
                string tagText = prefix + selectedText + suffix;

                string newText = tagText;

                if (length < originalText.Length)
                {
                    string strippedText = originalText.Remove(index, length);
                    newText = strippedText.Insert(index, tagText);
                }

                textObject.text = newText;

                ClearFocus();
            }
        }

        void AddColor()
        {
            string color = ColorUtility.ToHtmlStringRGB(textColor);

            string prefix = "<color=#" + color + ">";
            string suffix = "</color>";

            AddTag(prefix, suffix);
        }

        void ClearFocus()
        {
            GUIUtility.keyboardControl = 0;
            GUIUtility.hotControl = 0;
        }

    }
}