Unity3D implements backpack system

The Unity2018 version and c# used.
Material link, there are miscellaneous files inside, you need to clean up by yourself
Link: https://pan.baidu.com/s/1o7_RW_QQ1rrAbDzT69ApRw Extraction code: 8s95

Build Ui firstInsert picture description here
Insert picture description here

First create an Item class

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

[CreateAssetMenu(fileName = "New Item",menuName = "Inventory/New Iten")]
public class Item : ScriptableObject {
    
    
    public string itemName;
    public Sprite itemImage;
    public int itemHeld;

    [TextArea]
    public string itemInfo;

    public bool Equp;
}

Create three here
Insert picture description here

Insert picture description here
Create an Inventory class declaration

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

[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/New Inventory")]
public class Inventory : ScriptableObject {
    
    

    public List<Item> itemList = new List<Item>();
}


Insert picture description here
Create the itemOnWord class in the same way as the above two creation methods

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

public class itemOnWord : MonoBehaviour {
    
    

    public Item thisItem;
    public Inventory playerInventory;

    private void OnTriggerEnter2D(Collider2D other)
    {
    
    
        if (other.gameObject.CompareTag("Player"))
        {
    
    
            AddNewItem();
            Destroy(gameObject);
        }
    }

    
    public void AddNewItem()
    {
    
    
        if (!playerInventory.itemList.Contains(thisItem))
        {
    
    
            //playerInventory.itemList.Add(thisItem);
            //InventoryManager.CreateNewItem(thisItem);
            for (int i = 0; i < playerInventory.itemList.Count; i++)
            {
    
    
                if (playerInventory.itemList[i] == null)
                {
    
    
                    playerInventory.itemList[i] = thisItem;
                    break;
                }
            }
        }
        else
        {
    
    
            thisItem.itemHeld += 1;
        }
        InventoryManager.RefreshItem();
    }
}

Drag the code into this, and drag into the material to
Insert picture description here
create a Slot class, you need to reference the namespace using UnityEngine.UI;

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

public class Slot : MonoBehaviour {
    
    

    public int slotId;
    public Item slotItem;
    public Image slotImage;
    public Text slotNum;
    public string slotInfo;

    public GameObject itemInSlot;

    public void ItemOnClicked()
    {
    
    
        InventoryManager.UpdateItemInfo(slotInfo);
    }

    public void SetupSlot(Item item)
    {
    
    
        if (item == null)
        {
    
    
            itemInSlot.SetActive(false);
            return;
        }
        slotImage.sprite = item.itemImage;
        slotNum.text = item.itemHeld.ToString();
        slotInfo = item.itemInfo;
    }

}

Drag in the object and assign it to
Insert picture description here
create the InventoryManager class and reference the namespace using UnityEngine.UI;

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

public class InventoryManager : MonoBehaviour {
    
    

    static InventoryManager instance;

    public Inventory myBag;
    public GameObject slotGrid;
    public GameObject emptySlot;
    public Text itemInfromation;

    public List<GameObject> slots = new List<GameObject>();
    void Awake()
    {
    
    
        if (instance != null)
            Destroy(this);
        instance = this;
    }

    public void OnEnable()
    {
    
    
        RefreshItem();
        instance.itemInfromation.text = "";
    }

    public static void UpdateItemInfo(string itemDescription)
    {
    
    
        instance.itemInfromation.text = itemDescription;
    }

    public static void RefreshItem()
    {
    
    
        for (int i = 0; i < instance.slotGrid.transform.childCount; i++)
        {
    
    
            if (instance.slotGrid.transform.childCount == 0)
                break;
                Destroy(instance.slotGrid.transform.GetChild(i).gameObject);
            instance.slots.Clear();
        }

        for (int i = 0; i < instance.myBag.itemList.Count; i++)
        {
    
    
            instance.slots.Add(Instantiate(instance.emptySlot));
            instance.slots[i].transform.SetParent(instance.slotGrid.transform);
            instance.slots[i].GetComponent<Slot>().slotId = i; 
            instance.slots[i].GetComponent<Slot>().SetupSlot(instance.myBag.itemList[i]);
        }
    }
}

Drag in the object and assign it to
Insert picture description here
create the ItemOnDrag class and reference the name space using UnityEngine.EventSystems;

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

public class ItemOnDrag : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler{
    
    
    public Transform OriginalParent;
    public Inventory myBag;
    private int currentItemId;
    public void OnBeginDrag(PointerEventData eventData)
    {
    
    
        OriginalParent = transform.parent;
        currentItemId = OriginalParent.GetComponent<Slot>().slotId;
        transform.SetParent(transform.parent.parent);
        transform.position = eventData.position;
        GetComponent<CanvasGroup>().blocksRaycasts = false;
    }

    public void OnDrag(PointerEventData eventData)
    {
    
    
        transform.position = eventData.position;
        Debug.Log(eventData.pointerCurrentRaycast.gameObject.name);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
    
    
        if (eventData.pointerCurrentRaycast.gameObject != null)
        {
    
    
        if (eventData.pointerCurrentRaycast.gameObject.name == "Item Image")
        {
    
    
            transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform.parent.parent);
            transform.position = eventData.pointerCurrentRaycast.gameObject.transform.parent.parent.position;

            var temp = myBag.itemList[currentItemId];
            myBag.itemList[currentItemId] = myBag.itemList[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotId];
            myBag.itemList[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotId] = temp;


            eventData.pointerCurrentRaycast.gameObject.transform.parent.position = OriginalParent.position;
            eventData.pointerCurrentRaycast.gameObject.transform.parent.SetParent(OriginalParent);
            GetComponent<CanvasGroup>().blocksRaycasts = true;
            return;
        }

        if (eventData.pointerCurrentRaycast.gameObject.name == "Slot(Clone)")
        {
    
    
        transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform);
        transform.position = eventData.pointerCurrentRaycast.gameObject.transform.position;

        myBag.itemList[eventData.pointerCurrentRaycast.gameObject.GetComponentInParent<Slot>().slotId] = myBag.itemList[currentItemId];
        if (eventData.pointerCurrentRaycast.gameObject.GetComponent<Slot>().slotId != currentItemId)
        myBag.itemList[currentItemId] = null;
        GetComponent<CanvasGroup>().blocksRaycasts = true ;
            return;
        }
        }
     transform.SetParent(OriginalParent);
     transform.position = OriginalParent.position;
     GetComponent<CanvasGroup>().blocksRaycasts = true;
        
    }
}

Drag in objects and assign values
Insert picture description here
Create MoveBag class

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

public class MoveBag : MonoBehaviour,IDragHandler {
    
    

    public Canvas canvas;
    RectTransform currentRect;

    public void OnDrag(PointerEventData eventData)
    {
    
    
        currentRect.anchoredPosition += eventData.delta;
    }

    void Awake()
    {
    
    
        currentRect = GetComponent<RectTransform>();
    }
}

Drag into the object and assign a value
Insert picture description here
Thank you for your support, it is not very good, I hope everyone understands.
thank! ! !

Guess you like

Origin blog.csdn.net/m0_47605113/article/details/109850735