UE蓝图中变量的建立
先在C++的.h下设置变量名和属性
如下所示:
//场景中编辑,蓝图可读写
UPROPERTY(EditInstanceOnly,BlueprintReadWrite,Category= "FloaterVar")
FVector InitialLoction;//=FVector(0);
//任意窗口可见,蓝图只读
UPROPERTY(EditAnywhere,BlueprintReadOnly,Category= "FloaterVar")
//自定义的逻辑变量
bool bShouldFloat;
//蓝图窗口可编辑,蓝图可读写
UPROPERTY(EditDefaultsOnly,BlueprintReadWrite,Category= "FloaterVar")
bool bInitializeFloaterLocation;
然后在.cpp中初始化变量,并使用变量
如下所示:
AFloater::AFloater()
{
//构造函数
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CustomStaticMesh"));
//变量初始化
InitialLoction = FVector(0);
bInitializeFloaterLocation = false;
bShouldFloat = false;
InitialDirection = FVector(1,0,0);
}
// Called when the game starts or when spawned
void AFloater::BeginPlay()
{
Super::BeginPlay();
if (bInitializeFloaterLocation) {
SetActorLocation(InitialLoction);
}
}
// Called every frame
//这是Tick事件
void AFloater::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bShouldFloat)
{
AddActorLocalOffset(InitialDirection);
}
}

浙公网安备 33010602011771号