FarmRPG　Unity Inventory&Item Part6 Inventoryの情報をUIに反映 オブサーバーパターンを使用

# 概要
今回はアイテムを取得した際インベントリーUIに反映するようにします。

![0601.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/b94c8eca-4e2f-0dfa-b849-b915bdb06064.gif)





# 開発環境

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

# UnityEditor上の設定

## UIInventoryBarにスクリプトをアタッチ

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/dd2688dd-b045-9c33-66ce-7d74d64fc730.png)

## UIInventorySlotにスクリプトをアタッチ

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/b1bcaa70-446a-35ea-6286-1e9cb646d201.png)



# 実装のポイント
Observale：InventoryManager
Observer:UIInventoryBarとしてオブサーバーパターンを作成します。


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/109c088f-f45b-bf9f-c298-60404751c9aa.png)


# コード部分



## LevelManager

```diff_c_sharp:LevelManager.cs


```

## EventHandler

```diff_c_sharp:EventHandler.cs
using System;
using System.Collections.Generic;

public static class EventHandler
{
+    public static event Action<InventoryLocation, List<InventoryItem>> InventoryUpdatedEvent;
    
+    public static void CallInventoryUpdatedEvent(InventoryLocation inventoryLocation, List<InventoryItem> inventoryList)
+    {
+        if (InventoryUpdatedEvent != null)
+            InventoryUpdatedEvent(inventoryLocation, inventoryList);
+    }

}

```


## 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;
    public int[] inventoryListCapacityIntArray;
    public List<InventoryItem>[] inventoryLists;
    protected override void Awake()
    {
        base.Awake();
        CreateInventoryLists();
        CreateItemDetailsDictionary();
    }

    private void CreateInventoryLists()
    {}
    
    private void CreateItemDetailsDictionary()
    {}

    public ItemDetails GetItemDetails(int itemCode)
    {  }

    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);
        }
        
+        EventHandler.CallInventoryUpdatedEvent(inventoryLocation,inventoryLists[(int)inventoryLocation]);
    }

    private void AddItemAtPosition(List<InventoryItem> inventoryList, int itemCode)
    {}

    private void AddItemAtPosition(List<InventoryItem> inventoryList, int itemCode, int position)
    {}
    
    private void DebugPrintInventoryList(List<InventoryItem> inventoryList)
    {}

    public int FindItemInInventory(InventoryLocation inventoryLocation, int itemCode)
    {}


    
}


```

## UIInventorySlot

```diff_c_sharp:UIInventorySlot.cs
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UIInventorySlot : MonoBehaviour
{
    public Image inventorySlotHighlight;
    public Image inventorySlotImage;
    public TextMeshProUGUI textMeshProUGUI;

    public ItemDetails itemDetails;
    public int itemQuantity;

}


```


## UIInventoryBar

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


public class UIInventoryBar : MonoBehaviour
{
    [SerializeField] private Sprite blank16x16sprite = null;
    [SerializeField] private UIInventorySlot[] inventorySlot = null;

    private void OnDisable()
    {
        EventHandler.InventoryUpdatedEvent -= InventoryUpdated;
    }

    private void OnEnable()
    {
        EventHandler.InventoryUpdatedEvent += InventoryUpdated;
    }

    private void ClearInventorySlots()
    {
        if (inventorySlot.Length > 0)
        {
            for (int i = 0; i < inventorySlot.Length; i++)
            {
                inventorySlot[i].inventorySlotImage.sprite = blank16x16sprite;
                inventorySlot[i].textMeshProUGUI.text = "";
                inventorySlot[i].itemDetails = null;
                inventorySlot[i].itemQuantity = 0;                
            }
        }
    }
    
    
    private void InventoryUpdated(InventoryLocation inventoryLocation, List<InventoryItem> inventoryList)
    {
        if (inventoryLocation == InventoryLocation.player)
        {
            ClearInventorySlots();
            if (inventorySlot.Length > 0 && inventoryList.Count > 0)
            {
                for (int i = 0; i < inventorySlot.Length; i++)
                {
                    if (i < inventoryList.Count)
                    {
                        int itemCode = inventoryList[i].itemCode;
                        ItemDetails itemDetails = InventoryManager.Instance.GetItemDetails(itemCode);

                        if (itemDetails != null)
                        {
                            inventorySlot[i].inventorySlotImage.sprite = itemDetails.itemSprite;
                            inventorySlot[i].textMeshProUGUI.text = inventoryList[i].itemQuantity.ToString();
                            inventorySlot[i].itemDetails = itemDetails;
                            inventorySlot[i].itemQuantity = inventoryList[i].itemQuantity;

                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
        } 
    }
    
    
}


```





# 参考

## C#

## Unity Editor コンポーネント

## Unity スクリプト

### TextMeshProUGUI

https://docs.unity3d.com/Packages/com.unity.textmeshpro@1.1/api/TMPro.TMP_Text.html#TMPro_TMP_Text_text

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/fdc1bbb9-5d4b-42da-2e49-2e0f605a1a7c.png)

### Image.sprite

https://docs.unity3d.com/ja/2018.4/ScriptReference/UI.Image-sprite.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/d33a3934-898d-770d-3c42-c9b63eef9604.png)


## その他

Section7 27


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

https://github.com/RYA234/LearnFarmRPG/commit/b46555c9ec354d5db9de46ef9b2d7cc2c6de0232