Search Unity

Question TextMeshPro highlighting words with regex

Discussion in 'Scripting' started by Viroz_, Mar 2, 2023.

  1. Viroz_

    Viroz_

    Joined:
    Dec 30, 2022
    Posts:
    17
    Hi,

    I made code editor for my game's purposes, and it works, but sometimes, regex for highlignting comments doesn't work as I expected. Code editor works with help of using Unity UI inputs and TextMeshPro. To get highlighting effect, every frame I iterate through every regex match in my dictionary which stores regex and according color. I color every letter in that regex match with according color. The problem seems to happen only when I add comments to my code (one- or multiline).
    Example:
    upload_2023-3-2_15-11-35.png

    My code responsible for highlighting:
    Code (CSharp):
    1.    
    2. // called every frame if code editor is shown
    3. private void HighlightSyntax(TextMeshProUGUI text)
    4.     {
    5.         if (text == null || text.text.Length == 0) return;
    6.         foreach (var item in EditorTheme.currentTheme.highlight)
    7.         {
    8.             Color32 color = item.Value;
    9.             foreach (Match match in item.Key.Matches(text.text))
    10.             {
    11.                 //Debug.Log("<color=yellow>" + match.Value + "</color> <color=red>" + match.Index + "</color> <color=cyan>" + text.textInfo.characterInfo.Length + "</color>");
    12.                 for (int i = match.Index; i < match.Index + match.Length; i++)
    13.                 {
    14.                     if (i > text.textInfo.characterInfo.Length) break;
    15.                     TMP_CharacterInfo characterInfo = text.textInfo.characterInfo[i];
    16.                     int meshIndex = text.textInfo.characterInfo[characterInfo.index].materialReferenceIndex;
    17.                     int vertexIndex = text.textInfo.characterInfo[characterInfo.index].vertexIndex;
    18.  
    19.                     Color32[] vertexColors = text.textInfo.meshInfo[meshIndex].colors32;
    20.                     if(vertexColors != null)
    21.                     {
    22.                         vertexColors[vertexIndex + 0] = color;
    23.                         vertexColors[vertexIndex + 1] = color;
    24.                         vertexColors[vertexIndex + 2] = color;
    25.                         vertexColors[vertexIndex + 3] = color;
    26.                     }
    27.                 }
    28.             }
    29.         }
    30.         text.UpdateVertexData(TMP_VertexDataUpdateFlags.All);
    31.     }
    Sometimes it also throws me this error "IndexOutOfRangeException: Index was outside the bounds of the array." and it points this line
    Code (CSharp):
    1. TMP_CharacterInfo characterInfo = text.textInfo.characterInfo[i];
    My highlighting rule for comments(XML):
    Code (csharp):
    1.  
    2.         <rule>
    3.             <regex>(\/\/.*)|(\/\*[\s\S]*?\*\/)</regex>
    4.             <color>#235530ff</color>
    5.         </rule>
    6.  
     

    Attached Files:

    Last edited: Mar 2, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Well that's easy! GO FIX IT! Here's how:

    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236

    Steps to success:
    - find which collection it is and what line of code accesses it <--- critical first step!)
    - find out why it has fewer items than you expect
    - fix whatever logic is making the indexing value exceed the collection size
    - remember you might have more than one instance of this script in your scene/prefab
    - remember the collection may be used in more than one location in the code
    - remember that indices start at ZERO (0) and go to the count / length minus 1.

    This means with three (3) elements, they are numbered 0, 1, and 2 only.

    Here is how you reason about and solve problems, a process commonly called "debugging."

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Don't know much about the regex stuff but
    textInfo.characterInfo
    can be full of a bunch of garbage characters at the end, since it doesn't clear the entire array whenever the text changes. You probably want to check to make sure your i is less than
    textInfo.characterCount
    instead. Keep in mind this value doesn't actually show correctly if you just update the text, you have to do a
    ForceMeshUpdate()
    for this value to be correct. It might be easier to just compare to the length of the actual string in the first place though.