记录一些 UE 资源、小知识点、遇到的问题
资源 UE蓝图基础_@大宁字的博客-CSDN博客
UE蓝图进阶_@大宁字的博客-CSDN博客
新手指南 | Epic Developer Community (epicgames.com)
业界大佬们:
大钊 - 知乎 (zhihu.com)
南京周润发 - 知乎 (zhihu.com)
Jiff - 知乎 (zhihu.com)
大侠刘茗Marin - 知乎 (zhihu.com)
Jerish - 知乎 (zhihu.com)
陶仁贤 - 知乎 (zhihu.com)
安柏霖 - 知乎 (zhihu.com)
顾煜 - 知乎 (zhihu.com)
小知识 输出 1 2 3 4 5 6 7 8 9 10 11 12 UE_LOG (LogTemp, Warning, TEXT ("Hello" ));UE_LOG (LogTemp, Error, TEXT ("Hello" ));UE_LOG (LogTemp, Log, TEXT ("Hello" ));GEngine->AddOnScreenDebugMessage (-1 , 20 , FColor::Yellow, "Hello" ); FString fs = "asd" ; UE_LOG (LogTemp, Error, TEXT ("%s" ), *fs);
获取 获取,设置 Actor 的位置和旋转 1 2 3 4 5 6 FVector NewLocation = GetActorLocation (); FRotator NewRotation = GetActorRotation (); SetActorLocationAndRotation (NewLocation, NewRotation);
获取 GameState 1 2 3 AMyGameStateBase* GS = Cast <AMyGameStateBase>(GetWorld ()->GetGameState ()); GetWorld () 是 Actor 里面的函数;
获取 GameMode 1 2 3 4 5 6 7 8 9 #include "Kismet/GameplayStatics.h" const AMyGameModeBase* GM = Cast <AMyGameModeBase>(UGameplayStatics::GetGameMode (this ));AMyGameModeBase* GM = Cast <AMyGameModeBase>(GetWorld ()->GetAuthGameMode ()); const AMyGameModeBase* GM = Cast <AMyGameModeBase>(GetDefaultGameMode ());
通过反射,直接获取蓝图中配置的信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 FString AIPath = "Blueprint'" ; AIPath.Append (M_SoldierInfoList[i].ToString ()); AIPath.Append ("'" ); UClass* AIClass = LoadClass <AAICharacter_Base>(NULL , *AIPath); if (AIClass){ AAICharacter_Base* AI_Base = Cast <AAICharacter_Base>(AIClass->GetDefaultObject ()); if (AI_Base) { } }
加载 从 c++ 中加载 UI 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 FString HUDClassLoadPath = FString (TEXT ("Blueprint'/Game/Blueprints/HUD.HUD_C'" )); auto MyUIClass = LoadClass <UUserWidget>(NULL , *HUDClassLoadPath);if (MyUIClass != nullptr ){ UUserWidget* MyUI = CreateWidget <UUserWidget>(GetWorld (), MyUIClass); if (MyUI != nullptr ) MyUI->AddToViewport (); } FString UiPath = TEXT ("/Script/UMGEditor.WidgetBlueprint'/Game/Blueprints/HUD.HUD_C'" ); UClass* MyUIClass = LoadClass <UUserWidget>(nullptr , *UiPath); if (MyUIClass){ UUserWidget* MyUI = UWidgetBlueprintLibrary::Create (GWorld, MyUIClass, nullptr ); MyUI->AddToViewport (); }
在 C++ 代码中设置 GameMode 1 2 3 4 5 6 7 8 9 10 ATestGameGameMode::ATestGameGameMode () { GameStateClass = ATestGameState::StaticClass (); PlayerControllerClass = ATestPlayerController::StaticClass (); PlayerStateClass = ATestPlayerState::StaticClass (); HUDClass = ATestHUD::StaticClass (); DefaultPawnClass = ATestPawn::StaticClass (); SpectatorClass = ATest_SpectatorPawn::StaticClass (); }
C++类 加载 蓝图类 1 2 3 4 5 6 7 8 9 10 11 FString CharacterClassLoadPath = FString (TEXT ("Blueprint'/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter.ThirdPersonCharacter_C'" )); DefaultPawnClass = LoadClass <AMyProject02Character>(NULL , *CharacterClassLoadPath); FString s = "Blueprint'" ; s.Append (ClassPath.ToString ()); s.Append ("'" ); UClass* BP_ProjectileClass = LoadClass <AProjectile>(NULL , *s);
创建销毁 创建组件,初始化组件,使用组件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 UStaticMeshComponent *VisualMesh = nullptr ; VisualMesh = CreateDefaultSubobject <UStaticMeshComponent>(TEXT ("VisualMesh" )); RootComponent = Cast <USceneComponent>(VisualMesh); Box->SetupAttachment (VisualMesh); VisualMesh->SetCollisionProfileName (TEXT ("BlockAllDynamic" )); static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset (TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube" )) ; if (CubeVisualAsset.Succeeded ()){ VisualMesh->SetStaticMesh (CubeVisualAsset.Object); } VisualMesh->SetRelativeLocation (FVector (0.0f , 0.0f , 0.0f ));
创建、销毁Actor 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 AMyBox *mybox = GetWorld ()->SpawnActor <AMyBox>(SpawnLocation, SpawnRotation); mybox->destroy (); AMyEffect *MyEffect = GetWorld ()->SpawnActor <AMyEffect>(); MyEffect->AttachToActor (this , FAttachmentTransformRules::KeepRelativeTransform); AProjectile* NewProjectile = Cast <AProjectile>(UGameplayStatics::BeginDeferredActorSpawnFromClass (this , BP_ProjectileClass, SpawnTransform, ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn)); if (NewProjectile) { NewProjectile->SetOwner (this ); NewProjectile->Init (this ); } UGameplayStatics::FinishSpawningActor (NewProjectile, SpawnTransform);
粒子效果的使用实例 1 2 3 4 5 6 7 8 9 10 11 class UParticleSystem * PickUpFX;class UParticleSystem * StaticFX;UGameplayStatics::SpawnEmitterAtLocation (this , PickUpFX, GetActorLocation ()); UGameplayStatics::SpawnEmitterAttached (StaticFX, MeshComp); https:
数学计算 计算两个位置的距离 1 FVector::Distance (FVector1, Fvector2);
随机数的使用 1 2 #include "Kismet/KismetMathLibrary.h" float RandomX = UKismetMathLibrary::RandomFloatInRange (MIN_X, MAX_X);
时间的使用 1 2 3 4 5 6 7 8 9 10 11 12 double CurrentTime = FDateTime::Now ().GetTimeOfDay ().GetTotalMilliseconds ();FDateTime tt = FDateTime::Now (); int year = tt.GetYear ();int month = tt.GetMonth ();int day = tt.GetDay ();int hour = tt.GetHour ();int minute = tt.GetMinute ();int second = tt.GetSecond ();UE_LOG (LogTemp, Error, TEXT ("Time : %d, %d, %d, %d, %d, %d" ), year, month, day, hour, minute, second);
定时器的使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 FTimerHandle TimerHandle; GetWorldTimerManager ().SetTimer (TimerHandle, this , &AMyEffect::OnDestroyed, 2.0f , false );GetWorldTimerManager ().IsTimerActive (M_TimerHandle) == true ;GetWorldTimerManager ().ClearTimer (M_TimerHandle);https:
AI 运行行为树 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 UPROPERTY () class UBehaviorTreeComponent * M_BehaviorTree; UPROPERTY () class UBlackboardComponent * M_Blackboard; UPROPERTY (EditAnywhere, Category = "BaseConfig" ) class UBehaviorTree * BTree = nullptr ; UBehaviorTree* AAICharacter_Base::GetBTree () { return BTree; } void AAIController_Base::RunAIBehaviorTree (APawn* InPawn) { AAICharacter_Base* AI = Cast <AAICharacter_Base>(InPawn); if (AI && AI->GetBTree ()) { M_Blackboard->InitializeBlackboard (*AI->GetBTree ()->BlackboardAsset); M_BehaviorTree->StartTree (*AI->GetBTree ()); } }
使用感知组件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include "Perception/AISenseConfig_Sight.h" #include "Perception/AIPerceptionComponent.h" UPROPERTY ()class UAISenseConfig_Sight * SightConfig = nullptr ;UPROPERTY ()class UAIPerceptionComponent * M_AIPerception = nullptr ;AAIController::AAIController () { M_AIPerception = CreateDefaultSubobject <UAIPerceptionComponent>("AIPerception" ); SightConfig = CreateDefaultSubobject <UAISenseConfig_Sight>(TEXT ("AISightConfig" )); if (M_AIPerception) { M_AIPerception->ConfigureSense (*SightConfig); M_AIPerception->SetDominantSense (SightConfig->GetSenseImplementation ()); } } void AAIController::BeginPlay () { Super::BeginPlay (); if (M_AIPerception) { M_AIPerception->OnTargetPerceptionUpdated.AddDynamic (this , &AAIController::OnTargetPerceptionUpdated); } UAIPerceptionSystem::RegisterPerceptionStimuliSource (this , UAISense_Sight::StaticClass (), this ); } void AAIController_CloseCombat::InitAIPerception (APawn* InPawn) { M_AIPerception->ConfigureSense (*SightConfig); }
枚举的使用 1 2 3 4 5 6 7 8 9 10 11 UENUM (BlueprintType)enum class ECampType : uint8{ NONE, RED = 1 UMETA (DisplayName = "Red" ), BLUE = 2 UMETA (DisplayName = "Blue" ), }; UEnum* const CampTypeEnum = StaticEnum <ECampType>(); FString s = CampTypeEnum->GetDisplayNameTextByValue (static_cast <uint8>(ECampType::RED)).ToString ();
遍历 TMap 1 TMap<FString, int >::TConstIterator iter = _Map.CreateConstIterator (); iter; ++iter)
问题 导入项目时,vs显示不支持? 1 2 3 答:vs版本不匹配,可以去网上找找 vs 和 ue 版本的匹配程度;我是: ue 4.24 <=> vs 2017 ue 4.27 <=> vs 2022
换了之后 vs 编译时显示 sdk 有问题? 1 2 3 4 5 6 7 8 9 这个搞了好久...盲猜删2017 的时候给删掉了... 报错信息: 参考这篇文章: https: