1. 程式人生 > 實用技巧 >UE4C++(1)新增元件

UE4C++(1)新增元件

這個是.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); }