当前位置: 首页 > news >正文

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

我们打开上一篇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.lryc.cn/news/445781.html

相关文章:

  • 4款AI生成PPT工具推荐,提升工作效率
  • 3.postman脚本语言、接口关联(json引用(变量)、脚本用正则表达式)、断言封装、自动化构造接口请求(Postman工具)
  • 基于SpringBoot框架的订餐系统设计与实现
  • 基于JAVA+SpringBoot+Vue的医院后台管理系统
  • 基于SSM+Vue+MySQL的农家乐预约管理系统
  • 全新热门电商API接口,实现闲鱼商品详细搜索功能
  • 求10 个整数中最大值
  • 数据结构不再难懂:带你轻松搞定排序算法
  • YOLOv8 OBB win10+ visual 2022移植部署
  • E+H超声波物位仪FMU42-ATB2A22A
  • Linux风险应对策略:保障系统安全的有效措施
  • 芝法酱学习笔记(0.3)——SpringBoot下使用mybatis做增删改查和报表
  • windows msys2 编译x264 32位动态库
  • 【pytorch】relu的实现逻辑
  • 【Python篇】深入机器学习核心:XGBoost 从入门到实战
  • 简单学习 原码反码补码 学会了你才是真正的程序员了
  • 基于规则的命名实体识别
  • C语言从头学63—学习头文件stdlib.h(二)
  • js判断一个对象里有没有某个属性
  • Python(爬虫)正则表达式
  • Linux:进程(二)
  • 【UE5】将2D切片图渲染为体积纹理,最终实现使用RT实时绘制体积纹理【第二篇-着色器制作】
  • 【OceanBase 诊断调优】—— GC问题根因分析
  • 图像面积计算一般方法及MATLAB实现
  • 指挥平台在应急场所中的主要表现有哪些
  • 智能养殖场人机交互检测系统源码分享
  • 数据集-目标检测系列-海洋鱼类检测数据集 fish>> DataBall
  • 网络威慑战略带来的影响
  • 决策树算法在机器学习中的应用
  • Leetcode面试经典150题-39.组合总数进阶:40.组合总和II