项目过后对项目功能进行记录,(width*height)风格背包实现细节,包含对物体的存放,装备,替换,对未知装备的鉴定,物体前缀的获取,项目类型为tcg+rpg,背包的作用主要为游戏中的物品的获取存放,卡牌的获取管理,对可叠加物品的存放,(width*height)的目的为对物品的存放管理,其效果如下

  1. 基础物品 (width*height)

     物品的创建通过ScriptObject进行创建,根据物品类型设计相关的尺寸(width*height),利用deepcopy创建实体存放如背包,项目的物品管理通过一个独立的类

ItemDatabase进行管理,在玩家获取一个新物品(未鉴定),通过一个随机生成类来产生词缀和属性

    本项目物品的生成通过deepcopy生成物品并添加进itemdatabse,Itemdatabase的作用对游戏物品资产的管理以及生成,有利于未知装备的生成,在对商品商管理上,添加未知随机的装备可以为游戏的物品提高游戏性

    

  1. public static ItemClass DeepCopy(ItemClass obj) {
  2. GameObject oj = obj.worldObject;
  3. //ItemClass is item base class
  4. ItemClass i = (ItemClass)Process(obj);
  5. i.worldObject = oj;
  6. return i;
  7. }
  8.  
  9. static object Process(object obj) {
  10. if(obj==null)
  11. return null;
  12. Type type=obj.GetType();
  13. if(type.IsValueType || type==typeof(string)) {
  14. return obj;
  15. }
  16. else if(type.IsArray) {
  17. Type elementType=Type.GetType(
  18. type.FullName.Replace("[]",string.Empty));
  19. var array=obj as Array;
  20. Array copied=Array.CreateInstance(elementType,array.Length);
  21. for(int i=; i<array.Length; i++) {
  22. copied.SetValue(Process(array.GetValue(i)),i);
  23. }
  24. return Convert.ChangeType(copied,obj.GetType());
  25. }
  26. else if(type.IsClass) {
  27. object toret=Activator.CreateInstance(obj.GetType());
  28. FieldInfo[] fields=type.GetFields(BindingFlags.Public|
  29. BindingFlags.NonPublic|BindingFlags.Instance);
  30. foreach(FieldInfo field in fields) {
  31. object fieldValue=field.GetValue(obj);
  32. if(fieldValue==null)
  33. continue;
  34. field.SetValue(toret, Process(fieldValue));
  35. }
  36. return toret;
  37. }
  38. else
  39. throw new ArgumentException("Unknown type");
  40. }

             

  2.生成slot并将物品存放进slot(width*height)

    在物品添加进背包时,根据几个条件进行判断

        1.slot是否能填充进去

          在物品添加进背包栏,需要检测物品尺寸是否越过背包限制并物品之间是否存在重叠,重叠状态一般在物品编辑器中设置(通常为药水...),当不可叠加物品存在重叠状态中,获取状态颜色并提示状态

  1. //检测边界以及叠加状态
  2. public bool CheckItemFit(ItemClass item, InventorySlot slot, bool skipLastCheck) {
  3. //Run through all the slots that the item occupies
  4. for(int i = ; i < item.height; i++) {
  5. for(int j = ; j < item.width; j++) {
  6. //Check if the slot exists
  7. if(slot.itemStartNumber + inventoryWidth * i + j >= items.Count) {
  8. return false;
  9. }
  10. //Check to see if the first slot is located at the edge of the inventory
  11. for(int k = ; k < item.height; k++) {
  12. if(slot.itemStartNumber + inventoryWidth * k + j != slot.itemStartNumber + inventoryWidth * k) {
  13. if(((slot.itemStartNumber + inventoryWidth * i + j ) % inventoryWidth == ) && item.width != ) {
  14. return false;
  15. }
  16. }
  17. }
  18. //Last check is only used sometimes
  19. //Checks to see if there's already something in the slots
  20. if(!skipLastCheck) {
  21. if(items[slot.itemStartNumber + inventoryWidth * i + j].itemStartNumber != slot.itemStartNumber + inventoryWidth * i + j) {
  22. return false;
  23. }
  24. }
  25. else {
  26. List<int> counter = new List<int>();
  27. for(int l = ; l < item.height; l++) {
  28. for(int m = ; m < item.width; m++) {
  29. if((slot.itemStartNumber + inventoryWidth * (item.height - ) + (item.width - )) < items.Count - && items[slot.itemStartNumber + inventoryWidth * l + m].itemStartNumber != slot.itemStartNumber && items[slot.itemStartNumber + inventoryWidth * l + m].item.itemName != "" && !counter.Contains(items[slot.itemStartNumber + inventoryWidth * l + m].itemStartNumber)) {
  30. counter.Add(items[slot.itemStartNumber + inventoryWidth * l + m].itemStartNumber);
  31. }
  32. }
  33. }
  34. if(counter.Count > ) {
  35. //return false if there's more than one item
  36. return false;
  37. }
  38. else if(counter.Count == ) {
  39. return true;
  40. }
  41. }
  42. }
  43. }
  44. return true;
  45. }

    当物品状态为可添加时,添加进slot,根据width*height尺寸进行添加

  

  1. for(int l = ; l < item.height; l++) {
  2. for(int m = ; m < item.width; m++) {
  3. //First we add the items to the slots the it fills and set their slots to clear
  4. items[i + inventoryWidth * l + m].item = DeepCopy(item);
  5. items[i + inventoryWidth * l + m].itemStartNumber = i;
  6. items[i + inventoryWidth * l + m].GetComponent<Image>().color = Color.clear;
  7. items[i + inventoryWidth * l + m].stacksizeText.gameObject.SetActive(false);
  8. //If it's the first index of the added item
  9. if(items.IndexOf(items[i + inventoryWidth * l + m]) == i) {
  10. SetSlotImageSprite(items[i + inventoryWidth * l + m], item.icon);
  11. items[i + inventoryWidth * l + m].itemFrame.gameObject.SetActive(true);
  12. items[i + inventoryWidth * l + m].itemFrame.GetComponent<CanvasGroup>().interactable = true;
  13. items[i + inventoryWidth * l + m].itemFrame.GetComponent<CanvasGroup>().blocksRaycasts = true;
  14. items[i + inventoryWidth * l + m].GetComponent<CanvasGroup>().blocksRaycasts = true;
  15. items[i + inventoryWidth * l + m].itemFrame.rectTransform.sizeDelta = new Vector2(item.width * slotIconSize, item.height * slotIconSize);
  16. //If the item is stackable
  17. if(item.stackable) {
  18. items[i + inventoryWidth * l + m].stacksizeText.gameObject.SetActive(true);
  19. items[i + inventoryWidth * l + m].stacksizeText.text = item.stackSize.ToString();
  20. }
  21. //The item is unidentified
  22. if(item.unidentified) {
  23. items[i + inventoryWidth * l + m].itemImage.color = Color.red;
  24. items[i + inventoryWidth * l + m].unidentified.gameObject.SetActive(true);
  25. }
  26. }
  27. }
  28. }

      2.物品是否存在物品

          当物品存在物品时,需要对存在物品的占用尺寸进行判断(int slot=indexof(item)),当slot存在物品时,遍历slot.count 获取空栏位并存放,另外一种情况是当玩家处于dragging状态时,可以对物品进行替换,存放在物品的方式一般为

        

  1. Items[slot.itemStartNumber + inventoryWidth * l + m].item.variables

    

    

    

