【问题标题】:Attached actors are not displayed until mesh is toggled在切换网格之前,不会显示附加的演员
【发布时间】:2023-02-01 13:03:51
【问题描述】:

我有一个“收集器演员”,其中包含两个子演员,其他演员 1 和 2。 当我在编辑器中拖动这个 ACollectionActor 时,我必须手动选择子角色并在它们变得可见之前切换它们的网格。 但是,如果我在编辑器中拖入“OtherActor”,模型会立即显示出来。

有谁知道我的代码出了什么问题?

是的,“/Game/Models/other_model”在下面工作得很好,所以路径没有错误。

感谢任何...

CollectionActor.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "OtherActor.h"
#include "CollectionActor.generated.h"

UCLASS()
class IMPORTTEST_API ACollectionActor : public AActor
{
    GENERATED_BODY()
    
public: 
    // Sets default values for this actor's properties
    ACollectionActorActor();

    UPROPERTY()
    USceneComponent* Root = nullptr;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SubActor1", Meta = (MakeEditWidget = true))
    AOtherActor* SubActor1 = nullptr;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SubActor2", Meta = (MakeEditWidget = true))
    AOtherActor* SubActor2 = nullptr;
protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;
public:
};

CollectionActor.cpp文件

#include "CollectionActor.h"

#include "OtherActor.h"

// Sets default values
ACollectionActor::ACollectionActor()
{
    Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
    RootComponent = Root;

    FAttachmentTransformRules l_Rules(EAttachmentRule::KeepRelative, false);
    Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
    Mesh->AttachToComponent(Root, l_Rules);

    SubActor1 = CreateDefaultSubobject<AOtherActor>(TEXT("Other1"));
    SubActor1->AttachToActor(this, l_Rules);
    SubActor1->SetActorLocation(FVector(-10, 0, 0));

    SubActor2 = CreateDefaultSubobject<AOtherActor>(TEXT("Other2"));
    SubActor2->AttachToActor(this, l_Rules);
    SubActor2->SetActorLocation(FVector( 10, 0, 0));
}

// Called when the game starts or when spawned
void ACollectionActor::BeginPlay()
{
    Super::BeginPlay();
}

其他Actor.cpp

#include "OtherActor.h"

// Sets default values
AOtherActor::AOtherActor()
{
    Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
    RootComponent = Root;

    Mesh = CreateDefaultSubobject< UStaticMeshComponent>("Mesh");
    Mesh->AttachTo(Root);
    const ConstructorHelpers::FObjectFinder<UStaticMesh> MeshObj(TEXT("/Game/Models/other_model"));
    Mesh->SetStaticMesh(MeshObj.Object);
}

// Called when the game starts or when spawned
void AOtherActor::BeginPlay()
{
    Super::BeginPlay();
}

其他Actor.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "OtherActor.generated.h"

UCLASS()
class IMPORTTEST_API AOtherActor : public AActor
{
    GENERATED_BODY()

public: 
    // Sets default values for this actor's properties
    AOtherActor();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:
    UPROPERTY()
    USceneComponent* Root = nullptr;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    UStaticMeshComponent* Mesh = nullptr;
};

【问题讨论】:

  • 从未见过 AActor 被用作另一个 AActor 的默认子对象,我什至不知道可以编译。您可能需要考虑改用 UChildActorComponent

标签: c++ unreal-engine4 unreal-engine5


【解决方案1】:

AActor期待被世界勾选和吸引。

组件依赖于它们的父组件来实现此功能。

ComponentTick != Tick,不是文字代码,而是显示概念和其他事件,因此子 actor 不会在游戏中更新。

通过修改属性,您将强制在编辑器中进行 Actor 更新(属性更改时对象刷新)并且有效。

参与者需要被定义为 UChildActorComponent,以便为要用作组件的 AActor 提供正确的事件/回调连接。

【讨论】:

    猜你喜欢
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 2014-04-18
    • 2018-03-03
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多