文章目錄
- 前言
- 技能的升級
- 修改一下按鍵的輸入
- 判斷是否滿級
- 在ASC中升級技能
- 由角色的輸入調用ASC的升級功能
- 技能圖標的優化
- 技能升級材質,可升級技能圖標的閃爍
- 刷新技能升級后的藍耗和CD,以及藍不夠時技能進入灰色狀態
- 修復傷害數字特效只顯示3位數的問題
前言
重寫技能基類的判斷是否可以釋放技能的函數CanActivateAbility
,因為基本技能和被動的初始等級是1,技能數組里的需要通過學習才能釋放。
virtual bool CanActivateAbility(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayTagContainer* SourceTags = nullptr, const FGameplayTagContainer* TargetTags = nullptr, OUT FGameplayTagContainer* OptionalRelevantTags = nullptr) const override;
獲取技能的等級如果技能等級小于等于0返回false
,不能釋放
bool UCGameplayAbility::CanActivateAbility(const FGameplayAbilitySpecHandle Handle,const FGameplayAbilityActorInfo* ActorInfo, const FGameplayTagContainer* SourceTags,const FGameplayTagContainer* TargetTags, FGameplayTagContainer* OptionalRelevantTags) const
{FGameplayAbilitySpec* AbilitySpec = ActorInfo->AbilitySystemComponent->FindAbilitySpecFromHandle(Handle);if (AbilitySpec && AbilitySpec->Level <= 0){return false;}return Super::CanActivateAbility(Handle, ActorInfo, SourceTags, TargetTags, OptionalRelevantTags);
}
技能的升級
修改一下按鍵的輸入
在CGameplayAbilityTypes
的ECAbilityInputID
添加一些新的輸入
UENUM(BlueprintType)
enum class ECAbilityInputID : uint8
{None UMETA(DisplayName="None"),BasicAttack UMETA(DisplayName="基礎攻擊"),Aim UMETA(DisplayName="瞄準"),AbilityOne UMETA(DisplayName="一技能"),AbilityTwo UMETA(DisplayName="二技能"),AbilityThree UMETA(DisplayName="三技能"),AbilityFour UMETA(DisplayName="四技能"),AbilityFive UMETA(DisplayName="五技能"),AbilitySix UMETA(DisplayName="六技能"),AbilityQ UMETA(DisplayName="Q技能"),AbilityE UMETA(DisplayName="E技能"),AbilityF UMETA(DisplayName="F技能"),AbilityR UMETA(DisplayName="R技能"),Confirm UMETA(DisplayName="確認"),Cancel UMETA(DisplayName="取消")
};
判斷是否滿級
技能的升級之前需要先判斷技能是否滿級,在CAbilitySystemStatics
中添加一個函數判斷是否為滿級
// 判斷技能是否達到最大等級static bool IsAbilityAtMaxLevel(const FGameplayAbilitySpec& Spec, const float PlayLevel);
bool UCAbilitySystemStatics::IsAbilityAtMaxLevel(const FGameplayAbilitySpec& Spec, const float PlayLevel)
{float MaxLevel;// 如果是大招進來了if (Spec.InputID == static_cast<int32>(ECAbilityInputID::AbilityR)){// 6~10 : 1// 11~15 : 2// 16~18 : 3MaxLevel = PlayLevel >= 16 ? 3 :PlayLevel >= 11 ? 2 :PlayLevel >= 6 ? 1 :0;}else{// Q、E、F的小技能/** 1 ~ 2 :1* 3 ~ 4 :2* 5 ~ 6 :3* 7 ~ 8 :4* 9 ~18:5*/MaxLevel = PlayLevel >= 9 ? 5 :PlayLevel >= 7 ? 4 :PlayLevel >= 5 ? 3 :PlayLevel >= 3 ? 2 :1;}// Spec.InputID return Spec.Level >= MaxLevel;
}
在ASC中升級技能
/*** 服務器端處理能力升級請求* 通過指定的ECAbilityInputID參數升級對應能力* 包含可靠的網絡驗證機制* @param InputID - 要升級的能力輸入ID*/UFUNCTION(Server, Reliable, WithValidation)void Server_UpgradeAbilityWithID(ECAbilityInputID InputID);/*** 客戶端能力等級更新同步* 當能力等級發生變化時觸發網絡同步* 通過GameplayAbilitySpecHandle定位具體能力實例* @param Handle - 能力實例句柄* @param NewLevel - 新的能力等級數值*/UFUNCTION(Client, Reliable)void Client_AbilitySpecLevelUpdated(FGameplayAbilitySpecHandle Handle, int NewLevel);
在ASC中實現技能在服務器中升級,并響應客戶端的同步更新
void void UCAbilitySystemComponent::Server_UpgradeAbilityWithID_Implementation(ECAbilityInputID InputID)
{// 獲取可用升級點數bool bFound = false;float UpgradePoint = GetGameplayAttributeValue(UCHeroAttributeSet::GetUpgradePointAttribute(), bFound);// 檢查可用升級點數是否大于0if (!bFound && UpgradePoint <= 0) return;// 獲取玩家等級float CurrentLevel = GetGameplayAttributeValue(UCHeroAttributeSet::GetLevelAttribute(), bFound);if (!bFound) return;// 獲取對應輸入ID的技能FGameplayAbilitySpec* AbilitySpec = FindAbilitySpecFromInputID(static_cast<int32>(InputID));// 檢查是否有該技能以及等級是否滿級if (!AbilitySpec || UCAbilitySystemStatics::IsAbilityAtMaxLevel(*AbilitySpec,CurrentLevel)){return;}UE_LOG(LogTemp, Warning, TEXT("技能升級成功%d"),InputID)// 消耗一個技能點升級技能SetNumericAttributeBase(UCHeroAttributeSet::GetUpgradePointAttribute(), UpgradePoint - 1);AbilitySpec->Level += 1;// 標記 FGameplayAbilitySpec 狀態已改變,通知 GAS 需要將其復制到客戶端// (直接修改AbilitySpec成員后必須調用此函數)MarkAbilitySpecDirty(*AbilitySpec);// 通知客戶端更新技能等級Client_AbilitySpecLevelUpdated(AbilitySpec->Handle, AbilitySpec->Level);
}bool UCAbilitySystemComponent::Server_UpgradeAbilityWithID_Validate(ECAbilityInputID InputID)
{return true;
}void UCAbilitySystemComponent::Client_AbilitySpecLevelUpdated_Implementation(FGameplayAbilitySpecHandle Handle,int NewLevel)
{// 通過句柄查找本地技能實例if (FGameplayAbilitySpec* const Spec = FindAbilitySpecFromHandle(Handle)){// 更新客戶端技能等級Spec->Level = NewLevel;// 廣播變更通知,刷新等客戶端響應AbilitySpecDirtiedCallbacks.Broadcast(*Spec);}
}
由角色的輸入調用ASC的升級功能
到角色CCharacter
中調用技能的升級,
protected:void UpgradeAbilityWithInputID(ECAbilityInputID InputID);
void ACCharacter::UpgradeAbilityWithInputID(ECAbilityInputID InputID)
{if (CAbilitySystemComponent){CAbilitySystemComponent->Server_UpgradeAbilityWithID(InputID);}
}
再到玩家角色中通過輸入來調用該升級函數,添加一個新的輸入用來判斷Ctrl
按鍵是否按下,用Ctrl+技能輸入ID
來進行升級
// 技能升級觸發鍵UPROPERTY(EditDefaultsOnly, Category = "Input")TObjectPtr<UInputAction> LearnAbilityLeaderAction;// 技能升級觸發按下void LearnAbilityLeaderDown(const FInputActionValue& InputActionValue);// 技能升級觸發抬起void LearnAbilityLeaderUp(const FInputActionValue& InputActionValue);// 是否按下技能升級鍵bool bIsLearnAbilityLeaderDown = false;
在這里我只想要我的設定幾個按鍵的技能可以升級
void ACPlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent);if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)){// 綁定跳、看、走EnhancedInputComponent->BindAction(JumpInputAction, ETriggerEvent::Triggered, this, &ACPlayerCharacter::Jump);EnhancedInputComponent->BindAction(LookInputAction, ETriggerEvent::Triggered, this, &ACPlayerCharacter::HandleLookInput);EnhancedInputComponent->BindAction(MoveInputAction, ETriggerEvent::Triggered, this, &ACPlayerCharacter::HandleMoveInput);// 按下EnhancedInputComponent->BindAction(LearnAbilityLeaderAction, ETriggerEvent::Started, this, &ACPlayerCharacter::LearnAbilityLeaderDown);// 抬起EnhancedInputComponent->BindAction(LearnAbilityLeaderAction, ETriggerEvent::Completed, this, &ACPlayerCharacter::LearnAbilityLeaderUp);// 綁定技能輸入for (const TPair<ECAbilityInputID, TObjectPtr<UInputAction>>& InputActionPair : GameplayAbilityInputActions){EnhancedInputComponent->BindAction(InputActionPair.Value, ETriggerEvent::Triggered, this, &ACPlayerCharacter::HandleAbilityInput, InputActionPair.Key);}}
}void ACPlayerCharacter::HandleAbilityInput(const FInputActionValue& InputActionValue, ECAbilityInputID InputID)
{bool bPressed = InputActionValue.Get<bool>();// 技能升級if (bPressed && bIsLearnAbilityLeaderDown){// 只會升級Q、E、F、R技能if (InputID >= ECAbilityInputID::AbilityQ && InputID <= ECAbilityInputID::AbilityR){UpgradeAbilityWithInputID(InputID);}return;}// 按下if (bPressed){GetAbilitySystemComponent()->AbilityLocalInputPressed(static_cast<int32>(InputID));}else{GetAbilitySystemComponent()->AbilityLocalInputReleased(static_cast<int32>(InputID));}// 按下的是普攻鍵if (InputID == ECAbilityInputID::BasicAttack){FGameplayTag BasicAttackTag = bPressed ? TGameplayTags::Ability_BasicAttack_Pressed : TGameplayTags::Ability_BasicAttack_Released;// 1. 本地直接廣播(觸發客戶端即時反饋)// 2. 服務器RPC廣播(確保權威狀態同步)UAbilitySystemBlueprintLibrary::SendGameplayEventToActor(this, BasicAttackTag, FGameplayEventData());Server_SendGameplayEventToSelf(BasicAttackTag, FGameplayEventData());}
}void ACPlayerCharacter::LearnAbilityLeaderDown(const FInputActionValue& InputActionValue)
{bIsLearnAbilityLeaderDown = true;
}void ACPlayerCharacter::LearnAbilityLeaderUp(const FInputActionValue& InputActionValue)
{bIsLearnAbilityLeaderDown = false;
}
創建一個輸入
技能圖標的優化
技能升級材質,可升級技能圖標的閃爍
float2 coord = (uvcoord - float2(0.5, 0.5)) * 2;float distanceToEdge = 1-abs(coord.x) > 1-abs(coord.y) ? 1 - abs(coord.y) : 1-abs(coord.x);float alpha = 0;if(distanceToEdge < shadeThickness)
{alpha = 1 - distanceToEdge / shadeThickness;
}return lerp(float3(0,0,0), shadeColor, alpha);
UTiling
用于設置技能的最大等級(原本的基礎上拓展了一下)
對技能圖標AbilityGauge
添加代碼
// 技能等級材質參數名UPROPERTY(EditDefaultsOnly, Category = "Visual")FName AbilityLevelParamName = "Level";// 能否釋放技能材質參數名UPROPERTY(EditDefaultsOnly, Category = "Visual")FName CanCastAbilityParamName = "CanCast";// 是否有可用升級點材質參數名UPROPERTY(EditDefaultsOnly, Category = "Visual")FName UpgradePointAvailableParamName = "UpgradeAvailable";// 最大升級的材質參數名UPROPERTY(EditDefaultsOnly, Category = "Visual")FName MaxLevelParamName = "UTiling";// 技能等級進度條控件UPROPERTY(meta=(BindWidget))TObjectPtr<UImage> LevelGauge;// 技能所屬的能力組件TObjectPtr<const UAbilitySystemComponent> OwnerAbilitySystemComponent;// 緩存的技能句柄FGameplayAbilitySpecHandle CachedAbilitySpecHandle;// 獲取技能Specconst FGameplayAbilitySpec* GetAbilitySpec();// 技能是否學習bool bIsAbilityLearned = false;// 技能更新回調void AbilitySpecUpdated(const FGameplayAbilitySpec& AbilitySpec);// 刷新技能能否釋放技能狀態void UpdateCanCast();// 技能升級點變化回調void UpgradePointUpdated(const FOnAttributeChangeData& Data);
此處的監聽是在,ASC的客戶端更新處廣播的
void UAbilityGauge::NativeConstruct()
{Super::NativeConstruct();// 隱藏冷卻計時器CooldownCounterText->SetVisibility(ESlateVisibility::Hidden);UAbilitySystemComponent* OwnerASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(GetOwningPlayerPawn());if (OwnerASC){// 監聽技能釋放OwnerASC->AbilityCommittedCallbacks.AddUObject(this, &UAbilityGauge::AbilityCommitted);// 監聽技能規格更新OwnerASC->AbilitySpecDirtiedCallbacks.AddUObject(this, &UAbilityGauge::AbilitySpecUpdated);// 綁定升級點屬性變化事件 - 當玩家獲得/消耗升級點時觸發OwnerASC->GetGameplayAttributeValueChangeDelegate(UCHeroAttributeSet::GetUpgradePointAttribute()).AddUObject(this, &UAbilityGauge::UpgradePointUpdated);// 初始化升級點顯示(獲取當前值并刷新UI)bool bFound = false;float UpgradePoint = OwnerASC->GetGameplayAttributeValue(UCHeroAttributeSet::GetUpgradePointAttribute(), bFound);if (bFound){// 創建屬性變化數據結構(模擬屬性變化事件)FOnAttributeChangeData ChangeData;ChangeData.NewValue = UpgradePoint;// 手動調用升級點更新函數以刷新UIUpgradePointUpdated(ChangeData);}// 保存能力系統組件引用供后續使用OwnerAbilitySystemComponent = OwnerASC;}WholeNumberFormattingOptions.MaximumFractionalDigits = 0;TwoDigitNumberFormattingOptions.MaximumFractionalDigits = 2;
}void UAbilityGauge::NativeOnListItemObjectSet(UObject* ListItemObject)
{IUserObjectListEntry::NativeOnListItemObjectSet(ListItemObject);AbilityCDO = Cast<UGameplayAbility>(ListItemObject);// 獲取冷卻和消耗float CoolDownDuration = UCAbilitySystemStatics::GetStaticCooldownDurationForAbility(AbilityCDO);float Cost = UCAbilitySystemStatics::GetStaticCostForAbility(AbilityCDO);// 設置冷卻和消耗CooldownDurationText->SetText(FText::AsNumber(CoolDownDuration));CostText->SetText(FText::AsNumber(Cost));LevelGauge->GetDynamicMaterial()->SetScalarParameterValue(AbilityLevelParamName, 0);
}
// 這里我添加了技能等級的初始化
void UAbilityGauge::NativeOnListItemObjectSet(UObject* ListItemObject)
{IUserObjectListEntry::NativeOnListItemObjectSet(ListItemObject);AbilityCDO = Cast<UGameplayAbility>(ListItemObject);// 獲取冷卻和消耗float CoolDownDuration = UCAbilitySystemStatics::GetStaticCooldownDurationForAbility(AbilityCDO);float Cost = UCAbilitySystemStatics::GetStaticCostForAbility(AbilityCDO);// 設置冷卻和消耗CooldownDurationText->SetText(FText::AsNumber(CoolDownDuration));CostText->SetText(FText::AsNumber(Cost));// 初始化技能等級LevelGauge->GetDynamicMaterial()->SetScalarParameterValue(AbilityLevelParamName, 0);// 初始化技能的最大等級// 獲取當前技能規格const FGameplayAbilitySpec* AbilitySpec = GetAbilitySpec();if (AbilitySpec){float MaxLevel = AbilitySpec->InputID == static_cast<int32>(ECAbilityInputID::AbilityR) ? 3 : 5;LevelGauge->GetDynamicMaterial()->SetScalarParameterValue(MaxLevelParamName, MaxLevel);}
}const FGameplayAbilitySpec* UAbilityGauge::GetAbilitySpec()
{// 技能組件和技能對象不存在if (!OwnerAbilitySystemComponent || !AbilityCDO) return nullptr;// 技能緩存句柄無效,重新查找if (!CachedAbilitySpecHandle.IsValid()){// 根據技能類查找規格FGameplayAbilitySpec* FoundAbilitySpec = OwnerAbilitySystemComponent->FindAbilitySpecFromClass(AbilityCDO->GetClass());// 緩存找到的規格句柄(Handle)CachedAbilitySpecHandle = FoundAbilitySpec->Handle;return FoundAbilitySpec;}// 技能緩存句柄有效,返回緩存的規格return OwnerAbilitySystemComponent->FindAbilitySpecFromHandle(CachedAbilitySpecHandle);
}void UAbilityGauge::AbilitySpecUpdated(const FGameplayAbilitySpec& AbilitySpec)
{// 檢測技能是否為該圖標技能if (AbilitySpec.Ability != AbilityCDO) return;// 更新學習狀態bIsAbilityLearned = AbilitySpec.Level > 0;// 更新顯示的技能等級LevelGauge->GetDynamicMaterial()->SetScalarParameterValue(AbilityLevelParamName, AbilitySpec.Level);// 刷新技能能否釋放的狀態UpdateCanCast();
}void UAbilityGauge::UpdateCanCast()
{Icon->GetDynamicMaterial()->SetScalarParameterValue(CanCastAbilityParamName, bIsAbilityLearned ? 1 : 0);
}void UAbilityGauge::UpgradePointUpdated(const FOnAttributeChangeData& Data)
{// 檢查是否有可用升級點bool HasUpgradePoint = Data.NewValue > 0;// 獲取當前技能規格const FGameplayAbilitySpec* AbilitySpec = GetAbilitySpec();if (AbilitySpec && OwnerAbilitySystemComponent){// 獲取玩家等級bool bFound;float CurrentLevel = OwnerAbilitySystemComponent->GetGameplayAttributeValue(UCHeroAttributeSet::GetLevelAttribute(), bFound);if (bFound){// 如果技能已達目前的最大等級,不顯示升級提示if (UCAbilitySystemStatics::IsAbilityAtMaxLevel(*AbilitySpec, CurrentLevel)){Icon->GetDynamicMaterial()->SetScalarParameterValue(UpgradePointAvailableParamName, 0);return;}}}// 更新UI材質顯示(1=可升級,0=不可升級)Icon->GetDynamicMaterial()->SetScalarParameterValue(UpgradePointAvailableParamName, HasUpgradePoint ? 1 : 0);
}
兩個技能都可以升級
2級小技能只能升到一級,已經升級的不會閃爍
刷新技能升級后的藍耗和CD,以及藍不夠時技能進入灰色狀態
到CAbilitySystemStatics
中添加一些函數
// 檢查當前是否可以支付技能消耗(法力等)// @param AbilitySpec - 要檢查的技能規格數據// @param ASC - 所屬的能力系統組件// @return 如果資源足夠支付消耗返回true,否則falsestatic bool CheckAbilityCost(const FGameplayAbilitySpec& AbilitySpec, const UAbilitySystemComponent& ASC);// 檢查技能消耗(靜態)static bool CheckAbilityCostStatic(const UGameplayAbility* AbilityCDO, const UAbilitySystemComponent& ASC);// 獲取技能的當前法力消耗值// @param AbilityCDO - 技能的默認對象(Class Default Object)// @param ASC - 所屬的能力系統組件(用于獲取屬性修飾符)// @param AbilityLevel - 當前技能等級// @return 計算后的實際法力消耗值static float GetManaCostFor(const UGameplayAbility* AbilityCDO, const UAbilitySystemComponent& ASC, int AbilityLevel);// 獲取技能的當前冷卻時間// @param AbilityCDO - 技能的默認對象// @param ASC - 所屬的能力系統組件(用于獲取冷卻修飾符)// @param AbilityLevel - 當前技能等級// @return 計算后的實際冷卻時間(秒)static float GetCooldownDurationFor(const UGameplayAbility* AbilityCDO, const UAbilitySystemComponent& ASC, int AbilityLevel);// 獲取技能的剩余冷卻時間// @param AbilityCDO - 技能的默認對象// @param ASC - 所屬的能力系統組件// @return 剩余的冷卻時間(秒),如果不在冷卻中返回0static float GetCooldownRemainingFor(const UGameplayAbility* AbilityCDO, const UAbilitySystemComponent& ASC);
bool UCAbilitySystemStatics::CheckAbilityCost(const FGameplayAbilitySpec& AbilitySpec,const UAbilitySystemComponent& ASC)
{// 獲取技能const UGameplayAbility* AbilityCDO = AbilitySpec.Ability;if (AbilityCDO){// 調用技能的檢查消耗方法return AbilityCDO->CheckCost(AbilitySpec.Handle, ASC.AbilityActorInfo.Get());}// 技能無效return false;
}bool UCAbilitySystemStatics::CheckAbilityCostStatic(const UGameplayAbility* AbilityCDO, const UAbilitySystemComponent& ASC)
{if (AbilityCDO){// 使用空句柄調用檢查(適用于未實例化的技能)return AbilityCDO->CheckCost(FGameplayAbilitySpecHandle(), ASC.AbilityActorInfo.Get());}return false; // 無有效技能對象時默認返回false
}float UCAbilitySystemStatics::GetManaCostFor(const UGameplayAbility* AbilityCDO, const UAbilitySystemComponent& ASC,int AbilityLevel)
{float ManaCost = 0.f;if (AbilityCDO){// 獲取消耗效果UGameplayEffect* CostEffect = AbilityCDO->GetCostGameplayEffect();if (CostEffect && CostEffect->Modifiers.Num() > 0){// 創建臨時的效果規格FGameplayEffectSpecHandle EffectSpec = ASC.MakeOutgoingSpec(CostEffect->GetClass(), AbilityLevel, ASC.MakeEffectContext());// 獲取技能的消耗效果的靜態效果值CostEffect->Modifiers[0].ModifierMagnitude.AttemptCalculateMagnitude(*EffectSpec.Data.Get(),ManaCost);}}// 返回絕對值(確保消耗值始終為正數)return FMath::Abs(ManaCost);
}float UCAbilitySystemStatics::GetCooldownDurationFor(const UGameplayAbility* AbilityCDO,const UAbilitySystemComponent& ASC, int AbilityLevel)
{float CooldownDuration = 0.f;if (AbilityCDO){// 獲取技能關聯的冷卻效果UGameplayEffect* CooldownEffect = AbilityCDO->GetCooldownGameplayEffect();if (CooldownEffect){// 創建臨時的效果規格FGameplayEffectSpecHandle EffectSpec = ASC.MakeOutgoingSpec(CooldownEffect->GetClass(), AbilityLevel, ASC.MakeEffectContext());// 計算冷卻效果的實際持續時間(考慮冷卻縮減屬性)CooldownEffect->DurationMagnitude.AttemptCalculateMagnitude(*EffectSpec.Data.Get(), CooldownDuration);}}// 返回絕對值(確保冷卻時間始終為正數)return FMath::Abs(CooldownDuration);
}float UCAbilitySystemStatics::GetCooldownRemainingFor(const UGameplayAbility* AbilityCDO,const UAbilitySystemComponent& ASC)
{if (!AbilityCDO) return 0.f;// 獲取冷卻效果UGameplayEffect* CooldownEffect = AbilityCDO->GetCooldownGameplayEffect();if (!CooldownEffect) return 0.f;// 創建查詢條件:查找此技能對應的冷卻效果FGameplayEffectQuery CooldownEffectQuery;CooldownEffectQuery.EffectDefinition = CooldownEffect->GetClass();float CooldownRemaining = 0.f;// 獲取所有匹配效果的剩余時間TArray<float> CooldownRemainings = ASC.GetActiveEffectsTimeRemaining(CooldownEffectQuery);// 找出最長的剩余時間for (float Remaining : CooldownRemainings){if (Remaining > CooldownRemaining){CooldownRemaining = Remaining;}}return CooldownRemaining;
}
回到技能圖標中,添加法力回調
// 法力變化回調void ManaUpdated(const FOnAttributeChangeData& Data);
void UAbilityGauge::NativeConstruct()
{Super::NativeConstruct();// 隱藏冷卻計時器CooldownCounterText->SetVisibility(ESlateVisibility::Hidden);UAbilitySystemComponent* OwnerASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(GetOwningPlayerPawn());if (OwnerASC){// 監聽技能釋放OwnerASC->AbilityCommittedCallbacks.AddUObject(this, &UAbilityGauge::AbilityCommitted);// 監聽技能規格更新OwnerASC->AbilitySpecDirtiedCallbacks.AddUObject(this, &UAbilityGauge::AbilitySpecUpdated);// 綁定升級點屬性變化事件 - 當玩家獲得/消耗升級點時觸發OwnerASC->GetGameplayAttributeValueChangeDelegate(UCHeroAttributeSet::GetUpgradePointAttribute()).AddUObject(this, &UAbilityGauge::UpgradePointUpdated);// 綁定法力值屬性變化事件 - 當玩家法力值變化時觸發OwnerASC->GetGameplayAttributeValueChangeDelegate(UCAttributeSet::GetManaAttribute()).AddUObject(this, &UAbilityGauge::ManaUpdated);// 初始化升級點顯示(獲取當前值并刷新UI)bool bFound = false;float UpgradePoint = OwnerASC->GetGameplayAttributeValue(UCHeroAttributeSet::GetUpgradePointAttribute(), bFound);if (bFound){// 創建屬性變化數據結構(模擬屬性變化事件)FOnAttributeChangeData ChangeData;ChangeData.NewValue = UpgradePoint;// 手動調用升級點更新函數以刷新UIUpgradePointUpdated(ChangeData);}// 保存能力系統組件引用供后續使用OwnerAbilitySystemComponent = OwnerASC;}WholeNumberFormattingOptions.MaximumFractionalDigits = 0;TwoDigitNumberFormattingOptions.MaximumFractionalDigits = 2;
}void UAbilityGauge::AbilitySpecUpdated(const FGameplayAbilitySpec& AbilitySpec)
{// 檢測技能是否為該圖標技能if (AbilitySpec.Ability != AbilityCDO) return;// 更新學習狀態bIsAbilityLearned = AbilitySpec.Level > 0;// 更新顯示的技能等級LevelGauge->GetDynamicMaterial()->SetScalarParameterValue(AbilityLevelParamName, AbilitySpec.Level);// 刷新技能能否釋放的狀態UpdateCanCast();// 并顯示新的冷卻時間和法力消耗float NewCooldownDuration = UCAbilitySystemStatics::GetCooldownDurationFor(AbilitySpec.Ability, *OwnerAbilitySystemComponent, AbilitySpec.Level);float NewCost = UCAbilitySystemStatics::GetManaCostFor(AbilitySpec.Ability, *OwnerAbilitySystemComponent, AbilitySpec.Level);CooldownDurationText->SetText(FText::AsNumber(NewCooldownDuration));CostText->SetText(FText::AsNumber(NewCost));
}
void UAbilityGauge::UpdateCanCast()
{const FGameplayAbilitySpec* AbilitySpec = GetAbilitySpec();// 技能學習才能亮起來bool bCanCast = bIsAbilityLearned;// 看藍量是否夠if (AbilitySpec && OwnerAbilitySystemComponent){if (!UCAbilitySystemStatics::CheckAbilityCost(*AbilitySpec, *OwnerAbilitySystemComponent)){bCanCast = false;}}// 更新UI材質顯示(1=可釋放,0=不可釋放)Icon->GetDynamicMaterial()->SetScalarParameterValue(CanCastAbilityParamName, bCanCast ? 1 : 0);
}void UAbilityGauge::ManaUpdated(const FOnAttributeChangeData& Data)
{UpdateCanCast();
}
藍不夠的時候就會變成灰色
創建一個曲線表格
到GE中設置為可擴展浮點
升級后應用上去了
修改一下傷害的定義,讓這個傷害也跟著一起升級
// 傷害效果定義
USTRUCT(BlueprintType)
struct FGenericDamageEffectDef
{GENERATED_BODY()public:FGenericDamageEffectDef();// 傷害類型UPROPERTY(EditAnywhere)TSubclassOf<UGameplayEffect> DamageEffect;// 基礎傷害大小UPROPERTY(EditAnywhere)FScalableFloat BaseDamage;// 屬性的百分比傷害加成UPROPERTY(EditAnywhere)TMap<FGameplayAttribute, float> DamageTypes;// 力的大小UPROPERTY(EditAnywhere)FVector PushVelocity;
};
傷害的獲取改為如此
void UCGameplayAbility::MakeDamage(const FGenericDamageEffectDef& Damage, int Level)
{float NewDamage = Damage.BaseDamage.GetValueAtLevel(GetAbilityLevel());//通過標簽設置GE使用的配置for(auto& Pair : Damage.DamageTypes){bool bFound ;float AttributeValue = GetAbilitySystemComponentFromActorInfo()->GetGameplayAttributeValue(Pair.Key, bFound);if (bFound){NewDamage += AttributeValue * Pair.Value / 100.f;}}GetAbilitySystemComponentFromActorInfo()->SetNumericAttributeBase(UCAttributeSet::GetBaseDamageAttribute(), NewDamage);
}
添加一個傷害的值,隨便設
然后GA中設置一下
修復傷害數字特效只顯示3位數的問題
發現特效的顯示似乎有點問題,來到特效里面進行修改一下
創建一個hlsl
if (Result == 0) {NiagaraFloat = 1;
} else {float logVal = log10(abs(Result));NiagaraFloat = floor(logVal) + 1;
}
把這幾個刪了
添加一下,由于傷害的顯示這邊數字太大的時候似乎顯示并不正常,因此我就給他進行限制到99999
最后應用一下
這下就能顯示更高的傷害了