UNITY->(width*height)style Inventory的更多相关文章

  1. offset[Parent/Width/Height/Top/Left] 、 client[Width/Height/Top/Left] 、 Element.getBoundingClientRect()

    开篇提示:以下内容都经个人测试,参考API文档总结,但还是不能保证完全正确,若有错误,还请留言指出___________________________________________________ ...

  2. client/scroll/offset width/height/top/left ---记第一篇博客

    client/scroll/offset width/height/top/left (盒模型为contentBox,定位原点是元素左上角边框最外层的交点) clientWidth  width+左p ...

  3. as3:sprite作为容器使用时,最好不要指定width,height

    除 TextField 和 Video 对象以外,没有内容的显示对象(如一个空的 Sprite)的高度为 0,即使您尝试将 height 设置为其它值,也是这样. 如果您设置了 height 属性,则 ...

  4. 正则:img的url,width,height 和 a标签的url以及替换

    代码:// 内容:$detail['content'] //img的url,width,height $img = array(); $matches = array(); $regeImg = '/ ...

  5. ffmpeg按比例缩放--"width / height not divisible by 2" 解决方法

    最近在处理视频的时候,有这么一个需求 如果视频的分辨率宽度大于960的话,就把宽度设为960,而高度按其比例进行缩放 如果视频的分辨率高度大于540的话,就把高度设为540,而宽度按其比例进行缩放 之 ...

  6. 如何理解VB窗体中的scale类属性及width height属性之间的关系

    如何理解VB窗体中的scale类属性及width height属性之间的关系 VB中的SCALEHIEGT,SCALEWIDTH,与窗体中的WIDTH,HEIGHT的区别及关系是许多VB初学者难以理解 ...

  7. css 行内元素 块元素 替换元素 非替换元素 以及这些元素的width height margin padding 特性

    一.各种元素的width height margin padding 特性(具体css元素的分来参看二) 1.块级元素 width. height. margin的四个方向. padding的四个方向 ...

  8. 转:Canvas标签的width和height以及style.width和style.height的区别

    转自:http://www.cnblogs.com/artwl/archive/2012/02/28/2372042.html 作者:Artwl 背景 今天在博问中看到一个问题:用canvas 的 l ...

  9. [WPF系列]- Style - Specify width/height as resource in WPF

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys=" ...

