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

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

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码

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

InventoryItem.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
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
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);
        }
    }

}

 

最近更新

  1. TCP协议是安全的吗?

    2024-03-20 10:16:09       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-20 10:16:09       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-20 10:16:09       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-20 10:16:09       18 阅读

热门阅读

  1. CSS实现点击防抖

    2024-03-20 10:16:09       20 阅读
  2. 原型,模板,策略,适配器模式

    2024-03-20 10:16:09       16 阅读
  3. 设计模式(结构型设计模式——适配器模式)

    2024-03-20 10:16:09       16 阅读
  4. flink启动错误(使用YARN)

    2024-03-20 10:16:09       20 阅读
  5. HttpClient4基础

    2024-03-20 10:16:09       21 阅读
  6. Oracle中使用coe_load_sql_profile脚本固定执行计划

    2024-03-20 10:16:09       22 阅读
  7. Android Studio中快速修改包名

    2024-03-20 10:16:09       22 阅读