FarmRPG Unity Inventory&Item Part2  2/2 InventoryManager  playerがItemに接触したときの処理を実装　Collider2D OnTriggerEnter2D

# 概要
今回はplayerがItemに接触した時にログを出す処理を追加します。

以下gif画像は実装後の画像です。

![ffffffffffffdsafdasnjlkljllkklll.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/b042abc0-bd96-e2a5-30de-7f0cc288603b.gif)





# 開発環境

IDE:Rider
Unity:2020.3.42(LTS)
OS:Windows10

# UnityEditor上の設定
## Playerオブジェクト
Is TriggerをOFFにします。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/fcd13328-e24e-bb72-2e69-0db6a1bb04dc.png)


## Itemオブジェクト
ItemObjectにBoxColliderコンポーネントを追加されていることを確認します。
Is TriggerをONにします。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/769f81bf-d91a-fb3b-a48d-a6f2d2690056.png)

# 実装のポイント
PlayerとItemが接触したときの流れ

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/b44ac424-6476-43ce-c21f-8740ab9e5b7b.png)



# コード部分

## ItemPickUp

```diff_c_sharp:ItemPickUp.cs
using System;
using UnityEngine;


public class ItemPickUp : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collsion)
    {
        Item item = collsion.GetComponent<Item>();
        if (item != null)
        {
            ItemDetails itemDetails = InventoryManager.Instance.GetItemDetails(item.ItemCode);
            Debug.Log(itemDetails.itemDescription);
        }
    }
}
```

## InventoryManager

```diff_c_sharp:InventoryManager.cs
using System.Collections.Generic;
using UnityEngine;


public class InventoryManager : SingletonMonobehaviour<InventoryManager>
{
    private Dictionary<int, ItemDetails> itemDetailDictionary;
    [SerializeField] private SO_ItemList itemList = null;

    private void Start()
    {
        CreateItemDetailsDictionary();
    }

    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;
+        }
+    }
}


```







# 参考

## C#
### Dictionary<TKey,TValue>.TryGetValue(TKey, TValue)

https://learn.microsoft.com/ja-jp/dotnet/api/system.collections.generic.dictionary-2.trygetvalue?view=net-7.0

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/993b70e0-577c-5f8b-2573-fec901fbeae2.png)
## Unity Editor コンポーネント

https://docs.unity3d.com/ja/2019.4/Manual/class-BoxCollider2D.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/93eb26f3-7efa-6591-503e-668dd4d1a462.png)



## Unity スクリプト

### OnTriggerEnter2D

https://docs.unity3d.com/ja/2021.1/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/bff4c7d5-2454-1940-93d5-bc94f4324bd1.png)


## その他

Section7 22 InventoryManager Class

https://www.udemy.com/course/unity-2d-game-developer-course-farming-rpg/learn/lecture/19845880#content


github コミット分（個人確認用　privateなので見れません）

https://github.com/RYA234/LearnFarmRPG/commit/994b41379983a7f6f161a3c98efa0290486f0907