这个是.h里的代码
#pragma once #include "CoreMinimal.h" #include "GameFramework/Pawn.h" #include "Components/StaticMeshComponent.h" #include "SphareBase.generated.h" UCLASS() class TEXTGAME_API ASphareBase : public APawn { GENERATED_BODY() public: // Sets default values for this pawn's properties ASphareBase(); //添加组件,设置宏为引擎到处可编辑,蓝图改写没问题 UPROPERTY(EditAnywhere, BlueprintReadWrite) UStaticMeshComponent * SphereMesh; protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; // Called to bind functionality to input virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; };
这个是.cpp里面的代码
#include "SphareBase.h" // Sets default values ASphareBase::ASphareBase() { // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; SphereMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SphereBase"));//创建组件方法 SphereMesh->SetSimulatePhysics(true);//设置物理属性开启 } // Called when the game starts or when spawned void ASphareBase::BeginPlay() { Super::BeginPlay(); } // Called every frame void ASphareBase::Tick(float DeltaTime) { Super::Tick(DeltaTime); } // Called to bind functionality to input void ASphareBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); }