当前位置:首页 > 动态 > UE4开发入门   > UE4开发日常记事本By-SevenLeos
内容详情
UE4开发日常记事本By-SevenLeos
来源:      发布时间:2018-05-31 17:19      浏览:5886     字体:    

FMessageDialog对话框的使用

在CPP文件添加头文件

#include "Runtime/Core/Public/Misc/MessageDialog.h"

在函数开头和结尾分别添加

#define LOCTEXT_NAMESPACE "FMyEEGPluginModule"

#undef LOCTEXT_NAMESPACE

在void里面添加

FMessageDialog::Open(EAppMsgType::Ok, LOCTEXT("ThirdPartyLibraryError", "Failed to load example third party library"));

总结如下:

该对话框应该属于UE4的对话框,不属于游戏内部的对话框,所以这个对话框只适用于一些游戏外的操作提示。


#define LOCTEXT_NAMESPACE "FMyEEGPluginModule"

void AStockHouseGameMode::BeginPlay()

    FMessageDialog::Open(EAppMsgType::Ok, LOCTEXT("ThirdPartyLibraryError", "Failed to load example third party library"));
}

#undef LOCTEXT_NAMESPACE

效果图:


游戏里面的对话框(自定义) GMessageDialog

(思路:利用widget,先创建一个用于Confirm的widget,然后在其他widget需要confirm提示的时候,在按钮的绑定事件对应的函数添加 GMessageDialog 函数做判断,如果confirm,则返回true;如果cancle,则返回false。)

暂时还在开发,有时间再整理代码


Debug- 屏幕打印(非VR模式)

GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Cyan, FString("这类就是你要输出的内容,也可以换成变量"));

可以写成一个方法

void GMessage::Print_T(FString InStr)  
{  
    if (GEngine)  
    {  
        GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, InStr);  
    }  
}  


UE4的调试输出

//*1 调试输出*//
/*case a、快速使用 不设置log类别 默认为LogTemp*/
UE_LOG(LogTemp,Log,TEXT("Your message"));
UE_Log(LogTemp,Warning,TEXT("You Number type of float value is %f"),YourFloatTypeValue);
UE_Log(LogTemp,Error,TEXT("Your Number type of FString value is %s"),YourFStringTypeValue);
//Log:输出日志字体颜色为灰色 Warning:输出字体颜色为黄色 Error:输出字体颜色为红色
/*case b、设置自定义Log类别*/
//在YourCode.h文件中声明自定义Log类别@parm YourLog
DECLARE_LOG_CATEGORY_EXTERN(YourLog, Log, All);
//在YourCode.cpp文件中定义
DEFINE_LOG_CATEGORY(YourLog);
UE_LOG(YourLog, Log, TEXT("This is a message to yourself during runtime!"));
/*case c、触发严重中断 程序执行到此时会触发程序中断*/
UE_LOG(YourLog, Fatal, TEXT("This fringe case was reached! Debug this!"));
//log输出在output log面板中显示
/*case d、 向屏幕打印消息 显示在屏幕上*/
if(GEngine)
{
    GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("This is an on screen message!"));
    GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Some variable values: x: %f, y: %f"), x, y));
}
//我们可以在.cpp文件下进行如下宏定义以快速使用该方法
#define print(text) if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::White,text)


在场景中查找对象

#include "EngineUtils.h"
/*case a、Actor 迭代*/
for (TActorIteratorActorItr(GetWorld()); ActorItr; ++ActorItr)
{
    AStaticMeshActor *Mesh = *ActorItr;
}
/*case b、Object迭代*/
for (TObjectIteratorItr; Itr; ++Itr)
{
    AActor *Component = *Itr;
}
//Object 迭代可以迭代的内容包括Actor可迭代的内容



射线的使用

//GetHitResultAtScreenPosition函数为引擎框架下APlayerController的方法  
//顾名思义是以屏幕为起点向鼠标所在坐标发出射线,进行碰撞检测
//可查看APlayerController下此函数的定义  
FVector2D cursorPosition(0,0);  
this->GetMousePosition(cursorPosition.X, cursorPosition.Y);  
FHitResult hitInfo(ForceInit);  
FCollisionQueryParams collisionParms(false);  
this->GetHitResultAtScreenPosition(cursorPosition,ECC_PhysicsBody,collisionParms,hitInfo);
//在此处理hitInfo;
/*GetWorld()->LineTraceSingleByObjectType等一系列函数用于处理更多的射线类功能,但APlayerController下已经封装好很多常用的方法。*/


