ふりかえり
前回はUIを整えました!ゲームらしさが一気に増しました!
前回の投稿はこちら↓
ゴールドでステータス強化!
これまでゴールドは集まるだけの存在でしたが、ついに使い道を実装します!
今回はゴールドを使ってプレイヤーのステータス(HP、攻撃力、防御力)を強化できるようにしました!
強化専用のUIを追加
画面下部に「強化」タブを設け、ボタンを押すとステータス強化パネルが開くようにしました。
強化ボタン3つ
- HP強化(+20):100G
- 攻撃力強化(+5):100G
- 防御力強化(+2):100G
それぞれのボタンでゴールドを消費し、即時でステータスが上昇します。強化後のメッセージ表示や、現在のステータス・消費ゴールド・増加量などもUIに反映されます。
スクリプトの変更
PowerUpManager.cs
- 強化処理
- ゴールドが足りていれば、各ステータスを増加&ゴールドを消費。
- 強化後はUIとメッセージを更新。
- UI制御
- ボタンで強化タブを開閉。
- ステータスやコスト表示をリアルタイム更新。
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class PowerUpManager : MonoBehaviour
{
[Header("UI References")]
public GameObject powerUpTab;
public Button openTabButton;
public Button hpUpButton;
public Button attackUpButton;
public Button defenseUpButton;
public TextMeshProUGUI messageText;
public TextMeshProUGUI hpText;
public TextMeshProUGUI attackText;
public TextMeshProUGUI defenseText;
// public TextMeshProUGUI goldText;
public TextMeshProUGUI hpCostText;
public TextMeshProUGUI attackCostText;
public TextMeshProUGUI defenseCostText;
public TextMeshProUGUI hpValueText;
public TextMeshProUGUI attackValueText;
public TextMeshProUGUI defenseValueText;
[Header("強化コスト")]
public int hpUpCost = 100;
public int attackUpCost = 100;
public int defenseUpCost = 100;
public int hpUpValue = 20;
public int attackUpValue = 5;
public int defenseUpValue = 2;
public PlayerManager playerManager;
private bool isTabOpen = false;
void Start()
{
if (openTabButton != null)
{
openTabButton.onClick.AddListener(TogglePowerUpTab);
}
if (hpUpButton != null)
{
hpUpButton.onClick.AddListener(PowerUpHP);
}
if (attackUpButton != null)
{
attackUpButton.onClick.AddListener(PowerUpAttack);
}
if (defenseUpButton != null)
{
defenseUpButton.onClick.AddListener(PowerUpDefense);
}
if (powerUpTab != null) powerUpTab.SetActive(false);
UpdateUI();
}
public void TogglePowerUpTab()
{
isTabOpen = !isTabOpen;
if (powerUpTab != null)
{
powerUpTab.SetActive(isTabOpen);
}
if (messageText != null) messageText.text = "";
UpdateUI();
}
public void PowerUpHP()
{
if (playerManager.player.gold >= hpUpCost)
{
playerManager.player.gold -= hpUpCost;
playerManager.player.maxHP += hpUpValue;
playerManager.player.currentHP = playerManager.player.maxHP;
ShowMessage($"HPが{hpUpValue}増加しました!");
}
else
{
ShowMessage("ゴールドが足りません。");
}
playerManager.playerUIManager.UpdateUI();
UpdateUI();
}
public void PowerUpAttack()
{
if (playerManager.player.gold >= attackUpCost)
{
playerManager.player.gold -= attackUpCost;
playerManager.player.attack += attackUpValue;
ShowMessage($"攻撃力が{attackUpValue}増加しました!");
}
else
{
ShowMessage("ゴールドが足りません。");
}
playerManager.playerUIManager.UpdateUI();
UpdateUI();
}
public void PowerUpDefense()
{
if (playerManager.player.gold >= defenseUpCost)
{
playerManager.player.gold -= defenseUpCost;
playerManager.player.defense += defenseUpValue;
ShowMessage($"防御力が{defenseUpValue}増加しました!");
}
else
{
ShowMessage("ゴールドが足りません。");
}
playerManager.playerUIManager.UpdateUI();
UpdateUI();
}
private void UpdateUI()
{
if (playerManager == null || playerManager.player == null) return;
// if (goldText != null) goldText.text = "G: " + playerManager.player.gold;
if (hpText != null) hpText.text = "HP: " + playerManager.player.maxHP;
if (attackText != null) attackText.text = "攻撃: " + playerManager.player.attack;
if (defenseText != null) defenseText.text = "防御: " + playerManager.player.defense;
if (hpCostText != null) hpCostText.text = $"消費G: {hpUpCost}";
if (attackCostText != null) attackCostText.text = $"消費G: {attackUpCost}";
if (defenseCostText != null) defenseCostText.text = $"消費G: {defenseUpCost}";
if (hpValueText != null) hpValueText.text = $"+{hpUpValue}";
if (attackValueText != null) attackValueText.text = $"+{attackUpValue}";
if (defenseValueText != null) defenseValueText.text = $"+{defenseUpValue}";
}
private void ShowMessage(string msg)
{
if (messageText != null) messageText.text = msg;
}
}
Unityでの設定手順
- 空のGameObjectを作成し「PowerUpManager.cs」をアタッチ。
- UIを作成:
- タブ(Panel)
- 強化ボタン(HP / 攻撃 / 防御)
- 各種テキスト(現在値、消費G、増加量など)
- PlayerManagerとの紐づけを忘れずに。

動作確認
強化タブを開くと、現在のステータスと強化内容が表示されます。


ボタンを押すと、ゴールドが減ってステータスが増加。

まとめ
今回は、ゴールドを消費してステータスを強化する仕組みを実装しました!
強化していくとゲームのテンポも良くなり、育成の楽しさが一気に増しますね!
次回予告
次回は、スキルを実装してバトルをさらに戦略的にしていきます!
お楽しみに!




コメント