如題,當一個類中的屬性發生了改變,從而引發的事件。
這里我們定義一個屬性show,當這個屬性發生改變的時候,引發了模型的visibility事件。
virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;由父類繼承而來,專門處理屬性改變的事件。
1 // Fill out your copyright notice in the Description page of Project Settings.
2
3 #pragma once
4
5 #include "CoreMinimal.h"
6 #include "Engine/StaticMeshActor.h"
7 #include "UObject/ConstructorHelpers.h"
8 #include "Host.generated.h"
9
13 UCLASS()
14 class MYPROJECT11_API AHost : public AStaticMeshActor
15 {
16 GENERATED_BODY()
17
18 public:
19 AHost();
20 UPROPERTY(EditAnywhere)
21 bool show;
22 virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
23 UStaticMeshComponent* myMesh;
24 };
源文件:
1 // Fill out your copyright notice in the Description page of Project Settings.
2
3
4 #include "Host.h"
5 #include "Components/StaticMeshComponent.h"
6
7 AHost::AHost() {
8 PrimaryActorTick.bCanEverTick = true;
9 show = true;
10 myMesh = GetStaticMeshComponent();
11
12 auto asset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cone.Cone'"));
13
14 if (asset.Succeeded()) {
15 myMesh->SetStaticMesh(asset.Object);
16 myMesh->SetGenerateOverlapEvents(true);
17 myMesh->SetVisibility(show);
18 myMesh->SetMobility(EComponentMobility::Movable);
19 myMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
20 myMesh->SetCollisionObjectType(ECC_WorldDynamic);
21 }
22
23 }
24
25 void AHost::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
26 {
27 if (PropertyChangedEvent.Property != nullptr) {
28 const FName PropertyName = PropertyChangedEvent.Property->GetFName(); //獲得該類所有屬性的名字
29
30 //const FString PropertyName = PropertyChangedEvent.Property->GetName();
31 //PropertyName == GET_MEMBER_NAME_STRING_CHECKED(AHost,show)
32
33 if (PropertyName == GET_MEMBER_NAME_CHECKED(AHost,show)) { //當這個屬性名字屬於AHost類,且屬性名字為show時候,myMesh改變visibility。
34 myMesh->SetVisibility(show);
35 }
36 }
37 Super::PostEditChangeProperty(PropertyChangedEvent); //調用父類的PostEditChangeProperty方法。
38
39 }
其實很多情況可以直接通過包含來完成,這種方式只是更加安全。
原文鏈接:https://blog.csdn.net/weixin_43654485/article/details/108151949