Unity类银河恶魔城学习记录11-3 p105 Inventory UI源代码

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考

此代码仅为较上一P有所改变的代码

【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili

UI_itemSlot.cs
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class UI_itemSlot : MonoBehaviour
{
    [SerializeField] private Image itemImage;
    [SerializeField] private TextMeshProUGUI itemText;

    public InventoryItem item;

    public void UpdateSlots(InventoryItem _newItem)
    {
        item = _newItem;

        itemImage.color = Color.white;

        if (item != null)
        {
            itemImage.sprite = item.data.icon;

            if (item.stackSize > 1)
            {
                itemText.text = item.stackSize.ToString();

            }
            else
            {
                itemText.text = "";
            }
        }
    }

    
}
Inventory.cs
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public static Inventory instance;

    public List<InventoryItem> inventoryItems;//inventoryItems类型的列表
    public Dictionary<ItemData, InventoryItem> inventoryDictianory;//以ItemData为Key寻找InventoryItem的字典

    [Header("Inventory UI")]

    [SerializeField] private Transform inventorySlotParent;
    private UI_itemSlot[] itemSlot;//UI Slot的数组


    private void Awake()
    {
        if (instance == null)
            instance = this;
        else
            Destroy(gameObject);
        //防止多次创建Inventory
    }

    public void Start()
    {
        inventoryItems = new List<InventoryItem>();
        inventoryDictianory = new Dictionary<ItemData, InventoryItem>();

        itemSlot = inventorySlotParent.GetComponentsInChildren<UI_itemSlot>();//拿到的方式有点绕,显示拿到Canvas 里的 Inventory 然后通过GetComponentsInChildren拿到其下的使用UISlot
    }


    private void UpdateSlotUI()
    {
        for(int i = 0;i < inventoryItems.Count;i++ )
        {
            itemSlot[i].UpdateSlots(inventoryItems[i]);
        }
    }

    public void AddItem(ItemData _item)//将物体存入Inventory的函数
    {
        if(inventoryDictianory.TryGetValue(_item,out InventoryItem value))
        {
            value.AddStack();
        }//字典的使用,通过ItemData类型的数据找到InventoryItem里的与之对应的同样类型的数据
        else//初始时由于没有相同类型的物体,故调用else是为了初始化库存,使其中含有一个基本的值
        {
            InventoryItem newItem = new InventoryItem(_item);
            inventoryItems.Add(newItem);//填进列表里只有一次
            inventoryDictianory.Add(_item, newItem);//同上
        }

        UpdateSlotUI();
    }

    public void RemoveItem(ItemData _item)//将物体剔除Inventory的函数
    {
        if(inventoryDictianory.TryGetValue(_item,out InventoryItem value))
        {
            if (value.stackSize <= 1)
            {
                inventoryItems.Remove(value);
                inventoryDictianory.Remove(_item);

            }
            else
                value.RemoveStack();
        }

        UpdateSlotUI();
    }

   
}
ItemObject.cs
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemObject : MonoBehaviour
{
    private SpriteRenderer sr;

    [SerializeField] private ItemData ItemData;

    private void OnValidate()
        //https://blog.csdn.net/paserity/article/details/130014259
        //大抵就是在Unity加载脚本或检查器中的值更改时调用。实时更新资产文件,比如材质、shader
    {
        GetComponent<SpriteRenderer>().sprite = ItemData.icon;
        gameObject.name = ItemData.name;
    }
    //private void Start()
    //{
    //    sr = GetComponent<SpriteRenderer>();

    //    sr.sprite = ItemData.icon;
    //}

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.GetComponent<Player>()!= null)
        {
            Inventory.instance.AddItem(ItemData);
            Destroy(gameObject);
        }
    }

}
相关推荐
CSDN_PBB3 小时前
[STM32 - 野火] - - - 固件库学习笔记 - - - 十五.设置FLASH的读写保护及解除
笔记·stm32·学习
鸡啄米的时光机6 小时前
vscode的一些实用操作
vscode·学习
Kai HVZ7 小时前
《深度学习》——调整学习率和保存使用最优模型
人工智能·深度学习·学习
守护者1708 小时前
JAVA学习-练习试用Java实现“使用Apache Ignite对大数据进行内存计算和快速筛查”
java·学习
浅陌sss8 小时前
Xlua中C#引用Lua变量,导致Lua侧的GC无法回收的原因及解决方法
c#·lua
棉晗榜9 小时前
c#模拟鼠标点击左键
c#
weixin_502539859 小时前
rust学习笔记2-rust的包管理工具Cargo使用
笔记·学习·rust
爱吃香蕉的阿豪10 小时前
在c#中虚方法和抽象类的区别
深度学习·c#·.netcore
晚秋大魔王12 小时前
C# 添加图标
c#·visual studio code
web_1553427465612 小时前
【合集】Java进阶——Java深入学习的笔记汇总 & 再论面向对象、数据结构和算法、JVM底层、多线程、类加载、
java·笔记·学习