参数均按顺序给出
1. GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT(" Hello "));
函数申明:
/**
* This function will add a debug message to the onscreen message list.
* It will be displayed for FrameCount frames.
* @param Key A unique key to prevent the same message from being added multiple times.
* @param TimeToDisplay How long to display the message, in seconds.
* @param DisplayColor The color to display the text in.
* @param DebugMessage The message to display.
*/
void AddOnScreenDebugMessage(int32 Key, float TimeToDisplay, FColor DisplayColor, const FString& DebugMessage, bool bNewerOnTop = true, const FVector2D& TextScale = FVector2D::UnitVector);
此外,两个默认参数
bNewerOnTop(true):是否顶层显示,
TextScale(FVector2D::UnitVector):字体比例。
此外,RemoveOnScreenDebugMessage(Key) 可以根据具体的Key去除正在显示的消息。
此外,可以使用宏定义简化操作
#define PRINT(String) {if (GEngine){GEngine->AddOnScreenDebugMessage(-1,10.0f,FColor::Red,*(String));}}
带参数的写法:FString::Printf(TEXT("%d, %d, %f"), A, B, C)
GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::White, FString::Printf(TEXT("-- TestFuncThreeParams,%d, %d,%f"), bVal, iVal, fVal));
2. UKismetSystemLibrary::PrintString(this,TEXT(" Hello "));
函数申明:
/**
* Prints a string to the log, and optionally, to the screen
* If Print To Log is true, it will be visible in the Output Log window. Otherwise it will be logged only as 'Verbose', so it generally won't show up.
*
* @param InString The string to log out
* @param bPrintToScreen Whether or not to print the output to the screen
* @param bPrintToLog Whether or not to print the output to the log
* @param bPrintToConsole Whether or not to print the output to the console
* @param TextColor Whether or not to print the output to the console
* @param Duration The display duration (if Print to Screen is True). Using negative number will result in loading the duration time from the config.
*/
UFUNCTION(BlueprintCallable, meta=(WorldContext="WorldContextObject", CallableWithoutWorldContext, Keywords = "log print", AdvancedDisplay = "2", DevelopmentOnly), Category="Utilities|String")
static void PrintString(const UObject* WorldContextObject, const FString& InString = FString(TEXT("Hello")), bool bPrintToScreen = true, bool bPrintToLog = true, FLinearColor TextColor = FLinearColor(0.0, 0.66, 1.0), float Duration = 2.f);
原注释不祥,给中文注释
const UObject* WorldContextObject:用于得到当前世界的上下文
const FString& InString:文本内容
bool bPrintToScreen:是否打印到屏幕
bool bPrintToLog:是否打印到Log
FLinearColor TextColor:文本颜色
float Duration:显示持续时间
此外,注意 #include "Kismet/KismetSystemLibrary.h"
此外,还有 PrintText() 函数用于打印 FText 类型的变量。
此外,可以看出,源代码的命名都十分规范,其含义一目了然。
3. UE_LOG(LogTemp, Log, TEXT(" Hello "));
宏定义 :
/**
* A macro that outputs a formatted message to log if a given logging category is active at a given verbosity level
* @param CategoryName name of the logging category
* @param Verbosity verbosity level to test against
* @param Format format text
**/
#define UE_LOG(CategoryName, Verbosity, Format, ...) \
{ \
CA_CONSTANT_IF((ELogVerbosity::Verbosity & ELogVerbosity::VerbosityMask) <= ELogVerbosity::COMPILED_IN_MINIMUM_VERBOSITY && (ELogVerbosity::Warning & ELogVerbosity::VerbosityMask) <= FLogCategory##CategoryName::CompileTimeVerbosity) \
{ \
UE_INTERNAL_LOG_IMPL(CategoryName, Verbosity, Format, ##__VA_ARGS__); \
} \
}
打印消息到Output Log ,在编辑器的 Windows/Log/OutputLog 或者Rider的 Unreal模板 可见,内容保存在 \项目文件夹\Saved\Logs 。
4. UE_LOG(LogDiy, Error, TEXT(" Hello "));
宏定义:
/**
* @param CategoryName 决定Log的名称
* @param DefaultVerbosity 决定Log的颜色
* @param CompileTimeVerbosity 决定Log的最大级别
**/
#define DECLARE_LOG_CATEGORY_EXTERN(CategoryName, DefaultVerbosity, CompileTimeVerbosity) \
extern struct FLogCategory##CategoryName : public FLogCategory<ELogVerbosity::DefaultVerbosity, ELogVerbosity::CompileTimeVerbosity> \
{ \
FORCEINLINE FLogCategory##CategoryName() : FLogCategory(TEXT(#CategoryName)) {} \
} CategoryName;
/**
* @param **CategoryName** 定义的Log名称
**/
#define DEFINE_LOG_CATEGORY(CategoryName) FLogCategory##CategoryName CategoryName;
在.h文件添加 DECLARE_LOG_CATEGORY_EXTERN(CategoryName, DefaultVerbosity, CompileTimeVerbosity);
在.cpp文件添加 DEFINE_LOG_CATEGORY(CategoryName);
在.cpp文件使用 UE_LOG(CategoryName, Log, TEXT(“Hello”));
代码测试(含多参数)
TestLog.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestLog.generated.h"
/**
* @param CategoryName 决定Log的名称
* @param DefaultVerbosity 决定Log的颜色
* @param CompileTimeVerbosity 决定Log的最大级别
* 在.h文件使用DECLARE_LOG_CATEGORY_EXTERN(CategoryName, DefaultVerbosity, CompileTimeVerbosity);
*
* @param CategoryName 定义的Log名称
* 在.cpp文件使用DEFINE_LOG_CATEGORY(CategoryName);
**/
DECLARE_LOG_CATEGORY_EXTERN(LogForInit, Log, All);
/**
* @brief Log测试
*/
UCLASS()
class MICROWORLD_API ATestLog : public AActor{
GENERATED_BODY()
protected:
/**
* @brief 打印所调用的函数
* @param Condition 打印的方式
*/
UFUNCTION(BlueprintCallable)
bool SwitchLog(int32 Condition);
};
TestLog.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "TestLog.h"
#include "Kismet/KismetSystemLibrary.h"
DEFINE_LOG_CATEGORY(LogForInit);
bool ATestLog::SwitchLog(int32 Condition){
bool bResult = true;
switch (Condition){
case 1: {
//使用AddOnScreenDebugMessage,打印到屏幕
if (GEngine != nullptr){
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red,
TEXT("Log by GEngine.AddOnScreenDebugMessage"));
}
break;
}
case 2: {
//使用PrintString,打印到屏幕
UKismetSystemLibrary::PrintString(this,
TEXT("Log by UKismetSystemLibrary.PrintString"));
break;
}
case 3: {
//使用Log
UE_LOG(LogTemp, Log, TEXT("Log by UE_Log in Different Level"));
break;
}
case 4: {
//使用自定义Log
UE_LOG(LogForInit, Error, TEXT("Log by UE_Log LogForInit"));
break;
}
case 5: {
//带参数的打印
//与c语言的Printf函数类似,格式符一致
//主要是对TEXT字符串做处理,若不是基础类型或者FString类型,先转换为FString类型在打印
//注意浮点数默认打印6位小数
const bool ParamBool = true;
const int32 ParamInt = 1;
const float ParamFloat = 1233.1415f;
const double ParamDouble = 3.1415926;
//更为简单的字符串插值
//TArray<FStringFormatArg> Args;
//Args.Add(FStringFormatArg(FString::SanitizeFloat(ParamDouble,10)));
//const FString Message =FString::Format(TEXT("{0}"),Args);
const FString Message = FString::Printf(TEXT("Log by Format Parameter:\
\nParamBool:%s,\nParamInt:%d,\nParamDouble:%-20.3f,\nParamDouble:%.5lf")
, (ParamBool ? TEXT("true") : TEXT("false")), ParamInt, ParamFloat,
ParamDouble);
if (GEngine != nullptr){
GEngine->AddOnScreenDebugMessage(-1, 20.0f, FColor::Red,
*Message);
}
}
default: {
bResult = false;
break;
}
}
return bResult;
}
Log级别
enum Type : uint8
{
/** Not used */
NoLogging = 0,
/** Always prints a fatal error to console (and log file) and crashes (even if logging is disabled) */
Fatal,
/**
* Prints an error to console (and log file).
* Commandlets and the editor collect and report errors. Error messages result in commandlet failure.
*/
Error,
/**
* Prints a warning to console (and log file).
* Commandlets and the editor collect and report warnings. Warnings can be treated as an error.
*/
Warning,
/** Prints a message to console (and log file) */
Display,
/** Prints a message to a log file (does not print to console) */
Log,
/**
* Prints a verbose message to a log file (if Verbose logging is enabled for the given category,
* usually used for detailed logging)
*/
Verbose,
/**
* Prints a verbose message to a log file (if VeryVerbose logging is enabled,
* usually used for detailed logging that would otherwise spam output)
*/
VeryVerbose,
// Log masks and special Enum values
All = VeryVerbose,
NumVerbosity,
VerbosityMask = 0xf,
SetColor = 0x40, // not actually a verbosity, used to set the color of an output device
BreakOnLog = 0x80
};
网友评论