c# - 如何在 C# 中只影响一个委托侦听器?
问题描述
所以我在每个播放器上都有一个 ManaScript 和一个 ManaBar。问题是,Manabar 监听每个 OnManaPercentChanged,因此即使每个游戏对象都有自己的 ManaScript 并且我在一个特定实例上调用 ModifyMana,ManaBar 也会为每个玩家更新。我应该如何着手只更换一个 ManaBar?
手稿:
[SerializeField] const int maxMana = 100;
int currentMana;
public event Action<float> OnManaPercentChange = delegate { };
private void Awake()
{
currentMana = maxMana;
}
public void ModifyMana(int amount)
{
currentMana += amount;
float currentManaPct = (float)currentMana / (float)maxMana;
OnManaPercentChange(currentManaPct);
}
法力条:
[SerializeField] private Image foregroundImage;
private void Awake()
{
GetComponentInParent<PlayerMana>().OnManaPercentChange += HandleManachanged;
}
private void HandleManachanged(float percent)
{
foregroundImage.fillAmount = percent;
}
解决方案
推荐阅读
- pandas - 使用 pandas 数据框有效地搜索数据
- python - python tkinter pandas搜索功能的问题
- javascript - Ant Design Select - 选择所有过滤的选项
- python - 如何使用 TkInter 中的标签打印元组列表?
- javascript - JavaScript 中的日期和时间格式
- javascript - 为什么 ngIf 会破坏其中组件的功能?我应该如何处理这种情况?
- android - 设置 BottomNavigationView + 导航组件的问题
- flutter - Flutter/Dart:子类化冻结的数据类
- android - Android 推送通知机制
- c# - 如何使用 CsvHelper 库读取没有引用的 csv 文件?