概要
今回はInvenotrySlotにカーソルを合わせると説明欄を表示するようにします。
開発環境
IDE:Rider
Unity:2020.3.42(LTS)
OS:Windows10
UnityEditor上の設定
inventoryTextBoxプレハブの作成
InventoryTextBox
TextBoxImage(1)とTextBoxImage(2)を整列して並べるため、ContentSizeFitter Vertical LayoutGroupコンポーネントをアタッチします。
TextBoxImageBox(1) ‥(2)も同様の設定
Imageコンポーネントで背景の画像を追加
Text類を垂直方向に並べるためVerticalLayoutGroupコンポーネントを追加
UIInventorySlotのインスペクターの設定を行う。
実装のポイント
InventorySlotにカーソルを合わせて説明欄を表示させる
UIInventorySlotクラスにOnPointerEnterメソッドを加えます。
OnPointerEnterはオブジェクトがマウスーバーされた時に実行されるメソッドです。
UIInventoryTextBoxクラスを新規作成します。InventoryTextBoxのTextオブジェクトを更新するメソッドSetTextboxTextを加えます。
InventorySlotからカーソルを外して説明欄を消去する
UIInventorySlotクラスにOnPointerExitメソッドを加えます。
OnPointerExitはマウスオーバーされる時に実行されます。
処理内容としてはOnPointerEnterで生成したinventoryTextBoxを削除します。
コード部分
UIInventorySlot
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
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
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
public enum ItemType
{
Seed,
Commodity,
Watering_tool,
Hoeing_tool,
Chopping_tool,
Breaking_tool,
Reaping_tool,
Collecting_tool,
Reapable_scenary,
none,
count,
Furniture,
}
Settings
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
Vetrical Layout Group
Unity スクリプト
OnPointerEnter
OnPointerExit
Transform.SetParent
RectTransform.pivot
その他
Section7 30 Item Description Pop Ups
github コミット分(個人確認用 privateなので見れません)