因为背包有很多道具,用户要根据不同需要搜索出不同的道具.  道具的属性有非常居多,游戏快开发完毕的时候,突然发现ItemManager类里面几乎每一个搜索方法都有一个foreach循环, 循环里面因为一点点不同的搜索条件就会导致重新写一个搜索方法出来.最后发现有10多种搜索方法. 后来打算优化下这个问题, 参考了下Itween传入参数进行移动的方式.

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace SearchOp
{class Program{static void Main(string[] args){Dictionary<string, object> search = new Dictionary<string, object>();search.Add("ConfigId", 101);search.Add("Bind", true);ItemManager.I.TestInit();//第一次搜索List<ItemBase> itemList = ItemManager.I.Get(search);PrintItem(itemList);//第二次搜索
            search.Clear();search.Add("Bind", false);itemList = ItemManager.I.Get(search);PrintItem(itemList);Console.ReadLine();}public static void PrintItem(List<ItemBase> itemList){foreach (var item in itemList){Console.WriteLine("Name: " + item.Name);Console.WriteLine("Id: " + item.ConfigId);Console.WriteLine("Bind: " + item.Bind);Console.WriteLine("BigType: " + item.BigType);Console.WriteLine("SubType: " + item.SubType);Console.WriteLine("TimeType: " + item.TimeType);Console.WriteLine("-----------------------");}}}public class ItemManager{private static ItemManager m_I;public static ItemManager I{get{if (m_I == null)m_I = new ItemManager();return m_I;}}private List<ItemBase> mItemList = new List<ItemBase>();public void TestInit(){mItemList.Add(new ItemBase(){Name = "幽梦之刃",ConfigId = 101,BigType = 1,SubType = 1,Bind = true,TimeType = TimelimitEnum.None});mItemList.Add(new ItemBase(){Name = "幽梦之刃",ConfigId = 101,BigType = 1,SubType = 1,Bind = false,TimeType = TimelimitEnum.None});mItemList.Add(new ItemBase(){Name = "幽梦之刃",ConfigId = 101,BigType = 1,SubType = 1,Bind = true,TimeType = TimelimitEnum.None});mItemList.Add(new ItemBase(){Name = "无尽之刃",ConfigId = 102,BigType = 1,SubType = 1,Bind = true,TimeType = TimelimitEnum.PastDue});}public List<ItemBase> Get(Dictionary<string, object> condition){List<ItemBase> tempList = new List<ItemBase>();List<ItemFilter> conditionFunList = ConvertCondition(condition);bool isNotThrough = false;foreach (var item in mItemList){isNotThrough = true;foreach (var cond in conditionFunList){if (cond.Execute(item) == false){isNotThrough = false;break;}}if (isNotThrough){tempList.Add(item);}}return tempList;}public List<ItemFilter> ConvertCondition(Dictionary<string, object> dic){List<ItemFilter> conditionFunList = new List<ItemFilter>();foreach (var str in dic){switch (str.Key){case "ConfigId":conditionFunList.Add(new ItemFilter(){Fun = ItemFilterFun.EquilId,FunArgs = str.Value});break;case "Bind":conditionFunList.Add(new ItemFilter(){Fun = ItemFilterFun.IsBind,FunArgs = str.Value});break;case "TimeType":conditionFunList.Add(new ItemFilter(){Fun = ItemFilterFun.CheckTime,FunArgs = str.Value});break;case "IsEquip":conditionFunList.Add(new ItemFilter(){Fun = ItemFilterFun.IsEquip,FunArgs = str.Value});break;case "IsMaterial":conditionFunList.Add(new ItemFilter(){Fun = ItemFilterFun.IsMaterial,FunArgs = str.Value});break;case "IsSpecial":conditionFunList.Add(new ItemFilter(){Fun = ItemFilterFun.IsSpecial,FunArgs = str.Value});break;}}return conditionFunList;}}public class ItemFilterFun{public static bool EquilId(ItemBase item, object args){int id = (int)args;return item.ConfigId == id;}public static bool IsBind(ItemBase item, object args){bool isBind = (bool)args;return item.Bind == isBind;}public static bool CheckTime(ItemBase item, object args){TimelimitEnum timeType = (TimelimitEnum)args;return item.TimeType == timeType;}public static bool IsEquip(ItemBase item, object args){return item.BigType == (int)ItemType.Equip;}public static bool IsMaterial(ItemBase item, object args){int type = item.BigType;return type == 2 || type == 4 || type == 6;}public static bool IsSpecial(ItemBase item, object args){int type = item.BigType;return type == 1 || type == 5 || type == 7 || type == 8;}}public class ItemFilter{public Func<ItemBase, object, bool> Fun;public object FunArgs;public bool Execute(ItemBase item){return Fun(item, FunArgs);}}public class ItemBase{public string Name;public int Pos;public int ConfigId;public bool Bind;public int BigType;public int SubType;public TimelimitEnum TimeType;}public enum TimelimitEnum{None = -1,PastDue = 0,NoPastDue = 1}public enum ItemType{//(0-装备,1-消耗品,2材料,3宝箱)
Equip = 0,Consume = 1,Material = 2,Task = 3,Gem = 4,SkillBook = 5,SkillGoods = 6,NumberGoods = 7,Chest = 8,TimeLimit = 9,Currency = 999,}}

