游戏中需要用到富文本提示信息,即一行提示信息文字可配置多种颜色,还可以考虑加入图片(本文只实现多颜色,扩展也比较简单)
实现富文本,大体分为两步:
1.实现富文本配置
2.显示富文本
先来看第一步,定义提示信息格式,如下所示:
<str color=252>测试</str><str color=253>行</str><str color=251>一</str>
很明显,可以为“测试”、“行”、“一”配置不同颜色,此处使用了颜色索引,约定好一些颜色,当然也可以直接配置颜色值,想要加入图片,只要扩展属性即可
以上格式采用正则表达式解析比较简单,具体解析代码如下:
USTRUCT()
struct FTextElementInfo
{
GENERATED_USTRUCT_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FString Text;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 Color;
};
UFUNCTION(BlueprintCallable, Category = "TextElement")
void ParseNoticeString(const FString& Strs, TArray<FTextElementInfo>& ElementList);
void ParseTextElement(const FString& Str, FString& Text, int32& Color);
void UTestGameInstance::ParseNoticeString(const FString& Strs, TArray<FTextElementInfo>& ElementList)
{
TArray<FString> OutArray;
Strs.ParseIntoArray(OutArray, TEXT("><"));
for (int32 Index = 0; Index < OutArray.Num(); ++Index)
{
FTextElementInfo Info;
if (Index == 0)
{
this->ParseTextElement(OutArray[Index], Info.Text, Info.Color);
}
else
{
this->ParseTextElement(TEXT("<") + OutArray[Index], Info.Text, Info.Color);
}
ElementList.Add(Info);
}
}
void UTestGameInstance::ParseTextElement(const FString& Str, FString& Text, int32& Color)
{
const FRegexPattern NodePattern = FRegexPattern(TEXT("<([\\w\\d\\.-]+)"));
FRegexMatcher NodeMatcher(NodePattern, Str);
if (NodeMatcher.FindNext())
{
FString NodeName = NodeMatcher.GetCaptureGroup(1);
if (!NodeName.Equals(TEXT("str")))
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("字符串格式错误,结点名必须为str"));
return;
}
}
TMap<FString, FString> AttrList;
const FRegexPattern AttrPattern = FRegexPattern(TEXT("([\\w\\d\\.-]+=[\\w\\d\\.-]+)"));
FRegexMatcher AttrMatcher(AttrPattern, Str);
while (AttrMatcher.FindNext())
{
FString MatchStr = AttrMatcher.GetCaptureGroup(1);
int32 EqualPos = MatchStr.Find("=");
FString Key = MatchStr.Mid(0, EqualPos);
FString Value = MatchStr.Mid(EqualPos+1);
AttrList.Add(Key, Value);
}
FString* ColorStr = AttrList.Find(TEXT("color"));
if (ColorStr != NULL && !ColorStr->IsEmpty())
{
Color = FCString::Atoi(**ColorStr);;
}
else
{
Color = 255;
}
const FRegexPattern TextPattern = FRegexPattern(TEXT(">([\\w\\d\\.-]+)<"));
FRegexMatcher TextMatcher(TextPattern, Str);
if (TextMatcher.FindNext())
{
Text = TextMatcher.GetCaptureGroup(1);
}
}
通过以上代码,即可把约定格式的文本解析到FTextElementInfo列表中
解析逻辑比较通用,跟引擎甚至语言无关,当然也跟显示无关
接下来看第二步,实现富文本显示
显示富文本跟具体的需求有关,以下提供一个简单的实现:
先静态创建最大数量的文本显示控件,把要显示的提示信息放到一个列表里,然后每帧重新刷新显示列表的信息,隐藏没有用上的文本控件,具体实现由于是蓝图实现就不上图了,很简单