这是siki学院的公开课内容,详细视频教程可见B站链接:
Unreal入门第一季 - 虚幻C++基础训练_哔哩哔哩_bilibili
了解玩这一套基本理论语法之后完全可以上手UE的官方教程进行练习,并对其代码有更深一层的理解。
反射与垃圾回收系统
创建UObject子类
UCLASS(Blueprintable)//指示可转化为蓝图使用,如果父类写了这里不写也可以
class MYBASICTRAINING_API UMyObject : public UObject
{
GENERATED_BODY()
};
基础宏参数介绍
实现打印及实例化一个继承自Object的类
调用类的方法
如何删除自定义的C++类
创建Actor子类和学习命名规范
在C++中创建静态网格组件
//组件继承自UObject,所以是用U开头
UPROPERTY(VisibleAnywhere,Categroy="My Actor Componets")//对于组件的标记
UStaticMeshComponent* StaticMesh;
//使用创建子物体函数(前默认为this),参数填写该组件的标识(非名字),使用指针名作为组件名
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
UPROPERTY中的参数使用简介
UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Vector")//|表示到下一级目录
FVector InitLocation;
UPROPERTY(EditAnywhere, Category = "My Actor Properties | Vector", meta = (ClampMin = -5.0f, ClampMax = 5.0f, UIMin = -5.0f, UIMax = 5.0f))//Clamp表示限制值的范围,UIMxx表示使用UI拖动时的范围
FVector LocationOffset;
物理系统
StaticMesh->AddForce(InitForce);//注意AddForce的默认参数,第三个可设置是否忽略质量进行加速
FHitResult hitResult;
AddActorLocalOffset(LocationOffset,true,&hitResult);
UE_LOG(LogTemp, Warning, TEXT("X:%f,Y:%f,Z:%f\n"), hitResult.Location.X, hitResult.Location.Y, hitResult.Location.Z);
Get/Set——Location/Rotation/Scale
FMath数学类
创建自己的Pawn类,自己的根组件并将静态网格组件附加到其上
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
//创建根组件,可能是从父类继承来的,不需要声明而是直接赋值
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
MyStaticMesh->SetupAttachment(GetRootComponent());
MyStaticMesh->SetupAttachment(RootComponent);
MyStaticMesh->AttachTo(RootComponent);
为Pwan设置摄像机
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->SetupAttachment(RootComponent);
MyCamera->SetRelativeLocation(FVector(-300.0f, 0.0f, 300.0f));
MyCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
设置GameMode使之自动持有我们建立的Pawn
按键映射与轴事件绑定
1.在头文件中声明对应的前后左右移动函数,并创建定义:
void MoveForward(float Value);
void MoveRight(float Value);
void AMyPawn::MoveForward(float Value)
{
}
void AMyPawn::MoveRight(float Value)
{
}
PlayerInputComponent->BindAxis(TEXT("MoveForward")/*操作明,在Input中的设置*/, this/**绑定到哪个类*/, &AMyPawn::MoveForward/*响应函数的引用*/);
PlayerInputComponent->BindAxis(TEXT("MoveRight")/*操作明,在Input中的设置*/, this/**绑定到哪个类*/, &AMyPawn::MoveRight/*响应函数的引用*/);
void AMyPawn::MoveForward(float Value)
{
Velocity.X = FMath::Clamp(Value, -1.0f, 1.0f)*maxSpeed;
}
void AMyPawn::MoveRight(float Value)
{
Velocity.Y = FMath::Clamp(Value, -1.0f, 1.0f)*maxSpeed;
}
void AMyPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
AddActorLocalOffset(Velocity*DeltaTime, true);
}
添加SpringArm组件
MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArm"));
MySpringArm->SetupAttachment(MyStaticMesh);
MySpringArm->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
MySpringArm->TargetArmLength = 400.0f;//作用相当于摄像机的相对位置设定
MySpringArm->bEnableCameraLag = true;//开启镜头平滑移动
MySpringArm->CameraLagSpeed = 3.0f;//平滑移动速度
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
MyCamera->SetupAttachment(MySpringArm);//将摄像机附着在SpringArm上,对摄像机的位置设定不需要了
使用C++代码设置默认的模型Mesh和材质
首先包含路径:#include “UObject/ConstructorHelpers.h”
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset(/*资源路径*/);
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshAsset(TEXT("StaticMesh'/Engine/BasicShapes/Sphere.Sphere'"));
//材质
static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("Material'/Game/StarterContent/Materials/M_Metal_Gold.M_Metal_Gold'"));
if (StaticMeshAsset.Succeeded()&& MaterialAsset.Succeeded())
{
MyStaticMesh->SetStaticMesh(StaticMeshAsset.Object);
MyStaticMesh->SetMaterial(0/*材质插槽*/,MaterialAsset.Object);
}
将StaticMesh设置为根组件
MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
RootComponent = MyStaticMesh;
控制视野上下看
void AMyPawn::LookUp(float Value)
{
MouseInput.Y = FMath::Clamp(Value, -1.0f, 1.0f);
}
void AMyPawn::LookRight(float Value)
{
MouseInput.X = FMath::Clamp(Value, -1.0f, 1.0f);
}
PlayerInputComponent->BindAxis(TEXT("LookUp"), this, &AMyPawn::LookUp);
PlayerInputComponent->BindAxis(TEXT("LookRight"), this, &AMyPawn::LookRight);
//XYZ在旋转中对应Row,Pitch,Yaw
FRotator NewSpringArmRotation = MySpringArm->GetComponentRotation();
//控制抬头和低头角度范围
NewSpringArmRotation.Pitch = FMath::Clamp(NewSpringArmRotation.Pitch += MouseInput.Y, -75.0f, 75.0f);
MySpringArm->SetWorldRotation(NewSpringArmRotation);
使用Controller实现角度旋转控制
//左右旋转
AddControllerYawInput(MouseInput.X);
使用系统继承自Controller的方式(前提是开启此继承)
注意编写代码实现的时候要注意蓝图中对应勾选栏目是否同步了