第三部分:关于物体的管理
总的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层,越小就越在上层。