转载于:https://www.cnblogs.com/plateFace/p/6490577.html

Unity 背包道具搜索相关推荐

  1. unity 背包选中_Unity背包系统实现

    MVC模式简析: Model: BackpackConfig.json:存放装备信息 Item:json文件对应的数据模型 View: Scroll Rect:制作可滑动的背包面板 Grid Layo ...

  2. Q Inventory System unity背包物品插件 使用笔记

    Q Inventory System 一个非常不错的背包物品插件 功能很完整,用起来简单.但是作者好像很久没更新了 网上相关教程也比较少,用户手册上的内容版本对不太上了.我在此记录一下学习摸索的过程, ...

  3. [Unity][FlowCanvas] FlowScript 搜索功能不可用的解决办法:更新 UnityEditor 的版本

    FlowScript 搜索功能不可用的解决办法:更新 UnityEditor 的版本 我原来的版本是 2020.3.0f1c1,装了一个 2020.3.9f1c1 就正常了 好神奇 图1 搜索功能不可 ...

  4. Unity编辑器内搜索小技巧

    一.Hierarchy场景面板--搜对象和组件资源 (1)直接根据对象名称搜场景里的物体 (2)搜索场景里挂有的某个组件 不是搜放在场景里的物体,而是搜场景里物体身上有挂的控件/脚本/属性等 比如一个 ...

  5. Unity MVC设计模式与UI背包界面制作

    Unity MVC设计模式与UI背包界面制作 MVC设计模式非常适合UI的架构,UI界面相当于View,UI转换控制相当于Controller,UI上面的数据变换相当于Model.MVC设计模式在软件 ...

  6. 《学Unity的猫》——第十二章:使用Unity制作背包,皮皮的梦想背包

    文章目录 12.1 皮皮的梦想背包 12.2 准备道具图片 12.3 UGUI打图集 12.3.1 设置图集模式:Always Enabled(Legacy Sprite Packer) 12.3.2 ...

  7. Unity背包系统-2:数据库存储方法ScriptableObject和显示背包物品InventoryManager

    1. 数据库脚本 1.1 元素(item)代码: using System.Collections; using System.Collections.Generic; using UnityEngi ...

  8. 魔坊APP项目-20-种植园,背包显示道具、用户购买道具的时候,判断背包存储是否达到上限、背包解锁

    种植园 一.背包显示道具 在背包中显示道具,会涉及到用户的背包格子的显示以及解锁问题,所以我们需要在服务端准备一个参数信息, 用于保存种植园中用户的业务参数,例如: 格子的初始化数量, 每次解锁背包格 ...

  9. Unity面试题总结

    先在右侧目录查找感兴趣的问题➡ 1. IL是什么? IL的全称是Intermediate Language,很多时候我们看到的是CIL(Common Intermediate Language,特指在 ...

  10. Unity 2D独立开发手记(五):通用任务系统

    一年多没写博了,因为迫不得已转行,这破游戏也搁置了好久,过完年也有一个月了,回来找找感觉.那就记录一下任务系统的开发吧,方便以后回忆.(2022年3月注:文章中的任务系统太旧了,仅供思路参考,获取新版 ...

最新文章

  1. jvm:运行时数据区--方法返回地址
  2. 中国氨纶市场“十四五”规划及未来动态分析报告2021年版
  3. 云炬Android开发笔记 6启动图功能开发与封装
  4. IIS 6.0 401 错误
  5. 启动tomcat不出现命令窗口
  6. iphone开发我的新浪微博客户端-用户登录准备篇(1.1)
  7. python 延时_理解Python多线程5:加锁解决问题,但又带来麻烦!
  8. js html转为实体,字符串js编码转换成实体html编码的方法(防范XSS攻击)
  9. 为什么需要 AtomicInteger 原子操作类?
  10. Android常用组件,太全了
  11. Android 一个adapter实现商品列表折叠
  12. 局计算机房制度,岳阳市统计局机房管理制度
  13. JavaScript的事件委托(事件代理)原理
  14. 信息系统安全等级保护流程
  15. 聊一次跳槽被怼的经历
  16. 「Fail-Fast与Fail-Safe机制」
  17. MacBook怎样装Win10 双系统玩转Win10
  18. Android进阶之光 读书笔记
  19. 第一卷清晨的帝国 第一百五十一章 起步
  20. 同样是IT行业,测试和开发薪资真有这么大差别?

热门文章

  1. 一个假程序员的肺腑之言
  2. 苹果推出新款iPod touch:支持增强现实体验 售价1599元起
  3. C# Cad二次开发新手入门系列教程(三)添加图形到数据库
  4. Quick-Cocos2d-x初学者游戏教程(1) ——【Quick的一些基础知识】
  5. SpringCloud中的注册中心
  6. aws cloud map_Google Cloud击败AWS的11种方式
  7. 无线网络一直连接限制
  8. DLL的灾难 --- DEBUG与RELEASE版本不能交叉调用
  9. 低功耗智能语音信号处理之六脉神剑总纲
  10. 科学的减肥方式有哪些?不可采取的方式又有哪些?