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

embedded/2024/9/25 21:25:43/

我们打开上一篇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 {//插槽格子Slotpublic 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获取Itempublic 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;elseslot.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道具栏物品生成

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.ppmy.cn/embedded/116835.html

相关文章

oracle direct path read处理过程

文章目录 缘起处理过程1.AWR Report 分析2.调查direct path read发生的table3.获取sql text4.解释sql并输出执行计划&#xff1a; 结论&#xff1a;补充direct path read等待事件说明 缘起 记录direct path read处理过程 处理过程 1.AWR Report 分析 问题发生时间段awr如下…

使用 Istio 缓解电信 5G IoT 微服务 Pod 架构的安全挑战

在 Kubernetes 集群中部署微服务在 5G 电信中至关重要。但是&#xff0c;它也带来了重大的安全风险。虽然防火墙规则和代理提供了初始安全性&#xff0c;但 Kubernetes 中的默认通信机制&#xff08;例如未加密的网络流量和缺乏访问控制&#xff09;本质上是不安全的。这种不安…

如何防止U盘资料被复制?(最全攻略来了,第一种你Get了吗?)

防止U盘资料被复制是一个涉及多个层面的策略。 以下是最全面的攻略&#xff0c;旨在帮助您确保U盘中的数据安全&#xff1a; 1. 使用加密软件 加密U盘&#xff1a;利用专业的加密软件&#xff08;如安企神、Verypt等&#xff09;对U盘进行全盘或分区加密。 这些软件可以确保只…

MySQL内存(Buffer Pool)

Buffer Pool MySQL 的数据存在磁盘&#xff0c;但是不能每次读取数据都从磁盘里去&#xff0c;这样磁盘IO太频繁&#xff0c;存在性能问题。 InnoDB设计了一个缓存池&#xff08;Buffer Pool&#xff09;&#xff0c;缓冲池在内存中。 默认配置Buffer Pool大小为128MB&#xf…

Lanterns (dp 紫 线段树 二分 维护dp)

Lanterns - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 让所有点被覆盖&#xff0c;那么状态可以设计成覆盖一段前缀&#xff0c;并且中间不允许出现断点 由于CF崩了&#xff0c;所以暂时没提交代码。 记f(i) 为前 i 个灯笼点亮的最长前缀。 由于答案具有保留性&#xff…

【第2章 开始学习C++】进入C++

文章目录 导语C语言输入和输出main( )函数作为接口的函数头C预处理器和iostream文件头文件名名称空间使用 cout 进行 C 输出控制符 endl 导语 首先介绍一个显示消息的简单C程序。 源代码中包含一些供读者阅读的注释&#xff0c; 这些注释都以 // 打头&#xff0c; 编译器将忽…

51单片机-系列-数码管中断和定时器

&#x1f308;个人主页&#xff1a;羽晨同学 &#x1f4ab;个人格言:“成为自己未来的主人~” 数码管 8051单片机的最小系统 电源&#xff08;5V&#xff09;复位电路晶振&#xff08;单片机的心脏&#xff09;如果要使用PO口&#xff0c;必须加4.7K-10K上拉电阻&#xf…

linux命令行快捷键

第一章 linux之帮助命令 第二章 linux命令行快捷键 文章目录 linux命令行快捷键 linux命令行快捷键 Tab 命令补全或文件补全Ctrlu 删除或剪切光标之前的命令Ctrla 将光标移动到命令行开头Ctrle 将光标移动到命令行结尾ctrlc 终止当前命令ctrll 清屏ctrly 粘贴ctrlu的内容 参考…