0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

FarmRPG Unity Inventory&Item Part10 InvenotrySlotにカーソルを合わせると説明欄を表示する

Posted at

概要

今回はInvenotrySlotにカーソルを合わせると説明欄を表示するようにします。

0605.gif

開発環境

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

UnityEditor上の設定

inventoryTextBoxプレハブの作成

image.png

InventoryTextBox

TextBoxImage(1)とTextBoxImage(2)を整列して並べるため、ContentSizeFitter Vertical LayoutGroupコンポーネントをアタッチします。

image.png

TextBoxImageBox(1) ‥(2)も同様の設定

Imageコンポーネントで背景の画像を追加
Text類を垂直方向に並べるためVerticalLayoutGroupコンポーネントを追加

image.png

UIInventorySlotのインスペクターの設定を行う。

image.png

実装のポイント

InventorySlotにカーソルを合わせて説明欄を表示させる

UIInventorySlotクラスにOnPointerEnterメソッドを加えます。
OnPointerEnterはオブジェクトがマウスーバーされた時に実行されるメソッドです。

UIInventoryTextBoxクラスを新規作成します。InventoryTextBoxのTextオブジェクトを更新するメソッドSetTextboxTextを加えます。

image.png

InventorySlotからカーソルを外して説明欄を消去する

UIInventorySlotクラスにOnPointerExitメソッドを加えます。
OnPointerExitはマウスオーバーされる時に実行されます。
処理内容としてはOnPointerEnterで生成したinventoryTextBoxを削除します。

image.png

コード部分

UIInventorySlot

UIInventorySlot.cs
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UIInventorySlot : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler,IPointerEnterHandler,IPointerExitHandler
{
    private Camera mainCamera;
    private Canvas parentCanvas;
    private GameObject draggedItem;
    private Transform parentItem;
    
    public Image inventorySlotHighlight;
    public Image inventorySlotImage;
    public TextMeshProUGUI textMeshProUGUI;
    [SerializeField] private UIInventoryBar inventoryBar = null;
    [SerializeField] private GameObject inventoryTextBoxPrefab = null;
    public ItemDetails itemDetails;
    [SerializeField] private GameObject itemPrefab;
    public int itemQuantity;
    [SerializeField] private int slotNumber = 0;

    private void Awake()
    {
        parentCanvas = GetComponentInParent<Canvas>();
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        if (itemQuantity != 0)
        {
            inventoryBar.inventoryTextBoxGameObject = Instantiate(inventoryTextBoxPrefab, transform.position, Quaternion.identity);
            inventoryBar.inventoryTextBoxGameObject.transform.SetParent(parentCanvas.transform,false);

            UIInventoryTextBox inventoryTextBox = inventoryBar.inventoryTextBoxGameObject.GetComponent<UIInventoryTextBox>();
            
            string itemTypeDescription = InventoryManager.Instance.GetItemTypeDescription(itemDetails.itemType);
            
            inventoryTextBox.SetTextboxText(itemDetails.itemDescription, itemTypeDescription, "", itemDetails.itemLongDescription,"","");
            
            inventoryBar.inventoryTextBoxGameObject.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0f);
            inventoryBar.inventoryTextBoxGameObject.transform.position = new Vector3(transform.position.x, transform.position.y + 50f, transform.position.z);
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        DestroyInventoryTextBox();
    }

    public void DestroyInventoryTextBox()
    {
        if (inventoryBar.inventoryTextBoxGameObject != null)
        {
            Destroy(inventoryBar.inventoryTextBoxGameObject);
        }
    }
    
    }


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;


    public string GetItemTypeDescription(ItemType itemType)
    {
        string itemTypeDescription;
        switch (itemType)
        {
            case ItemType.Breaking_tool:
                itemTypeDescription = Settings.BreakingTool;
                break;
            
            case ItemType.Chopping_tool:
                itemTypeDescription = Settings.ChoppingTool;
                break;
            
            case ItemType.Hoeing_tool:
                itemTypeDescription = Settings.HoeingTool;
                break;
            
            case ItemType.Reaping_tool:
                itemTypeDescription = Settings.ReapingTool;
                break;
            
            case ItemType.Watering_tool:
                itemTypeDescription = Settings.WateringTool;
                break;

            case ItemType.Collecting_tool:
                itemTypeDescription = Settings.CollectingTool;
                break;
            
            default:
                itemTypeDescription = itemType.ToString();
                break;
        }

        return itemTypeDescription;
    }   
}

UIInventoryTextBox

UIInventoryTextBox.cs
using TMPro;
using UnityEngine;
using UnityEngine.UI;


public class UIInventoryTextBox : MonoBehaviour
{
    [SerializeField] private TextMeshProUGUI textMeshTop1 = null;
    [SerializeField] private TextMeshProUGUI textMeshTop2 = null;
    [SerializeField] private TextMeshProUGUI textMeshTop3 = null;
    
    [SerializeField] private TextMeshProUGUI textMeshBottom1 = null;
    [SerializeField] private TextMeshProUGUI textMeshBottom2 = null;
    [SerializeField] private TextMeshProUGUI textMeshBottom3 = null;


    public void SetTextboxText(string textTop1, string textTop2, string textTop3, string textBottom1,
        string textBottom2, string textBottom3)
    {
        textMeshTop1.text = textTop1;
        textMeshTop2.text = textTop2;
        textMeshTop3.text = textTop3;
        textMeshBottom1.text = textBottom1;
        textMeshBottom2.text = textBottom2;
        textMeshBottom3.text = textBottom3;


    }
}


Enum

Enum.cs
public enum ItemType
{
    Seed,
    Commodity,
    Watering_tool,
    Hoeing_tool,
    Chopping_tool,
    Breaking_tool,
    Reaping_tool,
    Collecting_tool,
    Reapable_scenary,
    none,
    count,
    Furniture,
}

Settings

Settings.cs

using UnityEngine;

public static class Settings 
{
    public const string HoeingTool = "Hoe";
    public const string ChoppingTool = "Axe";
    public const string BreakingTool = "Pickaxe";
    public const string ReapingTool = "Scythe";
    public const string WateringTool = "Watering Can";
    public const string CollectingTool = "Basket";
}


参考

C#

Unity Editor コンポーネント

Content Size Fitter

image.png

Vetrical Layout Group

image.png

Unity スクリプト

OnPointerEnter

image.png

OnPointerExit

image.png

Transform.SetParent

image.png

RectTransform.pivot

image.png

その他

Section7 30 Item Description Pop Ups

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?