-1

Platform:


OS: Win10
Unity V.: 2022.2.19f1
textmeshpro V.: 2023.1
Language: C#
Build target: PC

Preamble:

Note; I have already looked over the suggested posts before making this one.

I can't find a solution for this anywhere. All I can find is how to replace the whole text body or add style / other things as the Author in the editor >Not< as the end user in a build at runtime.

I want to make a WYSIWYG text editor much like the Post editor on these forums.

I need to be able to insert rich text into the text body at a specific index during runtime e.g. "<b>" or "<b/>". And call textmeshpro to parse the inserted rich text tags.

I've looked at the documentation for C# and TMP but am unable to resovle using Insert() or any other means to insert into a string. I've exhaust my options searches only coming up with the same posts I've already read.

I Need a new set of terms, a new line of inquery, or a example of how to implent a working means of inserting.

If someone could post a stripped down working model that would be super!

Question:

How do I insert a string or rich text tag at a given index on a build during runtime via script.

NOT replace the whole text body if possible.

Clarification:

I'm aware you can access the TMP_InputField.Text component. I am unable to insert at an Index.

Here is some stripped down code of my problem.
I have a canvas TMP_InputField. There is a monobehaviour script that has a serialized field for the TMP object and a public method to insert "BB" into the inputfield.text property at index 0 for debbugging. To invoke that function I've got a button on the same canvas.

[SerializeField]
private TMP_InputField someTMPObject;

public void ButtonFunc_Insert()
{
//Insert Text at Index - Doesn't Work:
someTMPObject.text.Insert(0, "start of text array ");

//Replacing Text - works:
someTMPObject.text = "Replace all text.";

//Appending Text - works:
someTMPObject.text += "Add more text to the end!";
}
Valthalin
  • 415
  • 2
  • 7
  • well you can perform any string action you like on the text of the tmptext property, however, most likely it might look like you arent replacing the text but you will be, – BugFinder Aug 24 '23 at 07:37
  • You can get the .text property on the object and manipulate it like any other string. Can you show some code for what you've already tried? I personally have used rich text tags to add context-dependent colours this way. – Rocketman Dan Aug 24 '23 at 08:49
  • 1
    Use the string function [Insert](https://learn.microsoft.com/en-us/dotnet/api/system.string.insert?view=net-7.0). As for TMP, as along as it has rich text enabled it will handle the tags automatically. – hijinxbassist Aug 24 '23 at 16:01
  • @BugFinder I appreaciate the comment, but it doesn't offer any leads. – Valthalin Aug 29 '23 at 02:00
  • @RocketmanDan thank you for commenting, I updated my post to reflect what I've tried. It would be helpful if you could give a sample of how you target and update the colored text. Thank you. – Valthalin Aug 29 '23 at 02:02
  • @hijinxbassist I had thought Insert() would be the way to do what I wanted, but I've been unable to get it to work, and the documentation is impenetrable or useless in resolving issues beyond the basic syntax implementation. – Valthalin Aug 29 '23 at 02:04
  • I'm at work now so I can't give an immediate example but have you considered interpolated strings? `tmpObject.text = $"{primaryColourTag}Some text{colourEndTag}";` In my use cases I've had a builder function or even class that handles a set of tags and their values to allow user defined colours in the tags. To change the colour at runtime you can just change the string value (obtained from a colour object) in order to change the string colour. I will provide an example when I get home from work in about 7 hours. – Rocketman Dan Aug 29 '23 at 10:54
  • 2
    `Insert` returns a new string. Docs `(returns) "A new string that is equivalent to this instance, but with value inserted at position startIndex."` This is why the first attempt does not work, because the string with the inserted text is discarded. – hijinxbassist Aug 29 '23 at 15:21
  • 1
    @hijinxbassist is correct. Strings in C# are immutable. Function which "modify" strings typically return a new string object that must be the right operand of an assignment to your original string. Although I maintain that an interpolated string may be optimally readable when you know exactly where you are trying to insert your variable. – Rocketman Dan Aug 29 '23 at 15:46

1 Answers1

0

Use (string).Insert to insert text at a specific index.
(string).Insert returns a new instance of string with the inserted text.

var text = someTMPObject.text; // Get a copy of the existing text string

text = text.Insert(0, "start of text array "); // Modify string using Insert

someTMPObject.text = text; // Assign string back to TMP object
hijinxbassist
  • 3,667
  • 1
  • 18
  • 23
  • I thought the returned string would just be assigned back to the TMP object. I should have known better. Thank you so much! – Valthalin Sep 02 '23 at 08:38