UE C++学习笔记之创建组件

发布于:2025-05-28 ⋅ 阅读:(17) ⋅ 点赞:(0)

UE.h头文件:


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Components/BoxComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/SplineComponent.h"


#include "SnakeHead.generated.h"



UCLASS()
class SNAKEGAME_API ASnakeHead : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	ASnakeHead();
	UPROPERTY(VisibleAnywhere)//添加到虚幻引擎中在任何地方可视
	UStaticMeshComponent* Mesh;//mesh组件

	UPROPERTY(VisibleAnywhere)//添加到虚幻引擎中在任何地方可视
	UBoxComponent* Collision; //碰撞组件

	UPROPERTY(VisibleAnywhere)//添加到虚幻引擎中在任何地方可视
	UCameraComponent* Camera;//摄像机组件
	UPROPERTY(VisibleAnywhere)//添加到虚幻引擎中在任何地方可视
    USplineComponent* Spline;//添加虚幻样条线组件

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;

};

UE.cpp文件:

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


#include "SnakeHead.h"

// Sets default values
ASnakeHead::ASnakeHead()
{
 	// 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;
	
	Collision = CreateDefaultSubobject<UBoxComponent>(TEXT("Collision"));//创建碰撞物体名字是Collision
    RootComponent = Collision;//为Actor根组件赋值(设置根节点是Collision)
    Collision->InitBoxExtent(FVector(50.0f, 50.0f, 50.0f));//初始化碰撞体大小


    Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));//创建mesh组件名字是Mesh
    Mesh->SetupAttachment(RootComponent);//把mesh的父亲设置为RootComponent
    Mesh->SetWorldLocationAndRotation(FVector(50, 50, 50), FRotator(0, -90, 0));//设置网格体的位置和旋转


    Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera")); // 创建Camera组件名字是Camera
    Camera->SetupAttachment(RootComponent);//把Camera的父亲设置为RootComponent
    Camera->SetWorldLocationAndRotation(FVector(0, 0, 1000), FRotator(-90, 0, 0));//设置网格体的位置和旋转
    Camera->SetAbsolute(false, true, false);//把摄像机设置为绝对坐标(位置false,旋转true,大小false)

    Spline = CreateDefaultSubobject<USplineComponent>(TEXT("Spline"));//创建样条线组件名字是Spline
    Spline->SetupAttachment(RootComponent);//把样条线Spline的父亲设置为RootComponent
    Spline->SetAbsolute(true, true, true);//把样条线Spline设置为绝对坐标(位置true,旋转true,大小true)

}

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

	
}

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

}

// Called to bind functionality to input
void ASnakeHead::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}



网站公告

今日签到

点亮在社区的每一天
去签到