unity:背包系统讲解

这篇博客详细讲解了如何在Unity中实现一个背包系统,包括UI_PackCell.cs脚本的功能解析,如何处理Json文件及读取方法,以及ItemsInfo.cs中物品信息的管理。还特别提到了DrugData.json文件,它是用于存储游戏内道具数据的结构化文件。
摘要由CSDN通过智能技术生成

图片描述
在这里插入图片描述

		特别说明:UI框架的作用只是为了方便管理,和使得整个结构清晰明了。窗体封装了打开与关闭的公有方法,并无涉及到窗体具体的特有功能,所以这里不讲框架并不会造成做不出背包的问题。

		UI_PacksackForm.cs(脚本说明)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyUI;
using System;
using UnityEngine.EventSystems;

public class UI_PacksackForm : BaseUIForm,UnityEngine.EventSystems.IBeginDragHandler,
    UnityEngine.EventSystems.IDragHandler, UnityEngine.EventSystems.IEndDragHandler{
   



    /*属性字段*/
    public List<UI_PackCell> DrugCells;              //这个用来存储药品类所有小格子的列表
    public List<UI_PackCell> EquipCells;
    public List<UI_PackCell> SkillCells;
    public List<UI_PackCell> TaskCells;
    private Vector3 _offsetPointerToPack;            //这个鼠标指针点击到背包窗体的中心点的偏移
    private UnityEngine.UI.Button _Btn_ClearUp;      //这个是整理按钮
    private UnityEngine.UI.Button _Btn_Close;        //关闭按钮
    private UnityEngine.UI.ScrollRect _ScrollView;   //这个是ScrollView
    private UnityEngine.UI.Dropdown _DpDn_BackSelect;//背包类型的下拉选择菜单
    public RectTransform DrugContent;                //这是4个不同种类的背包
    public RectTransform EquipContent;
    public RectTransform SkillContent;
    public RectTransform TaskContent;

    private ItemsType _itemsType = ItemsType.Drug;   //背包当前选择的物品种类

    #region 拖拽
    ///开始拖拽
    void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
    {
   
        Debug.Log("开始拖拽背包");
        Vector3 worldPosition;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent<RectTransform>(),
            eventData.position, eventData.pressEventCamera, out worldPosition))
        {
   
            _offsetPointerToPack = worldPosition - transform.GetComponent<RectTransform>().position;
        }

    }
    ///拖拽中
    void IDragHandler.OnDrag(PointerEventData eventData)
    {
   
        Vector3 worldPosition;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent<RectTransform>(),
            eventData.position, eventData.pressEventCamera, out worldPosition))
        {
   
            transform.GetComponent<RectTransform>().position = worldPosition - _offsetPointerToPack;
        }
    }
    ///拖拽结束
    void IEndDragHandler.OnEndDrag(PointerEventData eventData)
    {
   
        Debug.Log("结束背包拖拽");
    }
    #endregion


    /*公有方法*/
    /// <summary>
    /// 用以注册下拉选项的事件
    /// </summary>
    /// <param name="num"></param>
    public void onValueChange(Int32 num)
    {
   
        switch(num)
        {
   
            case 0:
                {
   
                    DrugContent.gameObject.SetActive(true);
                    EquipContent.gameObject.SetActive(false);
                    SkillContent.gameObject.SetActive(false);
                    TaskContent.gameObject.SetActive(false);
                    _ScrollView.content = DrugContent;
                    _itemsType = ItemsType.Drug;
                }
                break;
            case 1:
                {
   
                    DrugContent.gameObject.SetActive(false);
                    EquipContent.gameObject.SetActive(true);
                    SkillContent.gameObject.SetActive(false);
                    TaskContent.gameObject.SetActive(false);
                    _ScrollView.content = EquipContent;
                    _itemsType = ItemsType.Equip;
                }
                break;
            case 2:
                {
   
                    DrugContent.gameObject.SetActive(false);
                    EquipContent.gameObject.SetActive(false);
                    SkillContent.gameObject.SetActive(true);
                    TaskContent.gameObject.SetActive(false);
                    _ScrollView.content = SkillContent;
                    _itemsType = ItemsType.Skil;
                }
                break;
            case 3:
                {
   
                    DrugContent.gameObject.SetActive(false);
                    EquipContent.gameObject.SetActive(false);
                    SkillContent.gameObject.SetActive(false);
                    TaskContent.gameObject.SetActive(true);
                    _ScrollView.content = TaskContent;
                    _itemsType = ItemsType.Other;
                }
                break;
        }

    }
    /// <summary>
    /// 用以注册整理按钮的事件
    /// </summary>
    public void CleanUp()
    {
   
        List<UI_PackCell> temp;
        switch (_itemsType)
        {
   
            case ItemsType.Drug:
                {
   
                    temp = DrugCells;
                }
                break;
            case ItemsType.Equip:
                {
   
                    temp = EquipCells;
                }
                break;
            case ItemsType.Skil:
                {
   
                    temp = SkillCells;
                }
                break;
            case ItemsType.Other:
                {
   
                    temp = TaskCells;
                }
                break;
            default:
                temp = new List<UI_PackCell>();
                break;
        }
        if (temp.Count > 0)
            for (int i = 0; i < temp.Count; ++i)
            {
   
                for(int j = i+1; j < temp.Count; ++j)
                {
   
                    if(temp[i].getGoodsId()<temp[j].getGoodsId()&& temp[j].getGoodsId()>0)
                    {
   
                        temp[i].Exchange(temp[j]);
                    }
                }
            }

    }

    // Use this for initialization
    void Awake () {
   
        _DpDn_BackSelect = transform.FindChild("DpDn_PackSelect").GetComponent<UnityEngine.UI.Dropdown>();
        _ScrollView = transform.FindChild("ScrollView").GetComponent<UnityEngine.UI.ScrollRect>(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值