基于Unity3D(UGUI)的背包系统<三>

这篇博客主要介绍了如何在Unity3D中利用UGUI实现背包系统的UI逻辑处理,包括配方类的设计以及五个核心面板(背包、箱子、角色、商店、锻造)的实现。每个面板基于Inventory基类进行扩展,利用单例模式确保组件的唯一性。
摘要由CSDN通过智能技术生成

这一篇,主要讲解UI逻辑处理方面,以及UI和该篇中的数据两者之间的使用。

首先补充一个上篇中缺少的一个配方类:这个类主要是用来保存锻造的所需的材料及锻造出来的结果。

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

/// <summary>
/// 配方类:用来保存锻造的所需的材料及锻造出来的结果
/// </summary>
public class Formula{

    public int Item1ID { get; private  set; }//用来锻造的物品1的ID
    public int Item1Amount { get; private set; }//用来锻造的物品1的数量
    public int Item2ID { get; private set; }//用来锻造的物品2的ID
    public int Item2Amount { get; private set; }//用来锻造的物品2的数量

    public int ResID { get; set; }//保存锻造结果的ID

    private List<int> needIDList = new List<int>();//保存锻造所需要材料的ID(相当于锻造的配方)
    public List<int> NeedIDList { get { return needIDList; } }

    public Formula(int item1ID, int item1Amount, int item2ID, int item2Amount, int resID )
    {
        this.Item1ID = item1ID;
        this.Item1Amount = item1Amount;
        this.Item2ID = item2ID;
        this.Item2Amount = item2Amount;
        this.ResID = resID;

        //初始化配方ID
        for (int i = 0; i < Item1Amount; i++)
        {
            needIDList.Add(Item1ID);
        }
        for (int i = 0; i < Item2Amount; i++)
        {
            needIDList.Add(Item2ID);
        }
    }

    /// <summary>
    /// 匹配所提供的物品是否 包含 锻造所需要的物品(利用ID匹配)
    /// </summary>
    /// <returns></returns>
    public bool Match( List<int> idList ) 
    {
        List<int> tempIDList = new List<int>(idList);//临时保存函数参数数据(所拥有的物品ID),防止下面Remove代码移除时被修改
        foreach (int id in needIDList)
        {
            bool isSuccess = tempIDList.Remove(id);
            if (isSuccess == false)
            {
                return false;//拥有的物品和锻造所需的物品匹配失败
            }
        }
        return true;//物品匹配成功
    }

}

接着,重点部分就来了。正如第一部分图中那样。总的有5个面板:背包面板,箱子面板,角色面板,商店小贩面板,以及锻造面板。其中,每一个面板都有一个基类(Inventory类),因为这些面板中有一些相似的功能和方法,所以选择使用了继承。各自对应的代码如下:

①基类Inventory:

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

/// <summary>
///存货类, 背包和箱子的基类
/// </summary>
public class Inventroy : MonoBehaviour {

    protected Slot[] slotArray;//存放物品槽的数组

    //控制背包的显示和隐藏相关变量
    private float targetAlpha = 1f;//显示目标值
    private float smothing = 4f;//渐变平滑速度
    private CanvasGroup canvasGroupMy;
    public CanvasGroup CanvasGroupMy      //对CanvasGroup的引用,用于制作隐藏显示效果
    {
        get
        {
            if (canvasGroupMy == null)
            {
                canvasGroupMy = GetComponent<CanvasGroup>();
            }
            return canvasGroupMy;
        }
    }

    // Use this for initialization
    public 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值