关于 Puerts 的性能问题
Puerts 在 UE 开发中提供了一定的便利性,可以用代码的方式写蓝图,但是官方是不推荐这么做的
原话如下
那么这个性能问题究竟有多大呢
这里先用 C++ 写一个测试代码
#include "TestWidget.h" #include "Blueprint/WidgetBlueprintLibrary.h" #include "Blueprint/WidgetLayoutLibrary.h" int32 UTestWidget::NativePaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const { FPaintContext Context(AllottedGeometry, MyCullingRect, OutDrawElements, LayerId, InWidgetStyle, bParentEnabled); FVector2D ViewportSize = UWidgetLayoutLibrary::GetViewportSize(const_cast<UTestWidget*>(this)); FVector2D LeftTopPoint = FVector2D(ViewportSize.X * 0.2, ViewportSize.Y * 0.2); FVector2D RightTopPoint = FVector2D(ViewportSize.X * 0.8, ViewportSize.Y * 0.2); FVector2D LeftBottomPoint = FVector2D(ViewportSize.X * 0.2, ViewportSize.Y * 0.8); FVector2D RightBottomPoint = FVector2D(ViewportSize.X * 0.8, ViewportSize.Y * 0.8); UWidgetBlueprintLibrary::DrawLine(Context, LeftTopPoint, RightTopPoint); UWidgetBlueprintLibrary::DrawLine(Context, RightTopPoint, RightBottomPoint); UWidgetBlueprintLibrary::DrawLine(Context, RightBottomPoint, LeftBottomPoint); UWidgetBlueprintLibrary::DrawLine(Context, LeftBottomPoint, LeftTopPoint); return Super::NativePaint(Args, AllottedGeometry, MyCullingRect, OutDrawElements, LayerId, InWidgetStyle, bParentEnabled); }
然后用 Unreal Insight 看一下性能数据
只看 Paint 的数据,作为对照组
然后在 TypeScript 实现一样的代码
import { $Ref } from "puerts" import * as UE from "ue" class TS_TestWidget extends UE.PuertsWidget { OnPaint(Context: $Ref<UE.PaintContext>): void { let ViewportSize = UE.WidgetLayoutLibrary.GetViewportSize(this); let LeftTopPoint = new UE.Vector2D(ViewportSize.X * 0.2, ViewportSize.Y * 0.2); let RightTopPoint = new UE.Vector2D(ViewportSize.X * 0.8, ViewportSize.Y * 0.2); let LeftBottomPoint = new UE.Vector2D(ViewportSize.X * 0.2, ViewportSize.Y * 0.8); let RightBottomPoint = new UE.Vector2D(ViewportSize.X * 0.8, ViewportSize.Y * 0.8); UE.WidgetBlueprintLibrary.DrawLine(Context, LeftTopPoint, RightTopPoint); UE.WidgetBlueprintLibrary.DrawLine(Context, RightTopPoint, RightBottomPoint); UE.WidgetBlueprintLibrary.DrawLine(Context, RightBottomPoint, LeftBottomPoint); UE.WidgetBlueprintLibrary.DrawLine(Context, LeftBottomPoint, LeftTopPoint); } } export default TS_TestWidget
跟 C++ 对比,在最慢的时候可以可以达到几十倍的差距,假如有几十个这样的 Widget 同时运行(例如血条),那么肯定是会出现游戏顿卡或者游戏帧数低的情况
所以在 Puerts 中尽量不写每帧都会运行的逻辑,避免因为跨语言调用而造成的性能问题