unity背包系统完整版

2 篇文章 0 订阅

背包系统分三个层次   背包整体——背包网格——背包物品,其中物品是用代码实例化出来的。

 

目录

背包类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Bag : MonoBehaviour
{
    public static Bag _instance;
    private int coinCount = 1000;//当前背包内有多少的金钱
    public Text coin;//用文本来显示金钱数字
    public List<BagItemGird> ItemGirdList = new List<BagItemGird>();
    //这是背包网格列表,表示这个背包可以存放多少不同的物品

    public BagItem bagItem;//新建一个背包物品类
    private void Awake()
    {
        _instance = this;
    }
    // Start is called before the first frame update
    void Start()
    {
        coin.text = coinCount.ToString();//显示金币数    
    }
    public void GetId(int id)//当有物品要放入背包的时候得到他的id并且显示
    {
        BagItemGird grid = null;//grid用来标记哪个网格可以存放东西
        foreach (BagItemGird temp in ItemGirdList)
        {//对背包网格进行循环找到可以存放物品的网格
            //如果背包要放入的物品的id和背包内某个网格的id一样(背包网格类具有id属性,看下文)那么就把这个物品的数量加一
            if (temp.id == id)
            {
                grid=temp;
                break;
            }
        }
        if (grid != null)//如果存在这么一个网格,物品数量加一
        {
            BagItem item=grid.transform.GetComponentInChildren<BagItem>();
            item.PlusNubmer();//这是背包物品类的一个方法 用来增加物品数量
        }
        else
        {
            //如果该物品在背包内没有相似的,查找一个空的网格放入
            foreach (BagItemGird temp in ItemGirdList)
            {
                if (temp.id == 0)//网格的id为0就表示该网格为空
                {
                    grid = temp;
                    break;
                }
            }
            if (grid != null)//如果有这么一个网格 他的id=0就把物品放入该网格
            {
                bagItem = (BagItem)Instantiate(bagItem, Vector3.zero, transform.rotation);//实例化了一个背包物品
                bagItem.transform.parent = grid.transform;//将背包物品变成网格的子对象
                bagItem.transform.localPosition = Vector3.zero;//设置背包物品的位置为0
                grid.id = id;//把物品id赋值给网格id表示该网格有物品
                bagItem.SetId(id);//这是对物品进行初始化,详情看下文的BagItem类说明
            }
        }

    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.X))
        {
            GetId(Random.Range(1001, 1004));
        }
    }
}

网格类

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

public class BagItemGird : MonoBehaviour
{
    public int id = 0;
    private bool isItem = false;
    // Start is called before the first frame update
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

物品类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class BagItem :MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    public int id = 0;//表明物品id
    private int num = 0;//物品数量
    private Text itemnum;//显示物品数量的文本
    private ObjectInfo info = null;//物品信息
    private BagItemGird parent;//该背包物品的网格
    private Image image;//背包物品显示的图片
    Vector3 CIPosition;//要交换位置的物品位置
    // Start is called before the first frame update
    void Awake()
    {
        image = this.GetComponent<Image>() as Image;
        itemnum = this.GetComponentInChildren<Text>();
    }
    void Start()
    {
    }
    public void SetId(int id, int num = 1)
    {

        this.id = id;//把id赋值给该类的id
        info = ObjectsInfo._instance.GetObjectInfoById(id);//获得该id的信息
        this.SetIconName(info.icon_name);//这个函数用来获得物品名称
        SetImg();//该函数用来设置图片信息
        itemnum.gameObject.SetActive(true);//设置为可见
        this.num = num;
        itemnum.text = num.ToString();//显示数量
    }
    public void SetImg()
    {
        string path = "RPG/GUI/Icon/" + info.icon_name;//设置图片的路径 必须要放在resources里面才可以
        image.sprite = Resources.Load(path, typeof(Sprite)) as Sprite;//将该路径的图片导入到image中
    }
    public void PlusNubmer(int num = 1)
    {
        //增加物品数量
        this.num += 1;
        itemnum.text = this.num.ToString();
    }
    public void ClearInfo()
    {
        //清除物品
        id = 0;
        info = null;
        num = 0;
        GameObject.Destroy(this,0);
    }
    // Update is called once per frame
    void Update()
    {

    }

    public void SetIconName(string icon_name)
    {
        image.name = icon_name;
    }
    //拖拽功能的实现
    public void OnBeginDrag(PointerEventData eventData)
    {
        CIPosition = eventData.position;
        transform.position = eventData.position;
    }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = eventData.position;
        transform.GetComponent<Image>().raycastTarget = false;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        GameObject go = eventData.pointerCurrentRaycast.gameObject;
        Transform Cparent;
        if (go.tag == TagManage.bagItem && go.transform.parent != transform.parent)
        {
            Cparent = transform.parent;
            transform.parent = go.transform.parent;
            go.transform.parent = Cparent;
            transform.localPosition = Vector3.zero;
            go.transform.localPosition = Vector3.zero;
        }
        else if (go.tag == TagManage.bagItemGird)
        {
            transform.parent = go.transform;
            transform.localPosition = Vector3.zero;
        }
        else
        {
            transform.position = CIPosition;
        }
        transform.GetComponent<Image>().raycastTarget = true;
    }

}

Unity背包系统是一个游戏中常用的组件,用于管理玩家在游戏中所获得的道具。一个完整的背包系统通常由数据、逻辑和UI三部分组成。在设计过程中,我们首先进行UI设计,包括背包的标题、关闭键和背包内的格子容器。 背包系统的数据部分主要负责管理物品的信息,例如物品的名称、图片、数量等。逻辑部分负责实现将物品放置进背包、对背包物品进行管理以及使用背包物品等功能。实际上,当我们获取或移动物品时,我们会直接修改背包数据库中的物品信息。修改完成后,我们通过背包中的UI元素来获取背包数据库中相应索引的物品信息,并将物品栏中的UI信息更新为数据库中对应序号的物品的图片和数量。 通过将背包系统分解为数据、逻辑和UI三部分,我们可以有效地管理背包系统的复杂逻辑关系,并使代码更加清晰和易于维护。这种设计模式有助于提高游戏开发的效率和代码质量。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Unity游戏开发:背包系统的实现](https://blog.csdn.net/float_freedom/article/details/126243888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Unity3D RPG实现 2 —— 背包系统](https://blog.csdn.net/weixin_43757333/article/details/123187025)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值