FarmRPG　Unity Inventory&Item Part9 inventorySlotのアイテムを交換

# 概要
今回はInventorySlotのアイテムを交換する機能を実装します。
以下実装後の様子です。

![0604.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/939a34b4-aaf3-cfce-9059-219df336f38d.gif)




# 開発環境

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

# UnityEditor上の設定
なし

# 実装のポイント
InventoryManagerクラスに新規メソッドSwapInventoryItemを加えます。
SwapInventoryItemではinventoryList[0]の交換元と交換先の要素を交換し、UIに反映します。

UIInventorySlotのEndDragメソッドの内容を修正します。
InventoryManagerクラスのSwapInventoryItemを実行するようにします。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/e469ce53-0e54-3298-9352-bb52ddc6d566.png)

fromSlotNumber
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/ce9447fb-9c04-2e8b-a254-5b5a1c8e7c17.png)

toSlotNumber

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/d9b2f8ce-baaa-6a00-9da9-78e70af4894f.png)


# コード部分

## UIInventorySlot

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

public class UIInventorySlot : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    private Camera mainCamera;
    private GameObject draggedItem;
    private Transform parentItem;
    
    public Image inventorySlotHighlight;
    public Image inventorySlotImage;
    public TextMeshProUGUI textMeshProUGUI;
    [SerializeField] private UIInventoryBar inventoryBar = null;
    public ItemDetails itemDetails;
    [SerializeField] private GameObject itemPrefab;
    public int itemQuantity;
+    [SerializeField] private int slotNumber = 0;
    
    public void OnEndDrag(PointerEventData eventData)
    {
        if (draggedItem != null)
        {
            Destroy(draggedItem);
            if (eventData.pointerCurrentRaycast.gameObject != null && eventData.pointerCurrentRaycast.gameObject.GetComponent<UIInventorySlot>() != null)
            {
+                int toSlotNumber = eventData.pointerCurrentRaycast.gameObject.GetComponent<UIInventorySlot>().slotNumber;
+                InventoryManager.Instance.SwapInventoryItem(InventoryLocation.player, slotNumber, toSlotNumber);
            }
            else
            {
                if (itemDetails.canBeDropped)
                {
                    DropSelectedItemAtMousePosition();
                }
            }

            Player.Instance.EnablePlayerInput();
        }
    }

}


```

## 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;

    
    public void SwapInventoryItem(InventoryLocation inventoryLocation, int fromItem, int toItem)
    {
        if(fromItem < inventoryLists[(int)inventoryLocation].Count && toItem < inventoryLists[(int)inventoryLocation].Count && fromItem != toItem && fromItem >= 0 && toItem >= 0 )
        {
            InventoryItem fromInventoryItem = inventoryLists[(int)inventoryLocation][fromItem];
            InventoryItem toInventoryItem = inventoryLists[(int)inventoryLocation][toItem];

            inventoryLists[(int)inventoryLocation][fromItem] = toInventoryItem;
            inventoryLists[(int)inventoryLocation][toItem] = fromInventoryItem;
            
            EventHandler.CallInventoryUpdatedEvent(inventoryLocation,inventoryLists[(int)inventoryLocation]);

        }
    }    
}


```




# 参考

## C#

## Unity Editor コンポーネント

## Unity スクリプト

## その他

Section1

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

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

https://github.com/RYA234/LearnFarmRPG/commit/79cf3fd7e7e8cc9b5a7fb7c531f51d5b1ef4f705