c++與umg組建互動
阿新 • • 發佈:2018-11-22
1 新建一個 umg c++類MyUserWidget繼承與userWidget
2 建立藍圖類BP_MyUserWidget繼承於MyUserWidget
3 在 BP_MyUserWidget 下建立文字塊,取名為Name
4 class setting下設定父類為MyUserWidget
5 開啟vs編寫c++程式碼
MyUserWidget.h
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "Blueprint/UserWidget.h" #include "Public/Components/TextBlock.h" #include "MyUserWidget.generated.h" /** * */ UCLASS() class TEST_API UMyUserWidget : public UUserWidget { GENERATED_BODY() public: virtual bool Initialize() override; public: //反射繫結,通過繫結umg組建名獲得組建物件 UPROPERTY(Meta = (BindWidget)) UTextBlock* Name; };
MyUserWidget.cpp
// Fill out your copyright notice in the Description page of Project Settings. #include "MyUserWidget.h" bool UMyUserWidget::Initialize() { if (!Super::Initialize()) { return false; } //獲得文字塊組建改變文字 Name = Cast<UTextBlock>(GetWidgetFromName(FName(TEXT("Name")))); //FString轉成FText FText a = FText::FromString(TEXT("abc")); //改變文字值 Name->SetText(a); return true; }