随机推荐

  1. 基于颜色的R2V软件快速矢量化

    跟同学一起做SRTP,矢量化,作图的工作点名让我去做,人家说,谁让你是学地理信息的呢?哎,什么时候地理信息不再被别人当成制图画图的,我们专业就有希望了. 话虽然这么说,但工作还是要去做. (进入正题) ...

  2. python 替换字符串的方法replace()、正则re.sub()

    一.replace()函数1用字符串本身的replace方法: a = 'hello word' b = a.replace('word','python') print b   1 2 3 二.re ...

  3. 瑞星推国内唯一Linux系统杀毒软件 国产操作系统还需国产安全软件保护

    近来在IT领域最爆炸的新闻莫过于5月20日中央国家机关政府採购中心下发通知.要求中央机关採购所有计算机类产品不同意安装Windows 8.而改用国产Linux操作系统. 此消息一出,立马引起各界关注. ...

  4. JavaScript原型链中toString()方法输出alert()和console.log()得到不同的结果

    <script language="javascript"> function myObj(){ var total = 0; } myObj.prototype.ad ...

  5. 从零学React Native之03页面导航

    之前我们介绍了RN相关的知识: 是时候了解React Native了 从零学React Native之01创建第一个程序 从零学React Native之02状态机 本篇主要介绍页面导航 上一篇文章给 ...

  6. UISearchDisplayController “No Results“ cancel修改

    Recently I needed to fully customize a UISearchBar, so here are some basic "recipes" on ho ...

  7. 检查进程启动情况,开始时间、启动时间、启动进程数、进程数是否正确、PID

    #!/bin/sh bin=$(cd ``;pwd) cd ${bin} ### 定义检查函数 chk(){ programName=$ correctNum=$ programSubName=$ # ...

  8. react框架下,在页面内加载显示PDF文件,关于react-pdf-js的使用注意事项

    react框架下,在页面内加载显示PDF文件,关于react-pdf-js的使用注意事项 之前做了一个需求,在注册账号的时候,让用户同意服务条款, 服务条款是一个PDF文件, 这就需要在react内加 ...

  9. oracle中=>是什么意思呢?

    => 是 Oracle 中调用 存储过程的时候, 指定 参数名进行调用.一般是, 某些参数有默认值的时候,你需要跳过某些参数来进行调用.下面是具体的例子. 参数的默认值SQL> CREAT ...

  10. hdu 1430 魔板 (BFS+预处理)

    Problem - 1430 跟八数码相似的一题搜索题.做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可. 代码如下: ...