Unity 背包道具搜索
因为背包有很多道具,用户要根据不同需要搜索出不同的道具. 道具的属性有非常居多,游戏快开发完毕的时候,突然发现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,
- }
- }
Unity 背包道具搜索的更多相关文章
- Unity 背包道具搜索(2)
上一篇: http://www.cnblogs.com/plateFace/p/6490577.html 上次编写代码只是把逻辑编写出来, 对于里面的代码还存在一下问题 1. 搜索功能没有解耦 2. ...
- Unity场景道具模型拓展自定义编辑器
(一)适用情况 当游戏主角进入特定的场景或者关卡,每个关卡需要加载不同位置的模型,道具等.这些信息需要先在unity编辑器里面配置好,一般由策划干这事,然后把这些位置道具信息保存在文件,当游戏主角进入 ...
- unity编辑器的搜索框好特么坑啊,居然不支持*号通配符
上图 t:Scene或者点搜索框旁边的 分类按钮 用*.unity是什么也搜索不出来的
- Unity背包/商城物品逐个显示缓动效果-依次显示
道具栏/商城中物品逐个显示效果 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分 ...
- nyoj 456——邮票分你一半——————【背包思想搜索】
邮票分你一半 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 小珂最近收集了些邮票,他想把其中的一些给他的好朋友小明.每张邮票上都有分值,他们想把这些邮票分 ...
- 道具搜索框(|=, & , ^=)实现的列子
需求: 勾上界面上多选框筛选出符合的道具 思路: 1. 使用组合数字让一个数字包含多这个搜索条件,比如2代表搜索衣服和武器, 数字按照2的n次幂的值递增,通过|,&,^运算符实现一个数字包含多 ...
- HDU - 5887 2016青岛网络赛 Herbs Gathering(形似01背包的搜索)
Herbs Gathering 10.76% 1000ms 32768K Collecting one's own plants for use as herbal medicines is pe ...
- Unity Shader后处理-搜索灰度效果
如U3D中Hierarchy面板下的搜索效果: 讲解分析: 1.这种PostEffect效果其实就是指Unity shader的后处理,即游戏中实现屏幕特效的常见方法.顾名思义屏幕后处理就是指在渲染完 ...
- [Unity]背包效果-使用NGUI实现物品的拖拽效果Drag
背包效果-使用NGUI实现物品的拖拽效果Drag 效果实现如图 对象层级关系图 PacketCell - Right 对象作为单元格背景 PacketContainer 对象作为单元格容器 Packe ...
随机推荐
- poj 2287(贪心)
Tian Ji -- The Horse Racing Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 12490 Acc ...
- Ubuntu 16.04LTS 常用软件安装
一.遇到的问题 1.su认证失败 sudo passwd //输入命令,然后修改密码即可 2.移动启动器 gsettings set com.canonical.Unity.Launcher laun ...
- hdu 4055 Number String (基础dp)
Number String Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- bzoj 2073: [POI2004]PRZ
2073: [POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍在桥上的 ...
- Problem C: 零起点学算法82——数组中查找数
#include<stdio.h> int main(void) { ],m; while(scanf("%d",&n)!=EOF) { ;i<n;i++ ...
- .net设置中GridView自适应列宽
有一个项目只有30分钟开发时间,速成,使用了古老的.net gridview. 但需要列宽自适应好看些. 于是琢磨了,实现思路如下. 先看下大致效果(很粗暴没有优化) 代码如下: protected ...
- vs2017 新建Class 文件时,自动添加作者版权声明注释
1.用文本打开,在其头部加上 “C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ItemTempl ...
- 【docker】解决docker pull镜像 拉取镜像龟速的问题,docker拉取镜像使用阿里云docker镜像加速器
在docker拉取mysql镜像过程中,出现龟速的问题,解决这个问题的方法: 这个页面 停留了好久好久,依旧没有下载完成. 碰上这种情况 1.先退出Ctrl+C 2.在浏览器上进入阿里云docker库 ...
- zabbix3.2 报错 Database error
一.Database errorThe frontend does not match Zabbix database. Current database version (mandatory/opt ...
- window进入本地组策略编辑器和计算机管理界面
win+R 进入运行窗口 输入gpedit.msc 进入本地组策略编辑器, 右键我的电脑,点击管理 进入计算机管理界面