这一篇介绍各个面板(UI)与数据的交互,以及对更细节方面的处理(如每一个面板的物品槽的管理)。PS:具体逻辑参照UML类图即可。
**①存货总管理类:重点是实现对物品Json文件的解析。**
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class InventroyManager : MonoBehaviour {
private static InventroyManager _instance;
public static InventroyManager Instance
{
get {
if (_instance == null)
{
_instance = GameObject.Find("InventroyManager").GetComponent<InventroyManager>();
}
return _instance;
}
}
private List<Item> itemList;
private ToolTip toolTip;
private bool isToolTipShow = false;
private Canvas canvas;
private Vector2 toolTipOffset =new Vector2(15, -10);
private ItemUI pickedItem;
public ItemUI PickedItem { get { return pickedItem; } }
private bool isPickedItem = false;
public bool IsPickedItem { get { return isPickedItem; } }
void Awake()
{
ParseItemJson();
}
void Start()
{
toolTip = GameObject.FindObjectOfType<ToolTip>();
canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
pickedItem = GameObject.Find("PickedItem").GetComponent<ItemUI>();
pickedItem.Hide();
}
void Update()
{
if (isToolTipShow == true && isPickedItem == false)
{
Vector2 postionToolTip;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, null, out postionToolTip);
toolTip.SetLocalPosition(postionToolTip+toolTipOffset);
}
else if (isPickedItem == true)
{
Vector2 postionPickeItem;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, null, out postionPickeItem);
pickedItem.SetLocalPosition(postionPickeItem);
}
if (isPickedItem == true && Input.GetMouseButtonDown(0) && UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(-1) == false )
{
isPickedItem = false;
pickedItem.Hide();
}
}
public void ParseItemJson()
{
itemList = new List<Item>();
TextAsset itemText = Resources.Load<TextAsset>("Item");
string itemJson = itemText.text;
JSONObject j = new JSONObject(itemJson);
foreach (var temp in j.list)
{
Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType), temp["type"].str);
int id = (int)(temp["id"].n);
string name = temp["name"].str;
Item.ItemQuality quality = (Item.ItemQuality)System.Enum.Parse(typeof(Item.ItemQuality), temp["quality"].str);
string description = temp["description"].str;
int capacity = (int)(temp["capacity"].n);
int buyPrice = (int)(temp["buyPrice"].n);
int sellPrice = (int)(temp["sellPrice"].n);
string sprite = temp["sprite"].str;
Item item = null;
switch (type)
{
case Item.ItemType.Consumable:
int hp = (int)(temp["hp"].n);
int mp = (int)(temp["mp"].n);
item = new Consumable(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, hp, mp);
break;
case Item.ItemType.Equipment:
int strength = (int)(temp["strength"].n);
int intellect = (int)(temp["intellect"].n);
int agility = (int)(temp["agility"].n);
int stamina = (int)(temp["stamina"].n);
Equipment.EquipmentType equiType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType), temp["equipType"].str);
item = new Equipment(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite,strength,intellect,agility,stamina,equiType);
break;
case Item.ItemType.Weapon:
int damage = (int)(temp["damage"].n);
Weapon.WeaponType weaponType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType), temp["weaponType"].str);
item = new Weapon(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite, damage, weaponType);
break;
case Item.ItemType.Material:
item = new Material(id, name, type, quality, description, capacity, buyPrice, sellPrice, sprite);
break;
}
itemList.Add(item);
}
}
public Item GetItemById(int id)
{
foreach (Item item in itemList)
{
if (item.ID == id)
{
return item;
}
}
return null;
}
public void ShowToolTip(string content)
{
if (this.isPickedItem == true) return;
toolTip.Show(content);
isToolTipShow