当前位置: 首页 > ai >正文

UE5多人MOBA+GAS 24、创建属性UI(一)

文章目录

  • 添加一些新的属性
  • 创新一个UI用来显示属性
  • 在蓝图中继承UI,并显示UI


添加一些新的属性

添加一些新的属性

UCLASS()
class UCAttributeSet : public UAttributeSet
{// 攻击力UPROPERTY(ReplicatedUsing = OnRep_AttackDamage)FGameplayAttributeData AttackDamage;ATTRIBUTE_ACCESSORS(UCAttributeSet, AttackDamage)// 护甲值UPROPERTY(ReplicatedUsing = OnRep_Armor)FGameplayAttributeData Armor;ATTRIBUTE_ACCESSORS(UCAttributeSet, Armor)// 移动速度UPROPERTY(ReplicatedUsing = OnRep_MoveSpeed)FGameplayAttributeData MoveSpeed;ATTRIBUTE_ACCESSORS(UCAttributeSet, MoveSpeed)// 移动加速度UPROPERTY(ReplicatedUsing = OnRep_MoveAcceleration)FGameplayAttributeData MoveAcceleration;ATTRIBUTE_ACCESSORS(UCAttributeSet, MoveAcceleration)UFUNCTION()void OnRep_AttackDamage(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_Armor(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_MoveSpeed(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_MoveAcceleration(const FGameplayAttributeData& OldValue);
};
void UCAttributeSet::GetLifetimeReplicatedProps(TArray<class FLifetimeProperty>& OutLifetimeProps) const
{Super::GetLifetimeReplicatedProps(OutLifetimeProps);DOREPLIFETIME_CONDITION_NOTIFY(UCAttributeSet, Health, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCAttributeSet, MaxHealth, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCAttributeSet, Mana, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCAttributeSet, MaxMana, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCAttributeSet, AttackDamage, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCAttributeSet, Armor, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCAttributeSet, MoveSpeed, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCAttributeSet, MoveAcceleration, COND_None, REPNOTIFY_Always);
}void UCAttributeSet::OnRep_AttackDamage(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCAttributeSet, AttackDamage, OldValue);
}void UCAttributeSet::OnRep_Armor(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCAttributeSet, Armor, OldValue);
}void UCAttributeSet::OnRep_MoveSpeed(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCAttributeSet, MoveSpeed, OldValue);
}void UCAttributeSet::OnRep_MoveAcceleration(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCAttributeSet, MoveAcceleration, OldValue);
}

再创建一个玩家角色用的属性

