一、脚本编程
1、创建InventoryUI.cs文件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MFarm.Inventory
{
public class InventoryUI : MonoBehaviour
{
[SerializeField] private SlotUI[] playerSlots;
private void OnEnable()
{
EventHandler.UpdateInventoryUI += OnUpdateInventoryUI;
}
private void OnDisable()
{
EventHandler.UpdateInventoryUI -= OnUpdateInventoryUI;
}
private void Start()
{
for (int i = 0; i < playerSlots.Length; i++)
{
playerSlots[i].slotIndex = i;
}
}
private void OnUpdateInventoryUI(InventoryLocation location, List<InventoryItem> list)
{
switch (location)
{
case InventoryLocation.Player:
for (int i = 0; i < playerSlots.Length; i++)
{
if (list[i].itemAmount > 0)
{
var item = InventoryManager.Instance.GetItemDetails(list[i].itemID);
playerSlots[i].UpdateSlot(item, list[i].itemAmount);
}
else
{
playerSlots[i].UpdateEmptySlot();
}
}
break;
}
}
}
}
2、修改Inventorymanager
EventHandler.CallUpdateInventoryUI(InventoryLocation.Player, playerBag.itemList);
3、创建EventHandler.cs文件
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements.Experimental;
public static class EventHandler
{
public static event Action<InventoryLocation, List<InventoryItem>> UpdateInventoryUI;
public static void CallUpdateInventoryUI(InventoryLocation location, List<InventoryItem> list)
{
UpdateInventoryUI?.Invoke(location, list);
}
}
4、不要忘记slotImage打开
slotImage.enabled = true;
二、开发操作
1、将物品栏和背包里的UI格子赋予Player Slots
2、将PlayerBag设置为26
三、脚本逻辑
未完待续......