Unity3D 游戏开发:制作游戏背包 背包物品选择时高亮显示和动画(28)

728 阅读1分钟

一、脚本编程

1、修改SlotUI.cs文件,实现选中效果

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;

namespace MFarm.Inventory
{
    public class SlotUI : MonoBehaviour,IPointerClickHandler
    {
        [Header("组件的获取")]
        [SerializeField] private Image slotImage;

        [SerializeField] private TextMeshProUGUI amountText;

        public  Image slotHightlight;

        [SerializeField] private Button button;

        [Header("格子类型")]
        public SlotType slotType;

        public bool isSelected;

        public int slotIndex;
        //物品信息
        public ItemDetails itemDetails;

        public int itemAmount; 

        private InventoryUI inventoryUI=>GetComponentInParent<InventoryUI>();

        private void Start()
        {
            isSelected = false;

            if (itemDetails.itemID == 0)
            {
                UpdateEmptySlot();
            }
        }

        //更新格子UI和信息
        public void UpdateSlot(ItemDetails item, int amount)
        {
            itemDetails = item;
            slotImage.sprite = item.itemIcon;
            itemAmount = amount;
            amountText.text = amount.ToString();
            slotImage.enabled = true;
            button.interactable = true;
        }

        //将Slot更新为空
        public void UpdateEmptySlot()
        {
            if (isSelected)
            {
                isSelected = false;
            }
            slotImage.enabled = false;
            amountText.text = string.Empty;
            button.interactable = false;
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (itemAmount == 0) return;
            isSelected=!isSelected;
           
            inventoryUI.UpdateSlotHightlight ( slotIndex );


        }
    }
}

2、修改InventoryUI。cs文件,实现高亮效果

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MFarm.Inventory 
{
    public class InventoryUI : MonoBehaviour
    {
        [Header("玩家背包")]
        [SerializeField] private GameObject bagUI;
        [SerializeField] private SlotUI[] playerSlots;

        private bool bagOpened;
        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; 
            }
            bagOpened = bagUI.activeInHierarchy;
        }


        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.B))
            {
                OpenBagUI();
            }
        }
        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;
            }
        }


        //打开关闭背包UI,Button调用事件
        public void OpenBagUI()
        {
            bagOpened = !bagOpened;

            bagUI.SetActive(bagOpened);
        }


        //更新高亮显示
        public void UpdateSlotHightlight(int index)
        {
            foreach (var slot  in playerSlots)
            {
                if (slot.isSelected && slot.slotIndex == index)
                {
                    slot.slotHightlight.gameObject.SetActive(true);
                }
                else
                {
                    slot.isSelected = false;
                    slot.slotHightlight.gameObject.SetActive(false);
                }
            }
        }
    }
}


二、开发操作

1、为Highlight添加动画 1)、添加Animator控制 image.png 2)、创建Animation动画

image.png

三、脚本逻辑

未完待续......

阅读更多作者文章:

# Unity3D 游戏开发:制作游戏背包 背包显示控制(27)