D1
D1 - 1 BindAxis
void AFBPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
//绑定周映射
PlayerInputComponent->BindAxis(TEXT("MoveRight"),this,&ThisClass::MoveRight);
}
void AFBPawn::MoveRight(float AxisScale)
{
if(AxisScale != 0 ) {UE_LOG(LogTemp,Error,TEXT("%f"),AxisScale);}
}
D1 - 2 启用 Block 碰撞检测
- .h 回调方法定义必须加 UFUNCTION()
UFUNCTION()
void HttEvent(UPrimitiveComponent* HitComponent , AActor* OtherActor , UPrimitiveComponent* OtherComponent ,FVector NormalImpulse , const FHitResult& Hit);
- .CPP
AFBPawn::AFBPawn()
{
FlipBookComponent->SetNotifyRigidBodyCollision(true); //启用 block 碰撞
FlipBookComponent->OnComponentHit.AddDynamic(this,&ThisClass::AFBPawn::HttEvent);
}
void AFBPawn::HttEvent(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent,
FVector NormalImpulse, const FHitResult& Hit)
{
if(GEngine)
{
GEngine->AddOnScreenDebugMessage(-1,90,FColor::Red,FString::Printf(TEXT("%s"),*OtherActor->GetName()));
}
}
D1 - 3 材质UV移动
D1 - 4 设置相机视野
D2
D2 - 1 关闭自动保存地图
D2 - 2 定时器
D2 - 1 - 1 定义并使用定时器
- .h 定义 FTimerHandle 类型变量
- CPP 启动定时器
D2 - 1 - 2 调用其他类的方法
D3 - 1 - 3 其他方法 - 停止 - 暂停 - 继续
GetWorld()->GetTimerManager().ClearTimer(Timer);
GetWorld()->GetTimerManager().PauseTimer(Timer);
GetWorld()->GetTimerManager().UnPauseTimer(Timer);
D2 - 3 动态材质
D3
D3 - 1 获取 通过 PlayerController 获取 HUD
AFBGameMode::AFBGameMode()
{
PrimaryActorTick.bCanEverTick = true;
PC = UGameplayStatics::GetPlayerController(GetWorld(),0);
}
void AFBGameMode::UpdateGameState(EGameState NewState)
{
/**
*显示 GameOver Icon 获取HUD
*/
auto HUD = PC->GetHUD();
auto BirdHUD =Cast<ABirdHUD>(HUD);
//显示 GameOver HUD
BirdHUD->ShowGameOverHUD();
//关闭切换夜昼的计时器
GetWorld()->GetTimerManager().ClearTimer(TimerHandle);
break;
}
D3 - 2 加载关卡
//重新开始游戏
UGameplayStatics::OpenLevel(GetWorld(),TEXT("StarterMap"));
D3 - 3 退出游戏
//退出游戏
UKismetSystemLibrary::QuitGame(GetWorld(),PC,EQuitPreference::Quit,false);
D3 - 4 根据 Z Velocity 控制旋转角度
D3 - 4 - 1 夹逼 - Clamp
//范围限制
const float RotateY = FMath::Clamp<float>(ZRotator,-30,30);
D3 - 4 - 2 范围映射
//范围映射
float Alpha = UKismetMathLibrary::MapRangeClamped(ZVelocityValue,-650.0f,500.0f,0.0f,1.0f);
D3 - 4 - 3 线性插值
//线性插值
float Pitch = UKismetMathLibrary::Lerp(-30,30,Alpha);
D3 - 4 - 4 获取轴速度分量
const float ZVelocityValue = FlipbookComp->GetPhysicsLinearVelocity().Z;
D3 - 4 - 5 Code
void AFBPawn::UpdateBirdPitch()
{
const float ZVelocityValue = FlipbookComp->GetPhysicsLinearVelocity().Z;
//范围映射
float Alpha = UKismetMathLibrary::MapRangeClamped(ZVelocityValue,-650.0f,500.0f,0.0f,1.0f);
//线性插值
float Pitch = UKismetMathLibrary::Lerp(-30,30,Alpha);
//数值夹逼
//const float RotateY = FMath::Clamp<float>(ZRotator,-30,30);
FlipbookComp->SetRelativeRotation(FRotator{Pitch,0,0});
}
D3 - 5 延迟播放两段声音 FTimerHandle 异步延时
- 这里举这个例子就是利用定时器想实现异步延时执行效果,而不是Delay()这样的同步阻断下面的代码执行
D3 - 6 SaveGame
D3 - 6 - 1 四步
- 创建 Save Game
UGameplayStatics::CreateSaveGameObject();
- 保存 SaveGame
UGameplayStatics::SaveGameToSlot()
- 判断保存游戏是否存在
UGameplayStatics::DoesSaveGameExist();
- 加载 Save Game
UGameplayStatics::LoadGameFromSlot();
D3 - 6 - 1 SaveGame 子类
- 属性必须加 UPROPERTY() MACRO.
- SaveGame 子类不能是指针类型.
D3 - 6 - 2 创建 蓝图函数库工具类
D3 - 7 AsyncSaveGame - Bind λ
- 异步保存不占用线程
D3 - 8 syncSaveGame - Bind UObject
D3 - 9 Curve - 曲线
D3 - 9 - 1 Curve->GetTimeRange(float& Mn , float& Max)
D3 - 9 - 2 Curve->GetFloatValue() 获取曲线因变量
D4
D4 - 1 Macro - UPROPERTY()
D4 - 2 Macro - UFUNCTION()
D4 - 2 - 1 BlueprintCallable
- 蓝图可调用
D4 - 2 - 2 BlueprintImplementableEvent
- 方法声明无法在CPP文件只能在 蓝图 中实现 , 多用于 时间轴功能.
- 声明
- 在蓝图中进行实现 , 来个时间轴
- 调用
D4 - 2 - 3 BlueprintNativeEvent
-
既可以在蓝图中实现,也可以zaiCPP中实现,与BlueprintImplementableEvent冲突.
-
方法实现需要在方法名后面加_Implementation
-
根据有无返回值,在蓝图中反应为自定义事件或者方法
-
默认 蓝图实现 优先.
- .h 声明
- 可以再 . CPP 中实现
- 也可以在 蓝图中实现
- 如果 CPP 与 蓝图中一起实现了 , 那么只会执行蓝图实现的逻辑 , 如果想要一起实现 , 需要调用父类的方法.
D4 - 2 - 4 BlueprintPure - 纯函数
D4 - 2 - 5 EXEC - 执行命令行命令 ~
- .h
- .cpp
- Test
D4 - 2 - 6 UFUNCTION() - Ref as Param
D4 - 3 BP AS Default Pawn
- 之前一直用CPP类的类作为 Default Pawn , 这次换 BP
D4 - 3 - 1 FClassFinder 查找蓝图类
- 路径结尾必须 以 _C 结束. "------------_C' "
ConstructorHelpers::FClassFinder<ASMCharacter> SMCharacterBPFinder (TEXT("/Script/Engine.Blueprint'/Game/Blueprints/BP_SMCharacter.BP_SMCharacter_C'"));
D4 - 4 BlueprintType & NotBlueprintType
- 用于 UClass() , USTRUCT() , UENUM() 中.
- BlueprintType 表示可以作为蓝图参数,反之不可以
- UClass() 默认为 BlueprintType
- USTRUCT() , UENUM() 默认为 NotBlueprintType
D4 - 5 Bluepritable & NotBlueprintable
- 表示是否可以 派生出子类 Final -
D4 - 6 结构体
D4 - 6 - 1 定义
D4 - 6 - 2 创建蓝图变量
D4 - 7 FString API
D4 - 7 - 1 字符串拼接
D4 - 7 - 2 T To FString
D4 - 7 - 3 字符串比较
D4 - 7 - 4 Replace
D4 - 7 - 5 Split two part
D4 - 7 - 6 Split to Array - ParseToArray
D5
D5 - 1 FName
D5 - 1 - 1 初始化
D5 - 1 - 2 判断相等 ==
D5 - 1 - 3 IsEqual
D5 - 1 - 4 FName 转 FString
D5 - 2 FText
D5 - 2 - 1 初始化1
//FText 初始化1
FText TempText = NSLOCTEXT("SuperMario","Hello","Hello");
D5 - 2 - 2 使用 FString 初始化 FText
FString Str = TEXT("OK");
FText Text = FText::FromString(Str); //使用 FString 初始化 FText
D5 - 2 - 3 FText.Format("{0},{1}")
- 占位符 {} , 必须是 FText 类型.
FString Str = TEXT("OK");
FText Text = FText::FromString(Str); //使用 FString 初始化 FText
FString StrNum = FString::FromInt(5);
FText Text1= FText::FromString(StrNum);
FText FinalText = FText::Format(NSLOCTEXT("SuperMario","Final","{0} got {1} coins"),Text, Text1);
UE_LOG(LogTemp,Warning,TEXT("%s"),*FinalText.ToString());
D5 - 3 Texture素材裁切
D5 - 3 - 1 各种属性
- Gird : 按固定面积裁切.
- Cell Width : 单位宽度.
- Cell Height : 单位高度.
- Num Cells X : 横向裁切个数.
- Num Cells Y : 纵向 裁切个数.
- MarginX : 从左往右侧空像素.
- MarginY : 虫上往下侧空像素.
D5 - 3 - 2 Extract 裁切生成 sprite
D5 - 4 AirControl
- 改变在空中的时候受到控制的比率
GetCharacterMovement()->AirControl = 1.0;
D5 - 5 屏幕坐标 转 世界坐标
D5 - 5 - 1 获取 Viewport Size(&X , &Y)
int32 X;
int32 Y;
PC->GetViewportSize(X, Y);
D5 - 5 - 2 屏幕坐标转为世界坐标
// 把这个坐标转化成世界坐标
FVector Location;
FVector Direction; //ScreenX ScreenY & &
PC->DeprojectScreenPositionToWorld(0, 0, Location, Direction);