『功能项目』QFrameWork背包框架【62】

我们打开上一篇61按G键持续显示对话内容的项目,

QFrameWork插件下载网址

qframework.cn/qf

导入即可

点击QFrameWork的偏好设置Preferences

重命名命名空间为QFrameWork

同样修改UIKit的命名空间

在场景中创建一个空物体

重命名为InventoryExample1

修改代码生成工具包的命名空间

(先制作示例案例所以设置生成脚本工具包的命名空间为.Example)

对场景中的空物体(库存案例1)增加组件 视图控制器ViewController

点击生成脚本:

创建一个脚本文件夹36-QFrameWorkExample

选择在该文件夹下生成脚本

查看路径名是否一致后点击生成代码

此时脚本就会生成在指定文件夹下了

打开脚本:InventoryExample1.cs

using UnityEngine;
using System.Collections.Generic;
namespace QFramework.Example{
    public partial class InventoryExample1 : ViewController {
        //格子位置
        public class Slot {
            public Item Item;
            public int Count;
            public Slot(Item item,int count) {
                Item = item;
                Count = count;
            }
        }
        //道具
        public class Item {
            public string Key;
            public string Name;
            public Item(string key, string name) {
                Key = key;
                Name = name;
            }
        }
        public Item Item1 = new("item_1", "物品1");
        public Item Item2 = new("item_2", "物品2");
        public Item Item3 = new("item_3", "物品3");
        public Item Item4 = new("item_4", "物品4");
        public Item Item5 = new("item_5", "物品5");
        //格子列表
        List<Slot> mSlots = null;
        Dictionary<string,Item> mItemByKey = null; 
        void Awake(){
            mSlots = new List<Slot>(){
            new Slot(Item1,1),
            new Slot(Item2,10),
            new Slot(Item3,1),
            new Slot(Item4,1),
            };
            mItemByKey = new Dictionary<string, Item>(){
                { Item1.Key,Item1},
                { Item2.Key,Item2},
                { Item3.Key,Item3},
                { Item4.Key,Item4},
                { Item5.Key,Item5},
            };
        }
        void OnGUI(){
            //调用IM帮助类的设置设计分辨率函数
            IMGUIHelper.SetDesignResolution(640,360);
            foreach (var slot in mSlots) {
                //创建一个"box"类型的水平布局图形用户界面
                GUILayout.BeginHorizontal("box");
                if (slot.Count == 0)
                    GUILayout.Label($"格子:空");
                else
                    //在水平布局图形用户界面中添加一个标签
                    GUILayout.Label($"格子:{slot.Item.Name} x {slot.Count}");
                //结束水平布局组
                GUILayout.EndHorizontal();
            }
            GUILayout.BeginHorizontal();
            GUILayout.Label("物品1");
            //创建一个按钮 - 增加
            if (GUILayout.Button("+")){
                if (!AddItem("item_1"))
                    Debug.Log("物品栏已满");
            }
            //减少
            if (GUILayout.Button("-")){ SubItem("item_1"); }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("物品2");
            //创建一个按钮 - 增加
            if (GUILayout.Button("+")) {
                if (!AddItem("item_2"))
                    Debug.Log("物品栏已满");
            }
            //减少
            if (GUILayout.Button("-")) { SubItem("item_2"); }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("物品3");
            //创建一个按钮 - 增加
            if (GUILayout.Button("+")) {
                if (!AddItem("item_3"))
                    Debug.Log("物品栏已满");
            }
            //减少
            if (GUILayout.Button("-")) { SubItem("item_3"); }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("物品4");
            //创建一个按钮 - 增加
            if (GUILayout.Button("+")) {
                if (!AddItem("item_4"))
                    Debug.Log("物品栏已满");
            }
            //减少
            if (GUILayout.Button("-")) { SubItem("item_4"); }
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("物品5");
            //创建一个按钮 - 增加
            if (GUILayout.Button("+")) {
                if (!AddItem("item_5"))
                    Debug.Log("物品栏已满");
            }
            //减少
            if (GUILayout.Button("-")) { SubItem("item_5"); }
            GUILayout.EndHorizontal();

        }
        //获取道具方法
        //Slot FindSlotByKey(string itemKey) {
        //    return mSlots.Find(s => s.Item != null && s.Item.Key == itemKey && s.Count != 0);
        //}
        //获取道具方法
        Slot FindSlotByKey(string itemKey) => mSlots.Find(s => s.Item != null && s.Item.Key == itemKey && s.Count != 0);
        //获取空格子方法
        Slot FindEmptySlot() => mSlots.Find(s => s.Count == 0);
        //可以增加格子方法
        Slot FindAddableSlot(string itemKey) {
            var slot = FindSlotByKey(itemKey);
            if (slot == null) {
                slot = FindEmptySlot();
                if (slot != null)
                    slot.Item = mItemByKey[itemKey];
            }
            return slot;
        }
        //增加道具方法
        bool AddItem(string itemKey,int addCount = 1) {
            var slot = FindAddableSlot(itemKey);
            if (slot == null)
                return false;
            else
                slot.Count += addCount;
            return true;
        }
        //减少道具方法
        bool SubItem(string itemKey, int subCount = 1) {
            var slot = FindSlotByKey(itemKey);
            if (slot != null) {
                slot.Count -= subCount;
                return true;
            }
            return false;    
        }
    }
}

运行项目

本章运用QFrameWork插件制作了简单背包道具使用案例

接下来的文章内容:

1.QFrameWork背包框架重构

2.窗口可拖拽脚本

3.点击名称寻找地点功能

4.隐藏怪物的生成

5.怪物I攻击范围内的主动攻击

6.掉落坐骑蛋的获取

7.异步传送转换场景

以及开放回合制、坐骑系统、宠物系统、背包系统、神炼系统、商城系统、Boss的目标跟随任务导航系统以及UI播放3D动画效果等等。

具体项目运行效果请关注water1024的b站视频项目演示《破碎纪元》

【Unity回合2.5D】破碎纪元_单机游戏热门视频 (bilibili.com)icon-default.png?t=O83Ahttps://www.bilibili.com/video/BV1rZY4e9Ebs/?spm_id_from=333.999.0.0&vd_source=547091a95b03acfa8e8a9e46ef499cd6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值