准星
创建材质
创建材质实例
准星控件
#pragma once
#include "CoreMinimal.h"
#include "Widgets/SCompoundWidget.h"
class SLAICOURSE_API SSlAiPointerWidget : public SCompoundWidget
{
public:
SLATE_BEGIN_ARGS(SSlAiPointerWidget)
{}
SLATE_END_ARGS()
void Construct(const FArguments& InArgs);
virtual void Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime) override;
void UpdatePointer(bool IsAim, float Range);
private:
FOptionalSize GetBoxWidth() const;
FOptionalSize GetBoxHeight() const;
private:
TSharedPtr<class SBox> RootBox;
float CurrentSize;
class UMaterialInstanceDynamic* PointerMaterial;
bool IsAimed;
};
#include "SSlAiPointerWidget.h"
#include "SlateOptMacros.h"
#include"SlAiStyle.h"
#include"SlAiGameWidgetStyle.h"
#include"SBox.h"
#include"SImage.h"
#include"Materials/MaterialInstanceDynamic.h"
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SSlAiPointerWidget::Construct(const FArguments& InArgs)
{
CurrentSize = 20.f;
IsAimed = false;
ChildSlot
[
SAssignNew(RootBox,SBox)
.WidthOverride(TAttribute<FOptionalSize>(this,&SSlAiPointerWidget::GetBoxWidth))
.HeightOverride(TAttribute<FOptionalSize>(this,&SSlAiPointerWidget::GetBoxHeight))
[
SNew(SImage)
.Image(&SlAiStyle::GetGameStyle()->PointerBrush)
]
];
static ConstructorHelpers::FObjectFinder<UMaterialInstance> StaticPointerMaterialInstance(TEXT("MaterialInstanceConstant'/Game/Material/PointerMatInst.PointerMatInst'"));
PointerMaterial = (UMaterialInstanceDynamic*)StaticPointerMaterialInstance.Object;
}
void SSlAiPointerWidget::Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime)
{
CurrentSize = IsAimed ? FMath::FInterpTo(CurrentSize, 130.f, InDeltaTime, 10.f) : FMath::FInterpTo(CurrentSize, 20.f, InDeltaTime, 10.f);
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION
FOptionalSize SSlAiPointerWidget::GetBoxWidth() const
{
return FOptionalSize(CurrentSize);
}
FOptionalSize SSlAiPointerWidget::GetBoxHeight() const
{
return FOptionalSize(CurrentSize);
}
void SSlAiPointerWidget::UpdatePointer(bool IsAim, float Range)
{
IsAimed = IsAim;
PointerMaterial->SetScalarParameterValue(FName("Range"), Range);
}
将准星添加到GameHUD
public:
TSharedPtr<class SSlAiPointerWidget> PointerWidget;
#include"SSlAiPointerWidget.h"
ChildSlot
[
SNew(SDPIScaler)
.DPIScale(UIScaler)
[
SNew(SOverlay)
+SOverlay::Slot()
.HAlign(HAlign_Center)
.VAlign(VAlign_Bottom)
[
SAssignNew(ShortcutWidget,SSlAiShortcutWidget)
]
+SOverlay::Slot()
.HAlign(HAlign_Center)
.VAlign(VAlign_Top)
[
SAssignNew(RayInfoWidget,SSlAiRayInfoWidget)
]
+SOverlay::Slot()
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)
[
SAssignNew(PointerWidget,SSlAiPointerWidget)
]
]
];
修改准星委托
控制类声明委托
DECLARE_DELEGATE_TwoParams(FUpdatePointer, bool, float)
public:
FUpdatePointer UpdatePointer;
GameHUD绑定委托
#include"SSlAiPointerWidget.h"
void ASlAiGameHUD::BeginPlay()
{
...
GM->SPController->UpdatePointer.BindRaw(GameHUDWidget->PointerWidget.Get(), &SSlAiPointerWidget::UpdatePointer);
}
控制类中使用
void ASlAiPlayerController::Tick(float DeltaSeconds)
{
...
static float TestPinter = 1.f;
TestPinter = FMath::FInterpTo(TestPinter, 0, DeltaSeconds, 1.f);
UpdatePointer.ExecuteIfBound(true, FMath::Clamp(TestPinter, 0.f, 1.f));
}
运行
|