unity背包和拾取系统(三)

第三部分:关于物体的管理

总的Item类:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using System;


public  class  Item 
{
    
    private int m_id;
    private string m_name;
    private string m_description;
    private string m_type;
    private float attack;
    private float nowhp;
    private float maxhp;

    public GameObject m_panel;
    public Text m_text;
    protected Image m_image;


    public Item()
    {

    }

    public Item(int Item_ID,string Item_name,string Item_description,string Item_type,float Item_attack,float Item_nowhp,float Item_maxhp)
    {
        Id = Item_ID;
        Name = Item_name;
        Description = Item_description;
        Type = Item_type;
        Attack = Item_attack;
        Nowhp = Item_nowhp;
        Maxhp = Item_maxhp;


    }
    //用来显示物品的介绍
    public virtual void TextShow()
    {
        m_text.text = "物品名: " + m_name + "\n" + "物品类型" + m_type + "\n" + "物品描述" + m_description;
        m_panel.SetActive(true);
    }
    //关闭物品的介绍
    public void TextClose()
    {
        m_panel.SetActive(false);
    }

    public int Id
    {
        get
        {
            return m_id;
        }

        set
        {
            m_id = value;
        }
    }
    public int GetId()
    {
        return m_id; 
    }
    public string Name
    {
        get
        {
            return m_name;
        }

        set
        {
            m_name = value;
        }
    }

    public string Description
    {
        get
        {
            return m_description;
        }

        set
        {
            m_description = value;
        }
    }

    public string Type
    {
        get
        {
            return m_type;
        }

        set
        {
            m_type = value;
        }
    }

    public float Attack
    {
        get
        {
            return attack;
        }

        set
        {
            attack = value;
        }
    }

    public float Nowhp
    {
        get
        {
            return nowhp;
        }

        set
        {
            nowhp = value;
        }
    }

    public float Maxhp
    {
        get
        {
            return maxhp;
        }

        set
        {
            maxhp = value;
        }
    }
}

几个子类主要是重写相应的构造方法和介绍的显示方法,因为不同物体可能要显示的东西不同,所以需要的就重写。举个例子:

using UnityEngine;
using System.Collections;

public class EquipmentItem : Item
{

    public EquipmentItem(int Item_ID, string Item_name, string Item_description, string Item_type,float Item_attack,float Item_maxhp)//:base(Item_ID,Item_name, Item_description, Item_type)
    {
        Id = Item_ID;
        Name = Item_name;
        Description = Item_description;
        Type = Item_type;

        Attack = Item_attack;
        Maxhp = Item_maxhp;
    }


    public override void TextShow()
    {
        Debug.Log("textshow执行");
        m_text.text = "物品名: " +"<color=#CD7F32>"+ Name+"</color>" + "\n" + "物品类型:" + "<color=#00ff00>"+Type+"</color>" + "\n" + "物品描述:" + Description+"\n"+"攻击加成:"+Attack+"\n"+"hp加成:"+Maxhp;
        m_panel.SetActive(true);
    }
}

总的物品存储类:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.EventSystems;

