Home > Net >  Unity: How to lerp the color of only a single char within a block of text?
Unity: How to lerp the color of only a single char within a block of text?

Time:01-06

I have an icon I want to appear exactly at the end of a paragraph. For example:

You throw the ice cube at the monster.

The monster falls over dead. *

where * represents the icon I want to appear at the end. This can be any char or image converted to a sprite.

This icon must flash in color to indicate a picture is still loading and can't be moused over yet. The rest of the text should not flash in color.

Only two approaches come to mind:

  1. Use rich text: Cannot lerp via rich text unless you constantly change the <color> string which I imagine must be extremely inefficient and freeze the game
  2. Represent the icon separately from the text: I can lerp this easily, but am unable to determine the position of the icon relative to the text. I could just always put it below, but that wastes space.

CodePudding user response:

If this is a TMP_Text (e.g. TextMeshProUGUI) you can go through the

TMP_TextInfo textInfo = yourText.textInfo;

The TMP_TextInfo holds all kind of information about the displayed text. E.g. the

TMP_LineInfo lastLine = textInfo.lineInfo[textInfo.lineCount - 1];

From that TMP_LineInfo you can now get e.g. the

float width = lastLine.width;

Having that information you should be able to go with option 2 and position an individual icon/text accordingly

  • Related