Unity类银河恶魔城学习记录11-2 p104 Inventoty源代码

此章节相对较难理解,有时间单独出一章讲一下

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

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

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

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

[Serializable]
public class InventoryItem
{
    public ItemData data;//保存实打实的Item数据
    public int stackSize;//记录相同Item的数量
    public InventoryItem(ItemData _newItemData)//创建时就传入要保存的Item
    {
        data = _newItemData;
        AddStack();//由初始时由于没有相同类型的物体,为了使刚开始初始化便拥有值,此处必须调用一次此函数
    }

    public void AddStack() => stackSize++;
    public void RemoveStack() => stackSize--;
}
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的字典

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

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


    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);//同上
        }
    }

    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();
        }
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.L))
        {
            ItemData newItem = inventoryItems[inventoryItems.Count - 1].data;

            RemoveItem(newItem);
        }
    }
}
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 Start()
    {
        sr = GetComponent<SpriteRenderer>();

        sr.sprite = ItemData.icon;
    }

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

}
相关推荐
sakabu1 小时前
ESP32 外设驱动开发指南 (ESP-IDF框架)——GPIO篇:基础配置、外部中断与PWM(LEDC模块)应用
笔记·单片机·学习·esp32
代码哈士奇1 小时前
VitePress学习笔记
javascript·笔记·学习
小眼睛FPGA1 小时前
【盘古100Pro+开发板实验例程】FPGA学习 | 基于紫光 FPGA 的键控 LED 流水灯
科技·学习·ai·fpga开发·fpga
天才少女爱迪生1 小时前
pytorch的自定义 CUDA 扩展怎么学习
人工智能·pytorch·学习
不可描述的两脚兽2 小时前
学习笔记《区块链技术与应用》第4天 比特币脚本语言
笔记·学习·区块链
明长歌4 小时前
【javascript】Reflect学习笔记
javascript·笔记·学习
超浪的晨4 小时前
Maven 与单元测试:JavaWeb 项目质量保障的基石
java·开发语言·学习·单元测试·maven·个人开发
MingYue_SSS6 小时前
【未解决】STM32无刷电机驱动电路问题记录
笔记·嵌入式硬件·学习
野原鑫之祝6 小时前
嵌入式开发学习———Linux环境下IO进程线程学习(一)
linux·c语言·学习·vim·嵌入式
embrace996 小时前
【C语言学习】scanf函数
c语言·开发语言·汇编·学习·青少年编程·c#·编辑器