1. 新建第三人称c++游戏(其它模板开局也可以的)
2. 新建继承自Actor的c++类
3. 添加静态网格体成员SM_MyActor,并在构造函数中进行相应的初始化
UCLASS()
class MYPROJECT9S_API AMyActor : public AActor
{
GENERATED_BODY()
protected:
/** Please add a variable description */
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category="Default")
TObjectPtr<UStaticMeshComponent> SM_MyActor;
// Sets default values
AMyActor::AMyActor()
{
// 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;
SM_MyActor = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
RootComponent = SM_MyActor;
// 加载静态网格资源
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset(TEXT("/Game/LevelProtoTyping/Meshes/SM_ChamferCube"));
if (StaticMeshAsset.Succeeded())
{
SM_MyActor->SetStaticMesh(StaticMeshAsset.Object);
}
// 设置纹理
static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("/Game/LevelProtoTyping/Materials/MI_Solid_Blue"));
if (MaterialAsset.Succeeded())
{
UMaterialInterface* Material = MaterialAsset.Object;
SM_MyActor->SetMaterial(0, Material);
}
}
4. BeginPlay或者Construction中初始化也是可以的
// 构造脚本函数实现
void AMyActor::OnConstruction(const FTransform& Transform)
{
// 在这里可以添加你的构造脚本代码逻辑
FString Message = TEXT("Hello World from AMyActor::OnConstruction c++!");
// 输出日志到屏幕
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, Message);
}
// 输出日志到控制台
UE_LOG(LogTemp, Warning, TEXT("%s"), *Message);
}
5. 拖一个到地图看效果
其它
- 引擎版本为5.3.2