UE常见变量并且在【细节面板】中进行修改

  1. 在UE4中新建一个C++类:TestActor,继承子Actor ,UE4会自动在类的前面添加上大写字母A,最后生成的类是:ATestActor;
    在这里插入图片描述
  2. 在UE4的内容面板中会出现新建Actor对象
    在这里插入图片描述
    在VS2019编辑器中,会出现对应的类:TestActor.h和TestActor.cpp
    在这里插入图片描述
    TestActor.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

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

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

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

TestActor.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "TestActor.h"

// Sets default values
ATestActor::ATestActor()
{
 	// 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;

}

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

// Called every frame
void ATestActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}
  1. 在头文件中添加向量变量FVector,并且添加上蓝图属性
	UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category="MyVars")
	FVector initLocation = FVector(0.0, 0.0, 0.0);

注:
EditInstanceOnly:只有ATestActor实例化以后才可以编辑
BlueprintReadWrite:可以在蓝图中读写
Category:表示属性类别为"MyVar"

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

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

	UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category="MyVars")
	FVector initLocation = FVector(0.0, 0.0, 0.0);
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

4.把新建的TestActor拖拽到场景中,在场景中什么也看不到(之所以看不到是因为没有添加组件),但是可以在世界面板中看到场景中已经有了一个ATestActor的对象实例。并且在细节面板中可以看到,新定义的变量类型“MyVars”
在这里插入图片描述
5.添加组件,并且设置组件是在蓝图中随处可见“VisibleAnywhere”,定义类别:“MyMess”

	UPROPERTY(VisibleAnywhere, Category = "MyMess")
	UStaticMeshComponent* mess = nullptr;

在构造函数中需要初始化一个对象

mess = CreateDefaultSubobject<UStaticMeshComponent>("TestMess");
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

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

	UPROPERTY(VisibleAnywhere, Category = "MyMess")
	UStaticMeshComponent* mess = nullptr;

	UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category="MyVars")
	FVector initLocation = FVector(0.0, 0.0, 0.0);
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

// Fill out your copyright notice in the Description page of Project Settings.


#include "TestActor.h"

// Sets default values
ATestActor::ATestActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;
	mess = CreateDefaultSubobject<UStaticMeshComponent>("TestMess");
}

// Called when the game starts or when spawned
void ATestActor::BeginPlay()
{
	Super::BeginPlay();
	SetActorLocation(initLocation);//开始运行的时候,位置调整到当前位置
}

// Called every frame
void ATestActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

回到UE4Editor中,把内容面板中中TestActor拖动到场景中,可以在细节面板中可以看到对应的组件信息,并且可以选择不同的静态网格体。
在这里插入图片描述

aaa

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值