RPG29:制作ui基础
1.创建该类的子类
然后重启项目,再创建子类的子类
再创建一个UI用于敌人
创建一个接口
2.然后打开Warriorplayer和EnemyCharacter实现ui组件
//UIUPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Combat", meta = (AllowPrivateAccess = "true"))UPlayerUIComponent* PlayerUIComponent;
PlayerUIComponent = CreateDefaultSubobject<UPlayerUIComponent>(TEXT("PlayerUIComponent"));
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "UI")UEnemyUIComponent* EnemyUIComponent;
EnemyUIComponent = CreateDefaultSubobject<UEnemyUIComponent>("EnemyUIComponent");
3.打开PawnUIInterface
public:virtual UPawnUIComponent* GetPawnUIComponent() const = 0;
打开CHaracterBase继承此接口
//Begin IUIPawnInterface Interfacevirtual UPawnUIComponent* GetPawnUIComponent() const override;//End IUIPawnInterface Interface
UPawnUIComponent* ACharacterBase::GetPawnUIComponent() const
{return nullptr;
}
然后打开WarriorPlayer做同样的事
//Begin IUIPawnInterface Interfacevirtual UPawnUIComponent* GetPawnUIComponent() const override;//End IUIPawnInterface Interface
UPawnUIComponent* AWarriorPlayer::GetPawnUIComponent() const
{return PlayerUIComponent;
}
打开EnemyBase做同样的事
//Begin IUIPawnInterface Interfacevirtual UPawnUIComponent* GetPawnUIComponent() const override;//End IUIPawnInterface Interface
UPawnUIComponent* AEnemyBase::GetPawnUIComponent() const
{return EnemyUIComponent;
}
4.接下来对数值进行广播
打开PawnUIComponent.h,创建好委托
//当值的百分比发生变化时
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPercentChangedDelegate, float, NewPercent);UPROPERTY(BlueprintAssignable)FOnPercentChangedDelegate OnCurrentHealthChanged;
然后进入PlayerUIComponent.h,为Rage单独创建一个委托(因为这个东西是只有玩家才有,怪物没有)
UPROPERTY(BlueprintAssignable)FOnPercentChangedDelegate OnCurrentRageChanged;
然后进入PawnUiInterface.h
virtual UPlayerUIComponent* GetPlayerUIComponent() const;
UPlayerUIComponent* IPawnUIInterface::GetPlayerUIComponent() const
{return nullptr;
}
进入WarriorPlayer覆写此函数
virtual UPlayerUIComponent* GetPlayerUIComponent() const override;
UPlayerUIComponent* AWarriorPlayer::GetPlayerUIComponent() const
{return PlayerUIComponent;
}
接着,进入XMBAttributeSet,先创建一个用于存储当前Pawn对象的变量
private://用于存储当前的PawnUIInterfaceTWeakInterfacePtr<IPawnUIInterface> CachedPawnUIInterface;
然后需要在数值发生变化时,先获取到当前对象的PawnUIComponent
void UXMBAttributeSet::PostGameplayEffectExecute(const struct FGameplayEffectModCallbackData& Data)
{
//获取到目标的UIComponent,则可以用广播if (!CachedPawnUIInterface.IsValid()){CachedPawnUIInterface = TWeakInterfacePtr<IPawnUIInterface>(Data.Target.GetAvatarActor());}checkf(CachedPawnUIInterface.IsValid(), TEXT("%s didn`t implement IPawnUIInterface"),*Data.Target.GetAvatarActor()->GetActorNameOrLabel());UPawnUIComponent* PawnUIComponent = CachedPawnUIInterface->GetPawnUIComponent();checkf(PawnUIComponent,TEXT("Couldn`t extrac a PawnUiComponent from %s"),*Data.Target.GetAvatarActor()->GetActorNameOrLabel());
}
然后在生命发生变化时,
//Data.EvaluatedData.Attribute是当前被修改的属性if (Data.EvaluatedData.Attribute == GetCurrentHealthAttribute())//找到正在修改的属性{const float NewCurrentHealth = FMath::Clamp(GetCurrentHealth(),0.f, GetMaxHealth());SetCurrentHealth(NewCurrentHealth);//广播变化后的生命PawnUIComponent->OnCurrentHealthChanged.Broadcast(GetCurrentHealth()/GetMaxHealth());}
当体力发生变化时,需要现货区到玩家的UIComponent,
if (UPlayerUIComponent* PlayerUIComponent = CachedPawnUIInterface->GetPlayerUIComponent()){PlayerUIComponent->OnCurrentRageChanged.Broadcast(GetCurrentRage()/GetMaxRage());}
最后在设置当前生命后再广播一次当前生命
SetCurrentHealth(NewCurrentHealth);const FString DebugString = FString::Printf(TEXT("OldHealth : %f, damage Done :%f, NewCurrentHealth:%f"),OldHealth,DamageDone,NewCurrentHealth);//将当前生命广播出去(不管变没变化都要广播)PawnUIComponent->OnCurrentHealthChanged.Broadcast(GetCurrentHealth()/GetMaxHealth());
5.启动项目,创建一个UserWidget的C++类
然后创建两个函数
protected:virtual void NativeOnInitialized() override;UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "On Owning Player UI Component Initialized"))void BP_OnOwningPlayerUIComponentInitialized(UPlayerUIComponent* OwningPlayerUIComponent);
void UWarriorWidgetBase::NativeOnInitialized()
{Super::NativeOnInitialized();if (IPawnUIInterface* PawnUIInterface = Cast<IPawnUIInterface>(GetOwningPlayerPawn())){if (UPlayerUIComponent* PlayerUIComponent = PawnUIInterface->GetPlayerUIComponent()){BP_OnOwningPlayerUIComponentInitialized(PlayerUIComponent);}}}
6.这一步要进行敌人widget的初始化,首先进入pawnuiinterface为敌人写一个获取uicompnent的函数
virtual UEnemyUIComponent* GetEnemyUIComponent() const;
UEnemyUIComponent* IPawnUIInterface::GetEnemyUIComponent() const
{return nullptr;
}
然后进入EnemyBase内进行覆写
virtual UEnemyUIComponent* GetEnemyUIComponent() const;
UEnemyUIComponent* AEnemyBase::GetEnemyUIComponent() const
{return EnemyUIComponent;
}
再回到WarriorWidgetBase创建两个函数
public://为敌人创建的UFUNCTION(BlueprintCallable)void InitEnemyCreatedWidget(AActor* OwningEnemyActor);
protected:
UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "On Owning Enemy UI Component Initialized"))void BP_OnOwningEnemyUIComponentInitialized(UEnemyUIComponent* OwningEnemyUIComponent);
void UWarriorWidgetBase::InitEnemyCreatedWidget(AActor* OwningEnemyActor)
{if (IPawnUIInterface* PawnUIInterface = Cast<IPawnUIInterface>(OwningEnemyActor)){//获取Enemy的UIComponentUEnemyUIComponent* EnemyUIComponent = PawnUIInterface->GetEnemyUIComponent();checkf(EnemyUIComponent, TEXT("Failed to extrac an EnemyUIComponent from %s"),*OwningEnemyActor->GetActorNameOrLabel());BP_OnOwningEnemyUIComponentInitialized(EnemyUIComponent);}
}