public class ArticleManage
{
    private Dictionary<string, EquipmentItem> equipmentDictionary = new Dictionary<string, EquipmentItem>();
    private Dictionary<string, ConsumableItem> consuableDictionary = new Dictionary<string, ConsumableItem>();
    private Dictionary<string, TaskThingItem> taskthingDictionary = new Dictionary<string,TaskThingItem>();
   

 
   public void Awake()
    {
        equipmentDictionary.Add("test_sword", new EquipmentItem(1, "test_sword", "测试描述", "武器", 20f, 10f));
        equipmentDictionary.Add("head_equipment", new EquipmentItem(2, "诅咒之帽", "一顶被诅咒的帽子", "头甲", 50f, 100f));
        equipmentDictionary.Add("coat_equipment", new EquipmentItem(3, "远古之甲", "虽然很破损,但是真的很厉害", "胸甲", 20f, 50f));
        equipmentDictionary.Add("bottoms_equipment", new EquipmentItem(4, "哥布林之王的绑腿", "这可是王的绑腿!","腿甲 ", 20f, 50f));
        equipmentDictionary.Add("shoes_equipment", new EquipmentItem(5, "风暴之靴", "我跑的贼快你信不", "靴子", 20f, 50f));
        equipmentDictionary.Add("low_sword", new EquipmentItem(6, "法师的新手法杖", "我们新手村都用这个", "武器", 20f, 10f));
        equipmentDictionary.Add("middle_sword", new EquipmentItem(7, "饕餮之杖", "它...它是活的!", "武器", 50f, 40f));
        equipmentDictionary.Add("high_sword", new EquipmentItem(8, "终结魔球", "拥有了它毁灭世界也不是很难嘛", "武器", 120f, 200f));
        consuableDictionary.Add("hp", new ConsumableItem(1, "大瓶的回复药剂","可以回复大量血的炼金物品","消耗品",0f,100f));
        taskthingDictionary.Add("taskthing_01", new TaskThingItem(1, "通界球", "进入最终次元的必需品", "任务物品"));
       
    }


    public Item Search(GameObject search_object,string tag_name)
    {
        string search_name = search_object.name;
      
        Item m_item=new Item();
       
        switch (tag_name)
        {

            case "equipment":
                m_item = equipmentDictionary[search_name];
                Debug.Log("Article" + equipmentDictionary[search_name].Name);
                break;
            case "consumable":
                m_item = consuableDictionary[search_name];
                break;
            case "taskthing":
                m_item = taskthingDictionary[search_name];
                break;
        }
        Debug.Log("ArticleMange:" + m_item.Name);
        return m_item;
       
    }
}

怎么显示相应的物品介绍呢,就需要在每一个UItem物体上面添加一个脚本,用来管理自身的存储自身的属性,然后反馈给自己的介绍框,然后通过继承IPointerEnterHandler,IPointerExitHandler接口,实现里面的相应方法,实现鼠标进入和鼠标出来的时候,完成介绍框的出现和隐藏。

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;


public class ItemManage : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
    Item Item;
    EquipmentItem EquipmentItem;
    ConsumableItem ConsumableItem;
    TaskThingItem TaskThingItem;
    Transform originalParentGamobject;

    CanvasGroup canvasGroup;
    Transform bagcanvas;

    public void TextClose()
    {
        gameObject.GetComponent<ItemManage>().Item.TextClose();
    
    }
    public void TextShow()
    {
        Debug.Log(gameObject.name + " " + gameObject.tag);
        //通过比较自身物体的tag来决定执行哪个重写的TextShow方法

        if (gameObject.CompareTag("equipment") == true)
        {     
            gameObject.GetComponent<ItemManage>().EquipmentItem1.TextShow();
        }

        else if (gameObject.CompareTag("consumable") == true)
        {
            gameObject.GetComponent<ItemManage>().ConsumableItem1.TextShow();
        }
        else if (gameObject.CompareTag("taskthing") == true)
        {
          
            gameObject.GetComponent<ItemManage>().TaskThingItem1.TextShow();
        }
    }


    //将字典里的物品信息,存储到自己的物品上面,然后找到自己的介绍框,将这些介绍信息给介绍框
    public void SetItem(string obj_tagname,Item obj_item)
    {
        Debug.Log("ItemManage:" + obj_item.Name);
        Item = new Item();
        switch (obj_tagname)
        {
            case "equipment":
                EquipmentItem = new EquipmentItem(obj_item.Id,obj_item.Name,obj_item.Description,obj_item.Type,obj_item.Attack,obj_item.Maxhp);
                EquipmentItem.m_panel = gameObject.transform.Find("Reduce").gameObject;
                EquipmentItem.m_text = EquipmentItem.m_panel.transform.GetChild(0).GetComponent<Text>();
                Debug.Log(EquipmentItem.m_panel);
                Item.m_panel = EquipmentItem.m_panel;
                Item.m_text = EquipmentItem.m_text;
                break;
            case "consumable":
                ConsumableItem = new ConsumableItem(obj_item.Id, obj_item.Name, obj_item.Description, obj_item.Type, obj_item.Attack, obj_item.Nowhp);
                ConsumableItem.m_panel = gameObject.transform.Find("Reduce").gameObject;
                ConsumableItem.m_text = ConsumableItem.m_panel.transform.GetChild(0).GetComponent<Text>();
                Item.m_panel =ConsumableItem.m_panel;
                Item.m_text = ConsumableItem.m_text;
                break;
            case "taskthing":
                TaskThingItem = new TaskThingItem(obj_item.Id, obj_item.Name, obj_item.Description, obj_item.Type);
                TaskThingItem.m_panel = gameObject.transform.Find("Reduce").gameObject;
                TaskThingItem.m_text = TaskThingItem.m_panel.transform.GetChild(0).GetComponent<Text>();
                Item.m_panel = TaskThingItem.m_panel;
                Item.m_text = TaskThingItem.m_text;
                break;
        }
       
    }
    public EquipmentItem EquipmentItem1
    {
        get
        {
            return EquipmentItem;
        }

        set
        {
            EquipmentItem = value;
        }
    }

    public ConsumableItem ConsumableItem1
    {
        get
        {
            return ConsumableItem;
        }

        set
        {
            ConsumableItem = value;
        }
    }

    public TaskThingItem TaskThingItem1
    {
        get
        {
            return TaskThingItem;
        }

        set
        {
            TaskThingItem = value;
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        TextShow();
    }

    public void OnPointerExit(PointerEventData eventData)
    {

        TextClose();

    }
}

