概要
今回はインベントリに該当するアイテムがある場合の処理を実装します。
以下実装後の様子です。
開発環境
IDE:Rider
Unity:2020.3.42(LTS)
OS:Windows10
UnityEditor上の設定
無し
実装のポイント
取得したアイテムがインベントリーに含まれているか確認する関数FindItemInInventoryを追加します。
含まれている場合はインベントリの番号を返します。
含まていない場合は-1を返します。
インベントリ内に既にアイテムがある場合に追加する関数AddItemAtPositionを加えます。
従来に変数に加えてインベントリの追加位置を加えた変数を加えます。
コード部分
InventoryManager
InventoryManager.cs
using System.Collections.Generic;
using UnityEngine;
public class InventoryManager : SingletonMonobehaviour<InventoryManager>
{
private Dictionary<int, ItemDetails> itemDetailDictionary;
[SerializeField] private SO_ItemList itemList = null;
public int[] inventoryListCapacityIntArray;
public List<InventoryItem>[] inventoryLists;
protected override void Awake()
{
base.Awake();
CreateInventoryLists();
CreateItemDetailsDictionary();
}
private void CreateInventoryLists()
{
inventoryLists = new List<InventoryItem>[(int)InventoryLocation.count];
for (int i = 0; i < (int)InventoryLocation.count; i++)
{
inventoryLists[i] = new List<InventoryItem>();
}
inventoryListCapacityIntArray = new int[(int)InventoryLocation.count];
inventoryListCapacityIntArray[(int)InventoryLocation.player] = Settings.playerInitialInventoryCapacity;
}
private void CreateItemDetailsDictionary()
{
itemDetailDictionary = new Dictionary<int, ItemDetails>();
foreach (ItemDetails itemDetails in itemList.itemDetails)
{
itemDetailDictionary.Add(itemDetails.itemCode,itemDetails);
}
}
public ItemDetails GetItemDetails(int itemCode)
{
ItemDetails itemDetails;
if (itemDetailDictionary.TryGetValue(itemCode, out itemDetails))
{
return itemDetails;
}
else
{
return null;
}
}
public void AddItem(InventoryLocation inventoryLocation, Item item, GameObject gameObjectToDelete)
{
AddItem(inventoryLocation, item);
Destroy(gameObjectToDelete);
}
public void AddItem( InventoryLocation inventoryLocation,Item item)
{
int itemCode = item.ItemCode;
List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];
+ int itemPosition = FindItemInInventory(inventoryLocation, itemCode);+
+
+ if (itemPosition != -1)
+ {
+ AddItemAtPosition(inventoryList, itemCode, itemPosition);
+ }
+ else
+ {
+ AddItemAtPosition(inventoryList,itemCode);
+ }
}
private void AddItemAtPosition(List<InventoryItem> inventoryList, int itemCode)
{
InventoryItem inventoryItem = new InventoryItem();
inventoryItem.itemCode = itemCode;
inventoryItem.itemQuantity = 1;
inventoryList.Add(inventoryItem);
DebugPrintInventoryList(inventoryList);
}
+ private void AddItemAtPosition(List<InventoryItem> inventoryList, int itemCode, int position)
+ {
+ InventoryItem inventoryItem = new InventoryItem();
+
+ int quantity = inventoryList[position].itemQuantity + 1;
+ inventoryItem.itemQuantity = quantity;
+ inventoryItem.itemCode = itemCode;
+ inventoryList[position] = inventoryItem;
+ DebugPrintInventoryList(inventoryList);
+ }
private void DebugPrintInventoryList(List<InventoryItem> inventoryList)
{
foreach (InventoryItem inventoryItem in inventoryList)
{
Debug.Log("Item Description:"+ InventoryManager.Instance.GetItemDetails(inventoryItem.itemCode).itemDescription + " Item Quantity: " + inventoryItem.itemQuantity);
}
}
+ public int FindItemInInventory(InventoryLocation inventoryLocation, int itemCode)
+ {
+ List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];
+
+ for (int i = 0; i < inventoryList.Count; i++)
+ {
+ if (inventoryList[i].itemCode == itemCode)
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
}
ItemPickUp
ItemPickUp.cs
using System;
using UnityEngine;
public class ItemPickUp : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
Item item = collision.GetComponent<Item>();
if (item != null)
{
ItemDetails itemDetails = InventoryManager.Instance.GetItemDetails(item.ItemCode);
if (itemDetails.canBePickedUp == true)
{
InventoryManager.Instance.AddItem(InventoryLocation.player,item,collision.gameObject);
}
}
}
}
参考
C#
Unity Editor コンポーネント
Unity スクリプト
その他
Section1
github コミット分(個人確認用 privateなので見れません)