ue4实现富文本提示
来源:互联网 发布:python 修改文件名称 编辑:程序博客网 时间:2024/05/02 07:17
游戏中需要用到富文本提示信息,即一行提示信息文字可配置多种颜色,还可以考虑加入图片(本文只实现多颜色,扩展也比较简单)
实现富文本,大体分为两步:
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)int32Color;};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列表中
解析逻辑比较通用,跟引擎甚至语言无关,当然也跟显示无关
接下来看第二步,实现富文本显示
显示富文本跟具体的需求有关,以下提供一个简单的实现:
先静态创建最大数量的文本显示控件,把要显示的提示信息放到一个列表里,然后每帧重新刷新显示列表的信息,隐藏没有用上的文本控件,具体实现由于是蓝图实现就不上图了,很简单
0 0
- ue4实现富文本提示
- NSMutableAttributedString实现富文本
- SpannableString实现富文本
- weex 实现富文本
- android上实现富文本
- NSMutableAttributedString富文本的实现
- android富文本的实现
- iOS NSMutableAttributedString 实现富文本
- JS实现富文本编辑器
- QTextEdit实现富文本记事本
- 使用NSMutableAttributedString 实现富文本
- iOS NSMutableAttributedString 实现富文本
- 富文本编辑器XHEditor实现
- 所见即所得富文本编辑器实现原理
- django + TinyMCE 实现富文本编辑器
- Javascript实现简单的富文本编辑器
- iOS使用NSMutableAttributedString实现富文本
- iOS使用NSMutableAttributedString实现富文本
- 查看某个命令是由那个包提供的
- swift类声明、继承
- VC中双击鼠标左键动态创建Edit
- android 类似qq空间微博微信九宫格图片
- 由 Runtime 所想到的
- ue4实现富文本提示
- ubuntu14.04中安装opencv2.4.9
- 【lintcode】1、A+B问题【位运算】
- 【NOIP2016提高A组五校联考4】ksum
- Redis源码目录介绍
- 如何在苹果手机上调试App
- 用Android studio进行类复制的时候是不区分大小写的,注意!
- mysql命令
- Swift 学习笔记---Inheritance