FString类是由存放TCHAR的TArray构成的。TCHAR是unicode编码的。
FString class is built upon a TArray of TCHARs. TCHAR (native Unicode encoding).
设置字面量时要用TEXT(),它会将ANSI的字面量转换成TCHAR。例如:
FString txt = TEXT("中国人");
std::string to FString
// uft8编码的string
std::string str_uft8 = u8"中国人";
FString txt = UTF8_TO_TCHAR(str_uft8.c_str());
// 若只含ASCII
std::string TestString = "Happy";
FString HappyString(TestString.c_str());
FString to std::string
FString txt = TEXT("中国人");
// uft8编码的string
std::string str_uft8 = TCHAR_TO_UTF8(*txt);
FString to std::wstring
FString txt = TEXT("中国人");
std::wstring ws = txt.GetCharArray().GetData();
std::wstring to FString
std::wstring ws = L"中国人";
FString txt(ws.c_str());
To FString
// float
FString::SanitizeFloat(FloatVariable);
// int
FString::FromInt(IntVariable);
// bool
InBool ? TEXT("true") : TEXT("false");
// FVector
VectorVariable.ToString();
// FVector2D
Vector2DVariable.ToString();
// FRotator
RotatorVariable.ToString();
// FLinearColor
LinearColorVariable.ToString();
// UObject
(InObj != NULL) ? InObj->GetName() : FString(TEXT("None"));
// FName
TestHUDString = TestHUDName.ToString();
// FText
TestHUDString = TestHUDText.ToString();
// int64
FString::Printf(TEXT("%lld"), int64_val);
// uint64
FString::Printf(TEXT("%llu"), uint64_val);
From FString
// bool
TestHUDString.ToBool();
// int
FCString::Atoi(*TestHUDString);
// float
FCString::Atof(*TestHUDString);
// FName
TestHUDName = FName(*TestHUDString);
// FText
TestHUDText = FText::FromString(TestHUDString);
// int64
FCString::Atoi64(*TestHUDString);
Print to Canvas
FCanvas::DrawText()
Canvas->DrawText(BigFont, TestHUDString, 110.0f,110.0f);
Print to Viewport
// UEngine::AddOnScreenDebugMessage(int Key, float TimeToDisplay, FColor DisplayColor, FString DebugMessage)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TestHUDString);
Print to Output Log
UE_LOG(CategoryName, Verbosity, Format, ...);
全局可用的LogCategory可以在如下文件中找到:
UE_4.18\Engine\Source\Runtime\Core\Public\CoreGlobals.h
UE_4.18\Engine\Source\Runtime\Core\Private\Misc\CoreGlobals.cpp
其中定义的全局LogCategory为:
LogHAL
LogMac
LogLinux
LogIOS
LogAndroid
LogPS4
LogXboxOne
LogWindows
LogSwitch
LogSerialization
LogUnrealMath
LogUnrealMatrix
LogContentComparisonCommandlet
LogNetPackageMap
LogNetSerialization
LogMemory
LogProfilingDebugging
LogCore
LogOutputDevice
LogSHA
LogStats
LogStreaming
LogInit
LogExit
LogExec
LogScript
LogLocalization
LogLongPackageNames
LogProcess
LogLoad
LogTemp
如果要声明并定义全局LogCategory,可以使用DECLARE_LOG_CATEGORY_EXTERN来声明,使用DEFINE_LOG_CATEGORY定义。
如果只声明并定义单个文件使用的LogCategory,只需在源码中使用DEFINE_LOG_CATEGORY_STATIC来定义。
DECLARE_LOG_CATEGORY_EXTERN
DEFINE_LOG_CATEGORY
DEFINE_LOG_CATEGORY_STATIC
这三个宏的定义可以在如下文件中找到:
UE_4.18\Engine\Source\Runtime\Core\Public\Logging\LogMacros.h
LogVerbosity is the verbosity level
enum ELogVerbosity {
Fatal, Error, Warning, Display, Log, Verbose, VeryVerbose
}
Log an FString,Int,Float
UE_LOG(YourLog,Warning,TEXT("%s has health %d, which is %f percent of total health"),
*MyCharacter->GetName(), MyCharacter->Health, MyCharacter->HealthPercent);
Log an FVector
UE_LOG(YourLog,Warning,TEXT("MyCharacter's Location is %s"), *MyCharacter->GetActorLocation().ToString());