场景捕获组件的使用


/*此处实现将SceneCapture2D看到的视角复制到Texture中 */ 
/*方式一、 */
/*sceneCapture为SceneCapture2D组件的引用 */ 
/*renderTexture为上图所示textureTarget的引用*/ 
UTexture2D *Texture = UTexture2D::CreateTransient(TextureRenderTarget->SizeX,TextureRenderTarget->SizeY, PF_B8G8R8A8);  
sceneCapture->GetCaptureComponent2D()->UpdateContent();  
TArraySurfData;
FRenderTarget *RenderTarget = renderTexture->GameThread_GetRenderTargetResource();
RenderTarget->ReadPixels(SurfData);
void* TextureData = Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
const int32 TextureDataSize = SurfData.Num() * 4;
FMemory::Memcpy(TextureData, SurfData.GetData(), TextureDataSize);
Texture->PlatformData->Mips[0].BulkData.Unlock();
Texture->UpdateResource();



/*方式二、*/
/*使用ConstructTexture2D函数,该函数每次返回的是同一块内存地址*/
sceneCapture->GetCaptureComponent2D()->UpdateContent();  
Texture = renderTexture->ConstructTexture2D(this,"AlphaTex",EObjectFlags::RF_NoFlags,CTF_DeferCompression);  
Texture->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;  
Texture->UpdateResource();




UE4字符类型到基本数据类型的转换

/** Covert a string buffer to intrinsic types */
inline void FromString(int8& OutValue,      const TCHAR* Buffer)    {   OutValue = FCString::Atoi(Buffer);      }
inline void FromString(int16& OutValue,     const TCHAR* Buffer)    {   OutValue = FCString::Atoi(Buffer);      }
inline void FromString(int32& OutValue,     const TCHAR* Buffer)    {   OutValue = FCString::Atoi(Buffer);      }
inline void FromString(int64& OutValue,     const TCHAR* Buffer)    {   OutValue = FCString::Atoi64(Buffer);    }
inline void FromString(uint8& OutValue,     const TCHAR* Buffer)    {   OutValue = FCString::Atoi(Buffer);      }
inline void FromString(uint16& OutValue,    const TCHAR* Buffer)    {   OutValue = FCString::Atoi(Buffer);      }
inline void FromString(uint32& OutValue,    const TCHAR* Buffer)    {   OutValue = FCString::Atoi64(Buffer);    }   //64 because this unsigned and so Atoi might overflow
inline void FromString(uint64& OutValue,    const TCHAR* Buffer)    {   OutValue = FCString::Strtoui64(Buffer, nullptr, 0); }
inline void FromString(float& OutValue,     const TCHAR* Buffer)    {   OutValue = FCString::Atof(Buffer);      }
inline void FromString(double& OutValue,    const TCHAR* Buffer)    {   OutValue = FCString::Atod(Buffer);      }
inline void FromString(bool& OutValue,      const TCHAR* Buffer)    {   OutValue = FCString::ToBool(Buffer);    }


UE4有很多个Module, GamePlay部分也是一个或多个Module, 每个Editor也是一个Module
搜索了一下, Module总数170+, 总共分三类: Runtime, Editor, Developer
Runtime这边比较重要的是Core, UObject, Engine


Core
    数据类型
    数据库
    容器
    IO/Log


UObject: 所有对象的基类. UE中还有个约定是Object代表不是Actor的对象
    反射
    序列化
    文件包


Engine
    Actor: 由Component所组成的集合, 并且可以不断地Tick
    Component: 组成Actor的功能组件, 如USceneComponent管理transform, UPrimitiveComponent代表几何信息
    Level: 关卡
    World: 游戏世界, 管理Level, Actor, Controller等 


Gameplay
    GameMode: 定义游戏规则. 只存在于服务器端
    GameState: 管理游戏状态, 
    Pawn: 可以被玩家或者AI控制的Actor
    Character: 人形的Pawn. 默认带有胶囊体碰撞和移动组件
    Controller: 控制器, 分PlayerController和AIController
    HUD: 2D信息, UI之类
    Camera: 玩家的视角. 值得注意的是PostEffect是跟相机关联的


途联微信号