// 幻雨喜欢小猫咪#pragma once#include "CoreMinimal.h"
#include "AttributeSet.h"
#include "AbilitySystemComponent.h"
#include "CHeroAttributeSet.generated.h"// 属性访问器宏,自动生成属性的Getter/Setter等
#define ATTRIBUTE_ACCESSORS(ClassName, PropertyName) \GAMEPLAYATTRIBUTE_PROPERTY_GETTER(ClassName, PropertyName) \GAMEPLAYATTRIBUTE_VALUE_GETTER(PropertyName) \GAMEPLAYATTRIBUTE_VALUE_SETTER(PropertyName) \GAMEPLAYATTRIBUTE_VALUE_INITTER(PropertyName)/*** 英雄属性集(UCHeroAttributeSet)* 用于管理英雄的成长属性、经验、等级、升级点、金币等* 支持属性同步、属性变化回调等功能*/
UCLASS()
class UCHeroAttributeSet : public UAttributeSet
{GENERATED_BODY()
public:// 属性访问器声明(自动生成标准接口)ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, Intelligence)             // 智力ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, Strength)                 // 力量ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, Experience)               // 当前经验ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, PrevLevelExperience)      // 上一级所需经验ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, NextLevelExperience)      // 下一级所需经验ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, Level)                    // 当前等级ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, UpgradePoint)             // 可用升级点ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, MaxLevel)                 // 最大等级ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, MaxLevelExperience)       // 满级所需经验ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, Gold)                     // 金币ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, StrengthGrowthRate)       // 力量成长率ATTRIBUTE_ACCESSORS(UCHeroAttributeSet, IntelligenceGrowthRate)   // 智力成长率// 属性同步(网络复制)配置virtual void GetLifetimeReplicatedProps( TArray< class FLifetimeProperty > & OutLifetimeProps ) const override;
private:// 智力UPROPERTY(ReplicatedUsing = OnRep_Intelligence)FGameplayAttributeData Intelligence;// 力量UPROPERTY(ReplicatedUsing = OnRep_Strength)FGameplayAttributeData Strength;// 当前经验UPROPERTY(ReplicatedUsing = OnRep_Experience)FGameplayAttributeData Experience;// 力量成长率UPROPERTY()FGameplayAttributeData StrengthGrowthRate;// 智力成长率UPROPERTY()FGameplayAttributeData IntelligenceGrowthRate;// 上一级所需经验UPROPERTY(ReplicatedUsing = OnRep_PrevLevelExperience)FGameplayAttributeData PrevLevelExperience;// 下一级所需经验UPROPERTY(ReplicatedUsing = OnRep_NextLevelExperience)FGameplayAttributeData NextLevelExperience;// 当前等级UPROPERTY(ReplicatedUsing = OnRep_Level)FGameplayAttributeData Level;// 可用升级点UPROPERTY(ReplicatedUsing = OnRep_UpgradePoint)FGameplayAttributeData UpgradePoint;// 最大等级UPROPERTY(ReplicatedUsing = OnRep_MaxLevel)FGameplayAttributeData MaxLevel;// 满级所需经验UPROPERTY(ReplicatedUsing = OnRep_MaxLevelExperience)FGameplayAttributeData MaxLevelExperience;// 金币UPROPERTY(ReplicatedUsing = OnRep_Gold)FGameplayAttributeData Gold;// 属性同步回调(用于客户端属性变化通知)UFUNCTION()void OnRep_Intelligence(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_Strength(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_Experience(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_PrevLevelExperience(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_NextLevelExperience(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_Level(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_UpgradePoint(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_MaxLevel(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_MaxLevelExperience(const FGameplayAttributeData& OldValue);UFUNCTION()void OnRep_Gold(const FGameplayAttributeData& OldValue);
};
// 幻雨喜欢小猫咪#include "GAS/Core/CHeroAttributeSet.h"
#include "Net/UnrealNetwork.h"
#include "GameplayEffectExtension.h"void UCHeroAttributeSet::GetLifetimeReplicatedProps(TArray<class FLifetimeProperty>& OutLifetimeProps) const
{Super::GetLifetimeReplicatedProps(OutLifetimeProps);DOREPLIFETIME_CONDITION_NOTIFY(UCHeroAttributeSet, Intelligence, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCHeroAttributeSet, Strength, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCHeroAttributeSet, Experience, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCHeroAttributeSet, PrevLevelExperience, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCHeroAttributeSet, NextLevelExperience, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCHeroAttributeSet, Level, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCHeroAttributeSet, UpgradePoint, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCHeroAttributeSet, MaxLevel, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCHeroAttributeSet, MaxLevelExperience, COND_None, REPNOTIFY_Always);DOREPLIFETIME_CONDITION_NOTIFY(UCHeroAttributeSet, Gold, COND_None, REPNOTIFY_Always);
}void UCHeroAttributeSet::OnRep_Intelligence(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCHeroAttributeSet, Intelligence, OldValue);
}void UCHeroAttributeSet::OnRep_Strength(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCHeroAttributeSet, Strength, OldValue);
}void UCHeroAttributeSet::OnRep_Experience(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCHeroAttributeSet, Experience, OldValue);
}void UCHeroAttributeSet::OnRep_PrevLevelExperience(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCHeroAttributeSet, PrevLevelExperience, OldValue);
}void UCHeroAttributeSet::OnRep_NextLevelExperience(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCHeroAttributeSet, NextLevelExperience, OldValue);
}void UCHeroAttributeSet::OnRep_Level(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCHeroAttributeSet, Level, OldValue);
}void UCHeroAttributeSet::OnRep_UpgradePoint(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCHeroAttributeSet, UpgradePoint, OldValue);
}void UCHeroAttributeSet::OnRep_MaxLevel(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCHeroAttributeSet, MaxLevel, OldValue);
}void UCHeroAttributeSet::OnRep_MaxLevelExperience(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCHeroAttributeSet, MaxLevelExperience, OldValue);
}void UCHeroAttributeSet::OnRep_Gold(const FGameplayAttributeData& OldValue)
{GAMEPLAYATTRIBUTE_REPNOTIFY(UCHeroAttributeSet, Gold, OldValue);
}

在玩家角色中创建该属性集

#pragma region Gameplay Ability
private:// 瞄准状态变化时回调virtual void OnAimStateChanged(bool bIsAiming) override;UPROPERTY()TObjectPtr<UCHeroAttributeSet> HeroAttributeSet;
#pragma endregion

在构造函数中创建该属性集

ACPlayerCharacter::ACPlayerCharacter()
{HeroAttributeSet = CreateDefaultSubobject<UCHeroAttributeSet>(TEXT("HeroAttributeSet"));
}

创新一个UI用来显示属性

创建一个StatsGauge将属性传入过去显示

