FarmRPG　Unity Inventory&Item Part8 2/2 ドラッグ終了時の処理を追加

# 概要
今回はインベントリバーのアイテムをドラッグを完了したときの処理を加えます。
以下gifアニメは実装後の様子です。

![0603.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/e82b89b0-577c-eec9-bf80-13e74ff20648.gif)


# 開発環境

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

# UnityEditor上の設定
無し


# 実装のポイント
InventorySlotクラスに新規メソッドOnEndDragとDropSelectedAtMousePositionを加えます。
OnEndDragはマウスドラッグ終了時に実行されます。

DropSelectedAtMousePosition以下３つの処理を行います。
1.draggedItemオブジェクトを削除
2.inventoryBarから該当するアイテムを削除
3.新規アイテムをItemオブジェクト以下に加える

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/b90250d3-d080-0302-30ee-050a8bccdb86.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;

    private void DropSelectedItemAtMousePosition()
    {
        if (itemDetails != null)
        {
            Vector3 worldPosition = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,
                Input.mousePosition.y, -mainCamera.transform.position.z));
            GameObject itemGameObject = Instantiate(itemPrefab, worldPosition, Quaternion.identity, parentItem);
            Item item = itemGameObject.GetComponent<Item>();
            item.ItemCode = itemDetails.itemCode;     
            InventoryManager.Instance.RemoveItem(InventoryLocation.player, item.ItemCode);    
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (draggedItem != null)
        {
            Destroy(draggedItem);
            if (eventData.pointerCurrentRaycast.gameObject != null &&
                eventData.pointerCurrentRaycast.gameObject.GetComponent<UIInventorySlot>() != null)
            {        
            }
            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>
{
    public int[] inventoryListCapacityIntArray;
    public List<InventoryItem>[] inventoryLists;
    public void RemoveItem(InventoryLocation inventoryLocation, int itemCode)
    {
        List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];

        int itemPosition = FindItemInInventory(inventoryLocation, itemCode);

        if (itemPosition != -1)
        {
            RemoveItemAtPosition(inventoryList, itemCode, itemPosition);
        }
        EventHandler.CallInventoryUpdatedEvent(inventoryLocation, inventoryLists[(int)inventoryLocation]);
    }   
}

```

## Player

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

public class Player : SingletonMonobehaviour<Player>
{
    private bool _playerInputIsDisabled = false;
    public bool PlayerInputIsDisabled { get => _playerInputIsDisabled; set => _playerInputIsDisabled = value; }


    private void Update()
    {
        if (!PlayerInputIsDisabled)
        {
            ResetAnimationTriggers();

            PlayerMovementInput();
            PlayerWalkInput();

            EventHandler.CallMovementEvent(xInput, yInput, isWalking, isRunning, isIdle, isCarrying, toolEffect,
                isUsingToolRight, isUsingToolLeft, isUsingToolUp, isUsingToolDown,
                isLiftingToolRight, isLiftingToolLeft, isLiftingToolUp, isLiftingToolDown,
                isPickingRight, isPickingLeft, isPickingUp, isPickingDown,
                isSwingingToolRight, isSwingingToolLeft, isSwingingToolUp, isSwingingToolDown,
                false, false, false, false);
        }
    }
    public void EnablePlayerInput()
    {
        PlayerInputIsDisabled = false;
    }
}

```





# 参考

## C#

## Unity Editor コンポーネント

## Unity スクリプト

### eventData.pointerCurrentRaycast.gameObject
https://docs.unity3d.com/2018.3/Documentation/ScriptReference/EventSystems.RaycastResult-gameObject.html


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/95d82a70-47e2-f8f0-2c21-1d5892f99edc.png)

### OnEndDrag

https://docs.unity3d.com/ja/2018.4/ScriptReference/UI.ScrollRect.OnEndDrag.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/2e21b154-49cb-b792-70a0-c456c1e17dac.png)


### mainCamera.ScreenToWorldPoint

https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/57c28e2a-659b-70b5-223f-293d86e533ea.png)


## その他

Section 28 Drop Items From Inventory

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


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

https://github.com/RYA234/LearnFarmRPG/commit/9b6e504bc92dec6c6c9e2ddd5cf8a0577f30ce8c