1、获得UClass方法:
#include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h"
static ConstructorHelpers::FClassFinder<UUserWidget> hud_widget_class(TEXT("WidgetBlueprint'/AirSim/Blueprints/BP_SimHUDWidget'"));
UClass* widget_class_ = hud_widget_class.Succeeded() ? hud_widget_class.Class : nullptr;
2、生成Widget
USimHUDWidget* widget_ = CreateWidget<USimHUDWidget>(player_controller, widget_class_);
widget_->AddToViewport();
3、创建对话框
UAirBlueprintLib::ShowMessage(EAppMsgType::Ok, std::string("Error at startup: ") + ex.what(), "Error");
EAppReturnType::Type UAirBlueprintLib::ShowMessage(EAppMsgType::Type message_type, const std::string& message, const std::string& title)
{
FText title_text = FText::FromString(title.c_str());
return FMessageDialog::Open(message_type,
FText::FromString(message.c_str()),
&title_text);
}
4、代码方式执行console,设置参数。
//use two different methods to set console var because sometime it doesn't seem to work
static const auto custom_depth_var = IConsoleManager::Get().FindConsoleVariable(TEXT("r.CustomDepth"));
custom_depth_var->Set(3);
//Equivalent to enabling Custom Stencil in Project > Settings > Rendering > Postprocessing
UKismetSystemLibrary::ExecuteConsoleCommand(GetWorld(), FString("r.CustomDepth 3"));
//during startup we init stencil IDs to random hash and it takes long time for large environments
//we get error that GameThread has timed out after 30 sec waiting on render thread
static const auto render_timeout_var = IConsoleManager::Get().FindConsoleVariable(TEXT("g.TimeoutForBlockOnRenderFence"));
render_timeout_var->Set(300000);
5、代码方式开启GameViewport()的各项参数。
GetWorld()->GetGameViewport()->GetEngineShowFlags()->SetMotionBlur(false);
6、屏幕输出:
UENUM(BlueprintType)
enum class LogDebugLevel : uint8 {
Informational UMETA(DisplayName = "Informational"),
Success UMETA(DisplayName = "Success"),
Failure UMETA(DisplayName = "Failure"),
Unimportant UMETA(DisplayName = "Unimportant")
};
void UAirBlueprintLib::LogMessage(const FString &prefix, const FString &suffix, LogDebugLevel level, float persist_sec)
{
if (log_messages_hidden_)
return;
static TMap<FString, int> loggingKeys;
static int counter = 1;
int key = loggingKeys.FindOrAdd(prefix);
if (key == 0) {
key = counter++;
loggingKeys[prefix] = key;
}
FColor color;
switch (level) {
case LogDebugLevel::Informational:
color = FColor(147, 231, 237);
//UE_LOG(LogTemp, Log, TEXT("%s%s"), *prefix, *suffix);
break;
case LogDebugLevel::Success:
color = FColor(156, 237, 147);
//UE_LOG(LogTemp, Log, TEXT("%s%s"), *prefix, *suffix);
break;
case LogDebugLevel::Failure:
color = FColor(237, 147, 168);
//UE_LOG(LogAirSim, Error, TEXT("%s%s"), *prefix, *suffix);
break;
case LogDebugLevel::Unimportant:
color = FColor(237, 228, 147);
//UE_LOG(LogTemp, Verbose, TEXT("%s%s"), *prefix, *suffix);
break;
default: color = FColor::Black; break;
}
if (GEngine) {
GEngine->AddOnScreenDebugMessage(key, persist_sec, color, prefix + suffix);
}
//GEngine->AddOnScreenDebugMessage(key + 10, 60.0f, color, FString::FromInt(key));
}
7, 遍历world中所有Actor,并挑选目标Actor
typedef AUrdfBotPawn TVehiclePawn;
void ASimModeUrdfBot::getExistingVehiclePawns(TArray<AirsimVehicle*>& pawns) const
{
for (TActorIterator<TVehiclePawn> it(this->GetWorld()); it; ++it)
{
pawns.Add(static_cast<AirsimVehicle*>(*it));
}
}