UE4C++ 设置UMG控件的Slot

1 篇文章 0 订阅

转:https://blog.csdn.net/l171672538/article/details/78874601

 

本篇介绍一下 如何使用代码来获得或者设置控件的位置锚点等。。。

 

首先看一下次控件所属插槽是什么类型的

如果当前所要操作的控件是Image1 而它的插槽是CanvasPanelSlot

那么C++中格式为:

如果所要操作的控件CanvasPanel1的Slot为HorizontalBoxSlot

C++中:

 

另贴上一些底层代码 来了解一下插槽的 Offset、Position、Size

 

 

Position.X = Left;   Y = Top;

 

Size.X = Right;  Y = Bottom;

 

OffSet

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UE5中,可以在C++类中创建Widget Component,具体步骤如下: 1. 首先在UE5编辑器中创建一个新的C++类,该类继承自Actor或Pawn等类。 2. 在该类的头文件中添加以下内容: ```cpp #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/WidgetComponent.h" #include "MyActor.generated.h" UCLASS() class MYPROJECT_API AMyActor : public AActor { GENERATED_BODY() public: AMyActor(); protected: virtual void BeginPlay() override; private: UPROPERTY(EditAnywhere, Category = "MyWidget", meta = (AllowPrivateAccess = "true")) TSubclassOf<class UUserWidget> MyWidgetClass; UPROPERTY() class UWidgetComponent* WidgetComponent; }; ``` 3. 在源文件中实现构造函数和BeginPlay函数: ```cpp #include "MyActor.h" #include "Blueprint/UserWidget.h" AMyActor::AMyActor() { PrimaryActorTick.bCanEverTick = false; // 设置Widget类,这里使用自带的Widget组件作为例子 static ConstructorHelpers::FClassFinder<UUserWidget> WidgetClass(TEXT("/Game/UMG/ExampleWidget")); if (WidgetClass.Succeeded()) { MyWidgetClass = WidgetClass.Class; } // 创建Widget Component WidgetComponent = CreateDefaultSubobject<UWidgetComponent>(TEXT("WidgetComponent")); WidgetComponent->SetupAttachment(RootComponent); WidgetComponent->SetWidgetSpace(EWidgetSpace::Screen); WidgetComponent->SetDrawAtDesiredSize(true); } void AMyActor::BeginPlay() { Super::BeginPlay(); // 创建Widget组件 if (MyWidgetClass) { UUserWidget* MyWidget = CreateWidget<UUserWidget>(GetWorld(), MyWidgetClass); if (MyWidget) { WidgetComponent->SetWidget(MyWidget); } } } ``` 在上面的代码中,我们首先在构造函数中创建Widget Component,并将其附加到该类的根组件上。然后,在BeginPlay函数中,我们使用CreateWidget函数创建Widget组件的实例,并将其设置为Widget Component的Widget。 4. 在UE5编辑器中可以设置该类的默认属性,例如,可以将该类默认的MyWidgetClass设置为自己的Widget类。 这样,我们就可以在C++类中创建Widget Component了。在使用时,只需要在UE5编辑器中将该类添加到场景中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值