遇到的小问题:
1.为什么在Item里面就把所有的属性给加进去了而没有在子类里面加,这是因为物品管理类里面的Search方法要返回一个值,如果分开写属性的话,这个就没法弄返回值,很不好搞,所以就干脆写在了总的Item类里面,
2.假如就这样写的话,会发现物品介绍框会被Ucell这个UI给遮盖,怎么办呢?之前想到的方法就是利用拖拽时候的思路,将鼠标进去悬浮的时候,将UItem的父物体设置为最上层的Bagcanvas,但这里就会与拖拽里面的一个思路有冲突,拖拽方法里面的鼠标点击时的父物体,就会变成Bagcanvas,所以拖拽就出现了问题。第二个思路是网上说的,越后面的UI会后渲染,遮盖前面的UI,但是这样弄得,会改变布局里面物体的位置,也不行。
最后的方法就是在被遮挡的UI组件上面添加一个Canvas组件,然后点击Over Sorting设置UI层,越小就越在上层。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity背包系统是一个游戏中常用的组件,用于管理玩家在游戏中所获得的道具。一个完整的背包系统通常由数据、逻辑和UI部分组成。在设计过程中,我们首先进行UI设计,包括背包的标题、关闭键和背包内的格子容器。 背包系统的数据部分主要负责管理物品的信息,例如物品的名称、图片、数量等。逻辑部分负责实现将物品放置进背包、对背包内物品进行管理以及使用背包内物品等功能。实际上,当我们获取或移动物品时,我们会直接修改背包数据库中的物品信息。修改完成后,我们通过背包中的UI元素来获取背包数据库中相应索引的物品信息,并将物品栏中的UI信息更新为数据库中对应序号的物品的图片和数量。 通过将背包系统分解为数据、逻辑和UI部分,我们可以有效地管理背包系统的复杂逻辑关系,并使代码更加清晰和易于维护。这种设计模式有助于提高游戏开发的效率和代码质量。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Unity游戏开发:背包系统的实现](https://blog.csdn.net/float_freedom/article/details/126243888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Unity3D RPG实现 2 —— 背包系统](https://blog.csdn.net/weixin_43757333/article/details/123187025)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值