Unity backpack function realization

Backpack

1. Create the backpack panel

insert image description here

The Grid uses the Grid Layout Group component layout, and the child objects will form a grid layout in the Grid

2. Create a database

Bag.cs

Bag database, save all items in the backpack

[CreateAssetMenu(menuName ="Bag/NewBag")]
public class Bag : ScriptableObject
{
    
    
    public List<Item> ItemList = new List<Item>();
}
//CreateAssetMenu可创建一个资源菜单,来新建这个ScriptableObject

Item.cs

Item database field

[CreateAssetMenu(menuName = "Bag/NewItem")]
public class Item : ScriptableObject
{
    
    
    public string ItemName;//物体名
    public int num;//物体数量
    public Sprite ItemImage;//物体图片
    [TextArea]
    public string ItemDescription;//物体描述
}

With this field, each newly created ScriptableObject is a database record
insert image description here

3. Create the world object

ItemOnWorld.cs

Create objects in the world

public class ItemOnWorld : MonoBehaviour
{
    
    
    public Bag Bag;
    public Item ThisItem;
    //如果触碰物体,更新数据库的信息
    private void OnTriggerEnter2D(Collider2D other)
    {
    
    
        if (other.CompareTag("Player"))
        {
    
    
            if (!Bag.ItemList.Contains(ThisItem))//如果这个物体不在bag里
            {
    
    
                //Bag.ItemList.Add(ThisItem);
                //因为ItemList是无序的,所以捡到新物体应在list里面找个空位置
                for(int i = 0; i < Bag.ItemList.Count; i++)
                    if (Bag.ItemList[i] == null)
                    {
    
    
                        Bag.ItemList[i] = ThisItem;
                        break;
                    }
            }
            else //如果bag里已经有这个物体
            {
    
    
                ThisItem.num++;
            }
            Destroy(this.gameObject);
        }
    }
}

insert image description here

This is mounted on the world object, Bag indicates that the world object belongs to the Bag database, and ThisItem indicates that the object belongs to the Sword data item, so the world object carries all the information of the object.

4. Create a backpack instance object

Slot.cs

The script on the backpack instance grid, the script carries the information to create the objects stored in this grid

public class Slot : MonoBehaviour
{
    
    
    public Item ThisItem;
    public Image image;
    public Text num;
    
    //给格子里的物体信息赋值,这个item是BagManager传过来的数据库的item
    public void SetupSlot(Item item)
    {
    
    
        if (item == null)
        {
    
    
            ItemInSlot.SetActive(false);
            return;
        }
        this.ThisItem = item;
        this.image.sprite = item.ItemImage;
        this.num.text = item.num.ToString();
    }
    
    public void ClickDescription()
    {
    
    
        BagManager.RefreshDescription(ThisItem.ItemDescription);
    }
}

insert image description here insert image description here

To create this instance object, you need:

  1. Set the sprite in the ItemImage under the object

  2. Set the Num under the object to display the number of objects

Therefore, the Image in the Slot script is the Image of the object in the grid, and the image can be assigned a value through code later;

Num is the Num under the object in the grid, and also assigns a value to the Text object through code

5. Create a backpack management class

BagManager

The backpack management class is mainly used to create instance objects and display some backpack information (in short, it is the operation of the backpack itself)

public class BagManager : MonoBehaviour
{
    
    
    //单例模式
    static BagManager instance;

    public GameObject Grid;
    public GameObject Slot;
    public Bag Bag;
    public Text Description;
    public List<GameObject> Slots = new List<GameObject>();
    private void Awake()
    {
    
    
        if (instance != null)
            Destroy(this);
        instance = this;
    }
    
    //每次打开背包,读取数据库信息,更新背包物体
    private void OnEnable()
    {
    
    
        RefreshItems();
        instance.Description.text = "";
    }
    private void RefreshItems()
    {
    
    
        //把GameObject列表Slots清空
        instance.Slots.Clear();
        //每次更新背包,先把背包的物体销毁,再从数据库读取信息创建物体
        for(int i = 0; i < Grid.transform.childCount; i++)
        {
    
    
            Destroy(Grid.transform.GetChild(i).gameObject);
        }
        //读取数据库,创建子物体
        for(int i = 0; i < instance.Bag.ItemList.Count; i++)
        {
    
    
            //CreateNewItem(Bag.ItemList[i]);
            //创建18个空格子(设置Bag.ItemList.Count为18)
            instance.Slots.Add(Instantiate(instance.Slot));
            instance.Slots[i].transform.SetParent(instance.Grid.transform);

            //读取数据库,更新格子里面的物品信息
            instance.Slots[i].GetComponent<Slot>().SetupSlot(instance.Bag.ItemList[i]);
        }
    }
    
    //更新背包的物体描述
    public static void RefreshDescription(string Description)
    {
    
    
        instance.Description.text = Description;
    }
}

(1) Open the backpack every time, read the database information, and update the backpack object

Enable backpack OnEnable() : OnEnable(), call RefreshItems()

Update the backpack according to the database information RefreshItems() : first delete all the objects in the backpack, then create 18 empty spaces, and then create objects according to the database information, so that all objects can be updated, including newly added objects and the number of objects themselves

(2) Click on the slot to display the description

The sub-object item of the slot itself is a button, and a click event can be added
insert image description here

The Item script is stored in the Slot script, and the description of the object can be obtained. Since the description information is displayed in the bag, the method of displaying the description should be written in the BagManager script.

Slot.cs

public Item ThisItem;
public void ClickDescription()
{
    
    
    //调用BagManager里面的方法,把描述信息传递过去
    BagManager.RefreshDescription(ThisItem.ItemDescription);
}

BagManager.cs

public Text Description;
private void OnEnable()
{
    
    
    RefreshItems();
    instance.Description.text = "";//打开背包,背包物体描述清空
}
public static void RefreshDescription(string Description)
{
    
    
    instance.Description.text = Description;//修改背包物体描述
}

insert image description here insert image description here

The Text Description of this BagManager is the sub-object Description under the Bag

Guess you like

Origin blog.csdn.net/miumiumiu00/article/details/127225924