嘿,我一直试图在运行时单击 StaticMeshComponent,但无法单击特定的 StaticMeshComponent。我尝试了以下逻辑来单击并在 OnClick() 函数中设置材料,但无法成功。AddDynamic 方法是否正确?
在.cpp
StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponentCOMP"));
ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMesh_obj(TEXT("/Game/StarterContent/Meshs/Chairs_Chair1"));
StaticMeshComponent->SetStaticMesh(StaticMesh_obj.Object);
StaticMeshComponent->OnClicked.AddDynamic(this, &AMyActor::OnClick);
StaticMeshComponent->AttachTo(RootComponent);
// OnClick Function
void AMyActor::OnClick(UPrimitiveComponent* pComponent)
{
ConstructorHelpers::FObjectFinder<UMaterial> MeshMaterial(TEXT("/Game/GTFreeMaterials/Materials/Metal_BrushedSteel"));
// Set properties for Staic mesh component
StaticMeshComponentArray[i]->SetMaterial(0, MeshMaterial.Object);
FMessageDialog::Open(EAppMsgType::Ok, FText::FromString(TEXT("Clicked")));
}
在.h
void OnClick(UPrimitiveComponent* pComponent);
你们能否在这个问题上帮助我或指导我任何其他有效的逻辑来解决我的问题。
PS:游戏模式:鼠标点击模式