// 幻雨喜欢小猫咪#pragma once#include "CoreMinimal.h"
#include "AttributeSet.h"
#include "Blueprint/UserWidget.h"
#include "GameplayEffectTypes.h"
#include "Components/Image.h"
#include "Components/TextBlock.h"
#include "StatsGauge.generated.h"/*** 绑定属性数值的UI*/
UCLASS()
class CRUNCH_API UStatsGauge : public UUserWidget
{GENERATED_BODY()
public:// 构建前virtual void NativePreConstruct() override;// 构建时virtual void NativeConstruct() override;private:// 属性图标控件UPROPERTY(meta=(BindWidget))TObjectPtr<UImage> Icon;// 属性数值文本控件UPROPERTY(meta=(BindWidget))TObjectPtr<UTextBlock> AttributeText;// 需要显示的属性UPROPERTY(EditAnywhere, Category = "Attribute")FGameplayAttribute Attribute;// 图标资源UPROPERTY(EditAnywhere, Category = "Visual")TObjectPtr<UTexture2D> IconTexture;// 设置属性数值显示void SetValue(float NewVal);// 数字格式化选项FNumberFormattingOptions NumberFormattingOptions;// 属性变化回调void AttributeChanged(const FOnAttributeChangeData& Data);
};
// 幻雨喜欢小猫咪#include "UI/Gameplay/StatsGauge.h"#include "AbilitySystemBlueprintLibrary.h"
#include "AbilitySystemComponent.h"void UStatsGauge::NativePreConstruct()
{Super::NativePreConstruct();Icon->SetBrushFromTexture(IconTexture);
}void UStatsGauge::NativeConstruct()
{Super::NativeConstruct();// 设置为整数格式NumberFormattingOptions.MaximumFractionalDigits = 0;APawn* OwnerPlayerPawn = GetOwningPlayerPawn();if (!OwnerPlayerPawn) return;UAbilitySystemComponent* OwnerASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(OwnerPlayerPawn);if (OwnerASC){bool bFound;float AttributeValue = OwnerASC->GetGameplayAttributeValue(Attribute, bFound);if (bFound){SetValue(AttributeValue);}OwnerASC->GetGameplayAttributeValueChangeDelegate(Attribute).AddUObject(this, &UStatsGauge::AttributeChanged);}
}void UStatsGauge::SetValue(float NewVal)
{AttributeText->SetText(FText::AsNumber(NewVal, &NumberFormattingOptions));
}void UStatsGauge::AttributeChanged(const FOnAttributeChangeData& Data)
{SetValue(Data.NewValue);
}

将属性UI放到游戏界面中,在GameplayWidget中添加几个属性UI

	// 属性面板:攻击力显示控件UPROPERTY(meta=(BindWidget))TObjectPtr<UStatsGauge> AttackDamageGauge;// 属性面板:护甲显示控件UPROPERTY(meta=(BindWidget))TObjectPtr<UStatsGauge> ArmorGauge;// 属性面板:移动速度显示控件UPROPERTY(meta=(BindWidget))TObjectPtr<UStatsGauge> MoveSpeedGauge;// 属性面板:智力显示控件UPROPERTY(meta=(BindWidget))TObjectPtr<UStatsGauge> IntelligenceGauge;// 属性面板:力量显示控件UPROPERTY(meta=(BindWidget))TObjectPtr<UStatsGauge> StrengthGauge;

在蓝图中继承UI,并显示UI

去到UE中创建蓝图版本的属性UI,再将其添加到GameplayWidget中去
在这里插入图片描述

在这里插入图片描述
添加一个边界作为底色
在这里插入图片描述

http://www.xdnf.cn/news/15499.html

相关文章:

  • 插板式系统的“生命线“:EtherCAT分布式供电该如何实现?
  • 第13章 AB实验平台的建设
  • 解锁高效Excel技能:摆脱鼠标,快速编辑单元格
  • 凯伦股份融合复合瓦:新时代可焊接物理防腐金属屋面系统方案
  • Mysql练习
  • Linux命令大全
  • 第五章 管道工程 5.4 管道安全质量控制
  • 设计一款用于捕捉动态产品视频的摄像机器人
  • 元宇宙经济:虚实融合引发经济新变革
  • 前端学习7:CSS过渡与动画--补间动画 (Transition) vs 关键帧动画 (Animation)
  • Linux切换到Jenkins用户解决Jenkins Host key verification failed
  • 工业相机GigE数据接口的优势及应用
  • 以太网供电与自愈网络对音视频系统的益处
  • 重学前端006 --- 响应式网页设计 CSS 弹性盒子
  • ssl相关命令生成证书
  • 阿里云 RabbitMQ 可观测性最佳实践
  • 蓝光三维扫描技术:手机闪光灯模块全尺寸3D检测的高效解决方案
  • 逆功率检测设备防逆流解决方案守护电网安全
  • 智能体架构深度解构:一次用户请求的完整旅程
  • MyBatis 之分页四式传参与聚合、主键操作全解
  • MySQL学习——面试版
  • Navicat操作指南:MySQL数据库配置与Todo应用部署
  • 从零开始的云计算生活——番外3,LVS+KeepAlived+Nginx高可用实现方案
  • 深入理解概率图模型:贝叶斯网络因子分解、d-分离与马尔可夫毯
  • vuex原理以及实现
  • MySQL基础学习之DML,DQL(二)
  • 【docker】将本地镜像打包部署到服务器上
  • 架构设计之计算高性能——单体服务器高性能
  • 从混沌到秩序:数据科学的热力学第二定律破局——线性回归的熵减模型 × 最小二乘的能量最小化 × 梯度下降的负反馈控制系统,用物理定律重构智能算法的统一场论
  • Java数据结构第二十五期:红黑树传奇,当二叉树穿上 “红黑铠甲” 应对失衡挑战