Unreal Engine 4:编译时出现中文乱码问题

    在中文windows下,使用中文版本的Visual Studio基于Unreal Engine 4开发C++代码,在Unreal Engine 4中编译时,在Output Log中总会出现乱码:

LogInit: Warning: Incompatible or missing module: UE4Editor-CrazyTanx.dll
Running C:/Epic Games/UE_4.18/Engine/Binaries/DotNET/UnrealBuildTool.exe CrazyTanx Development Win64 -project="D:/unreal_projects/CrazyTanx/CrazyTanx.uproject" -editorrecompile -progress -NoHotReloadFromIDE
Performing full C++ include scan (no include cache file)
@progress push 5%
@progress pop
Performing 3 actions (4 in parallel)
Conf.cpp
D:\unreal_projects\CrazyTanx\Source\CrazyTanx\Conf.cpp(3): error C2600: ??Conf::Conf??: ???ܶ????????????ɵ???????Ա????(??????????????????)
ERROR: UBT ERROR: Failed to produce item: D:\unreal_projects\CrazyTanx\Binaries\Win64\UE4Editor-CrazyTanx.pdb
Total build time: 4.18 seconds (Local executor: 0.00 seconds)

       无论怎么更改Unreal Engine 4中Edit -> Editor Preferences -> Region & Language 中的 Editor Language还是Editor Locale,都无法解决这个问题。
经过一段时间的研究,终于找到了解决此问题的方法,并了解了此问题的产生原因。

解决方法一
如果还是想正常显示中文,则在windows的“区域设置”中选中“使用Unicode UTF-8提供全球语言支持”,然后重启电脑问题就解决了。此项设置是windows的新功能,没有此项的请升级windows版本。

                                                                                                               

解决方法二
如果可以接受纯英文显示,则可以给Visual Studio下载安装Engish语言包,然后在Visual Studio中有“工具 -> 选项 -> 环境 -> 区域设置 -> 语言”中选择English,同时在windows的“区域设置”中选择“英语”,然后重启电脑就行了。

下面来分析一下中文乱码的产生原因。

经分析Output Log,可以发现产生乱码的原因之一是调用了UnrealBuildTool.exe后产生的。经查找代码,UnrealBuildTool.exe是在Engine\Source\Developer\DesktopPlatform\Private\Windows\DesktopPlatformWindows.cpp的如下方法中被调用的:

bool FDesktopPlatformWindows::RunUnrealBuildTool(const FText& Description, const FString& RootDir, const FString& Arguments, FFeedbackContext* Warn)

在此方法中调用了

bool FFeedbackContextMarkup::PipeProcessOutput(const FText& Description, const FString& URL, const FString& Params, FFeedbackContext* Warn, int32* OutExitCode)

此方法使用了

FPlatformProcess::ReadPipe(PipeRead);

来读取返回的结果,其代码如下:

FString FWindowsPlatformProcess::ReadPipe( void* ReadPipe )
{
    FString Output;

    uint32 BytesAvailable = 0;
    if (::PeekNamedPipe(ReadPipe, NULL, 0, NULL, (::DWORD*)&BytesAvailable, NULL) && (BytesAvailable > 0))
    {
        UTF8CHAR* Buffer = new UTF8CHAR[BytesAvailable + 1];
        uint32 BytesRead = 0;
        if (::ReadFile(ReadPipe, Buffer, BytesAvailable, (::DWORD*)&BytesRead, NULL))
        {
            if (BytesRead > 0)
            {
                Buffer[BytesRead] = '\0';
                Output += FUTF8ToTCHAR((const ANSICHAR*)Buffer).Get();    #1
            }
        }
        delete [] Buffer;
    }

    return Output;
}

从#1这行代码中可以看到,存储返回结果的Buffer被当成UTF-8编码的字符串转换成FString,而实际上在默认中文windows中,它应该是GBK编码的。产生乱码的原因就在此处。这也是为什么采用“解决方法一”能够解决问题的原因,因为“解决方法一”可以返回UTF-8编码的结果。


通过上述分析,也自然而然有了第三种解决方法

解决方法三
将FWindowsPlatformProcess中的#1处修改了:

Output += FString(string2wstring(std::string((const ANSICHAR*)Buffer), "Chinese_China.936").c_str());

即先把GBK编码的Buffer转化成std::wstring,然后再转换成FString。当然,这样需要重新编译unreal。

     另外,需要说明一下UnrealBuildTool.exe,它是由C#编写的,代码在Epic Games\UE_4.18\Engine\Source\Programs\UnrealBuildTool下。它会在LocalExecutor.cs文件中的ThreadFunc()方法中,分别调用如下三个命令:

{C:\Microsoft\Visual Studio\2017\Professional\VC\Tools\MSVC\14.13.26128\bin\HostX64\x64\cl.exe -  @"D:\unreal_projects\CrazyTanx\Intermediate\Build\Win64\UE4Editor\Development\CrazyTanx\Conf.cpp.obj.response"}
{C:\Microsoft\Visual Studio\2017\Professional\VC\Tools\MSVC\14.13.26128\bin\HostX64\x64\link.exe - @"D:\unreal_projects\CrazyTanx\Intermediate\Build\Win64\UE4Editor\Development\UE4Editor-CrazyTanx.dll.response"}
{C:\Microsoft\Visual Studio\2017\Professional\VC\Tools\MSVC\14.13.26128\bin\HostX64\x64\lib.exe - @"D:\unreal_projects\CrazyTanx\Intermediate\Build\Win64\UE4Editor\Development\UE4Editor-CrazyTanx.lib.response"}

并将执行结果返回给UnrealBuildTool.exe,再由UnrealBuildTool.exe将结果返回给UE4Editor.exe,UE4Editor.exe会将此结果显示在Output Log窗口,乱码就是在UnrealBuildTool.exe将结果返回给UE4Editor.exe时产生的。

  • 14
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值