0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unityでつくる2DRPG ~⑧プレイヤーのステータス管理

Last updated at Posted at 2025-12-24

はじめに

前回は、簡単なコードの解説を行いました。

今回は、プレイヤーのステータス管理をするスクリプトを作成します。

スクリプトの作成

今回作成するスクリプトは2つです。

PlayerStats.cs

スクリプトを新規作成し、名前を「PlayerStats」にしてください。
その後、以下のコードをコピペしてください。

using UnityEngine;
using System;
using System.Xml.Linq;

[Serializable]
public class CharacterStats
{
    public string charName = "Hero";
    public Sprite portrait;
    public int level = 1;
    public int maxHP = 100;
    public int hp = 100;
    public int maxMP = 30;
    public int mp = 30;
    public int atk = 10;
    public int def = 5;
    public int matk = 6;
    public int mdef = 3;
    public int speed = 5;

   // public MagicData[] knownMagic = new MagicData[0];

    // 累積経験値(レベルアップ処理は GainExp にて)
    public int totalExp = 0;
    public int expToNextLevel = 100;

    public bool IsAlive => hp > 0;
}

[RequireComponent(typeof(CharacterInventory))]
public class PlayerStats : MonoBehaviour
{
    public CharacterStats stats = new CharacterStats();

    public CharacterInventory Inventory { get; private set; }

    private void Awake()
    {
        Inventory = GetComponent<CharacterInventory>();
    }

    public void TakeDamage(int amount)
    {
        stats.hp = Mathf.Max(0, stats.hp - amount);
    }

    public void Heal(int amount)
    {
        stats.hp = Mathf.Min(stats.maxHP, stats.hp + amount);
    }

    public void UseMP(int amount)
    {
        stats.mp = Mathf.Max(0, stats.mp - amount);
    }

    public void RecoverMP(int amount)
    {
        stats.mp = Mathf.Min(stats.maxMP, stats.mp + amount);
    }

    public void GainExp(int exp)
    {
        stats.totalExp += exp;
        while (stats.totalExp >= stats.expToNextLevel)
        {
            stats.totalExp -= stats.expToNextLevel;
            LevelUp();
        }
    }

    void LevelUp()
    {
        stats.level++;
        stats.maxHP += 10;
        stats.maxMP += 5;
        stats.atk += 2;
        stats.def += 1;
        stats.hp = stats.maxHP;
        stats.mp = stats.maxMP;
        stats.expToNextLevel = Mathf.RoundToInt(stats.expToNextLevel * 1.2f);
        Debug.Log($"{stats.charName} がレベルアップ! 現在レベル: {stats.level}");
    }
}

CharacterInventory.cs

スクリプトを新規作成し、名前を「CharacterInventory」としてください。
その後、以下のコードをコピペしてください。

//CharacterInventory.cs
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class InventorySlot
{
//    public ItemData item;
    public int count;
}

public class CharacterInventory : MonoBehaviour
{
    public int personalCapacity = 15;
    public List<InventorySlot> slots = new List<InventorySlot>();

    private void Awake()
    {
   //     while (slots.Count < personalCapacity) slots.Add(new InventorySlot() { item = null, count = 0 });
    }


 //   public bool AddItem(ItemData item, int count = 1)
 //   {
 //       for (int i = 0; i < slots.Count; i++)
 //       {
 //          if (slots[i].item == item)
 //           {
 //               slots[i].count += count;
 //               return true;
 //           }
 //       }

 //       for (int i = 0; i < slots.Count; i++)
 //       {
 //           if (slots[i].item == null)
 //           {
 //               slots[i].item = item;
 //               slots[i].count = count;
 //               return true;
 //           }
 //       }
 //       return false;
 //   }

 //   public bool RemoveItem(ItemData item, int count = 1)
 //   {
 //       for (int i = 0; i < slots.Count; i++)
 //       {
 //          if (slots[i].item == item)
 //           {
 //               if (slots[i].count < count) return false;
 //               slots[i].count -= count;
 //               if (slots[i].count == 0) { slots[i].item = null; }
 //               return true;
 //           }
 //       }
 //       return false;
 //   }
}

コードのアタッチ

スクリプトを作成したら、どちらもPlayerにアタッチしてください。

インスペクター解説

インスペクターに表示されている各変数について、説明を入れておきます。

PlayerStats.cs

・Char Name ~ プレイヤーの名前です
・Portrait ~ 戦闘中に表示される、プレイヤーの立ち絵です。
・その他 ~ 他の値は、文字通り各種パラメータを示しています。

なおPortraitに、プレイヤーの見た目となるスプライトをアタッチしておいてください。

CharacterInventory.cs

・Personal Inventory ~ 一人が持つことのできる最大アイテム数です。
・Slots ~ 具体的にアイテムを入れる欄です。

Personal Inventoryは基本的に15のままにしておいてください。

おわり

今回は、プレイヤーのステータス管理を担うスクリプトを作りました。

次回は、アイテムを作成します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?