『功能项目』QFrameWork框架重构OnGUI【63】

news/2024/9/19 6:58:16 标签: Unity引擎, C#, 重构背包框架

我们打开上一篇62QFrameWork背包框架的项目,

上文将功能实现在一个脚本中

本章要做的事情让脚本实现背包框架思想

首先按照图示创建脚本:

创建脚本:Item.cs

namespace QFramework {
    public class Item{//道具
        public string Key;
        public string Name;
        public Item(string key, string name){
            Key = key;
            Name = name;
        }
    }
}

创建脚本:Slot.cs

namespace QFramework {//插槽格子Slot
    public class Slot{
        public Item Item;
        public int Count;
        public Slot(Item item, int count){
            Item = item;
            Count = count;
        }
    }
}

创建脚本:QFramework.cs

using System.Collections.Generic;
namespace QFramework {
    public class ItemKit{
        //数据
        public static Item Item1 = new("item_1", "物品1");
        public static Item Item2 = new("item_2", "物品2");
        public static Item Item3 = new("item_3", "物品3");
        public static Item Item4 = new("item_4", "物品4");
        public static Item Item5 = new("item_5", "物品5");
        //插槽格子Slot列表
        public static List<Slot> Slots = new List<Slot>() {
            new Slot(Item1,1),
            new Slot(Item2,10),
            new Slot(Item3,1),
            new Slot(Item4,1),
        };
        //根据Key获取Item
        public static Dictionary<string, Item> ItemByKey = new Dictionary<string, Item>() {
                { Item1.Key,Item1 },
                { Item2.Key,Item2 },
                { Item3.Key,Item3 },
                { Item4.Key,Item4 },
                { Item5.Key,Item5 },
        };
        //获取道具方法
        //Slot FindSlotByKey(string itemKey) {
        //    return mSlots.Find(s => s.Item != null && s.Item.Key == itemKey && s.Count != 0);
        //}
        //获取道具方法
        public static Slot FindSlotByKey(string itemKey) => ItemKit.Slots.Find(s => s.Item != null && s.Item.Key == itemKey && s.Count != 0);
        //获取空格子方法
        public static Slot FindEmptySlot() => ItemKit.Slots.Find(s => s.Count == 0);
        //可以增加格子方法
        public static Slot FindAddableSlot(string itemKey){
            var slot = FindSlotByKey(itemKey);
            if (slot == null){
                slot = FindEmptySlot();
                if (slot != null)
                    slot.Item = ItemKit.ItemByKey[itemKey];
            }
            return slot;
        }
        //增加道具方法
        public static bool AddItem(string itemKey, int addCount = 1){
            var slot = FindAddableSlot(itemKey);
            if (slot == null)
                return false;
            else
                slot.Count += addCount;
            return true;
        }
        //减少道具方法
        public static bool SubItem(string itemKey, int subCount = 1){
            var slot = FindSlotByKey(itemKey);
            if (slot != null){
                slot.Count -= subCount;
                return true;
            }
            return false;
        }
    }
}

修改脚本:InventoryExample1.cs

using UnityEngine;
namespace QFramework.Example{
    public partial class InventoryExample1 : ViewController {
        void OnGUI(){
            //调用IM帮助类的设置设计分辨率函数
            IMGUIHelper.SetDesignResolution(640,360);
            foreach (var slot in ItemKit.Slots) {
                //创建一个"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 (!ItemKit.AddItem("item_1"))
                    Debug.Log("物品栏已满");
            }
            //减少
            if (GUILayout.Button("-")){ ItemKit.SubItem("item_1"); }
            GUILayout.EndHorizontal();

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

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

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

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

本章做了让脚本实现背包框架思想

接下来的文章内容:

1.QFrameWork背包框架UGUI

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


http://www.niftyadmin.cn/n/5665188.html

相关文章

深入理解API和前后端网络请求流程

在现代web应用开发中&#xff0c;理解API和网络请求流程的细节至关重要。本文将深入探讨从用户操作到后端处理&#xff0c;再到前端展示的整个过程&#xff0c;包括每个环节的作用、原理和潜在的优化点。 一、API的本质与类型 1. API的定义与作用 API&#xff08;应用程序编…

基于python+django+vue的美术馆预约系统

作者&#xff1a;计算机学姐 开发技术&#xff1a;SpringBoot、SSM、Vue、MySQL、JSP、ElementUI、Python、小程序等&#xff0c;“文末源码”。 专栏推荐&#xff1a;前后端分离项目源码、SpringBoot项目源码、SSM项目源码 系统展示 【2025最新】基于协同过滤pythondjangovue…

esp32s3分区表配置及读写

一、分区表介绍 每片 ESP32-S3 的 flash 可以包含多个应用程序&#xff0c;以及多种不同类型的数据&#xff08;例如校准数据、文件系统数据、参数存储数据等&#xff09;。因此&#xff0c;在 flash 的 默认偏移地址 0x8000 处烧写一张分区表。 分区表中的每个条目都包括以下…

71、哪吒开发板试用结合oak深度相机进行评测

基本思想:收到intel的开发板-小挪吒,正好手中也有oak相机,反正都是openvino一套玩意,进行评测一下,竟然默认是个window系统,哈哈

【专题】2024中国生物医药出海现状与趋势蓝皮书报告合集PDF分享(附原数据表)

原文链接&#xff1a;https://tecdat.cn/?p37719 出海已成为中国医药产业实现提速扩容的重要途径。目前&#xff0c;中国医药产业发展态势良好&#xff0c;创新能力不断增强&#xff0c;然而也面临着医保政策改革和带量集采带来的压力。政府积极出台多项政策支持医药企业出海…

九章云极DataCanvas公司荣获2024年服贸会“科技创新服务示范案例”

9月15日&#xff0c;2024年中国国际服务贸易交易会&#xff08;服贸会&#xff09;示范案例交流会暨颁奖典礼在北京国家会议中心举行&#xff0c;九章云极DataCanvas 公司自研的DataCanvas Alaya NeW智算操作系统凭借卓越的AI创新实力、前瞻性的市场布局以及突破性的技术革新成…

运行容器应用

kubernetes通过各种controller来管理pod的生命周期&#xff0c;为了满足不同的业务场景&#xff0c;kubernetes开发了Deployment&#xff0c;ReplicaSet&#xff0c;DaemonSet&#xff0c;StatefulSet&#xff0c;Job等多种ControllerDeployment&#xff1a; kubectl run nginx…

基于Java的居家办公OA(Office Automation)系统的设计步骤

基于Java的居家办公OA&#xff08;Office Automation&#xff09;系统是一种可以帮助员工在远程位置高效完成工作的软件解决方案。这种系统通常包括但不限于文档管理、任务分配、日程安排、会议安排等功能模块。下面是一个简单的架构概述&#xff0c;以及一些关键组件和技术选型…