S老师 背包系统 装备系统 锻造系统 学习

Inventory

  1 using UnityEngine;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System;
  5 using System.Text;
  6 
  7 /// <summary>
  8 /// 面板基类
  9 /// </summary>
 10 public class Inventory:MonoBehaviour {
 11 
 12     /// <summary>
 13     /// 插槽列表
 14     /// </summary>
 15     protected Slot[] slotList;
 16     /// <summary>
 17     /// 目标透明度
 18     /// </summary>
 19     private float targetAlpha = 1;
 20     /// <summary>
 21     /// 动画速度
 22     /// </summary>
 23     private float smoothing = 4;
 24     /// <summary>
 25     /// CanvasGroup
 26     /// </summary>
 27     private CanvasGroup canvasGroup;
 28 
 29 
 30     public virtual void Start() {
 31         slotList = GetComponentsInChildren<Slot>();
 32         canvasGroup = GetComponent<CanvasGroup>();
 33     }
 34 
 35 
 36     void Update() {
 37         if(canvasGroup.alpha != targetAlpha) {
 38             canvasGroup.alpha = Mathf.Lerp(canvasGroup.alpha,targetAlpha,smoothing * Time.deltaTime);
 39             if(Mathf.Abs(canvasGroup.alpha - targetAlpha) < .01f) {
 40                 canvasGroup.alpha = targetAlpha;
 41             }
 42         }
 43     }
 44 
 45     /// <summary>
 46     /// 根据物品ID存储物品
 47     /// </summary>
 48     public bool StoreItem(int id) {
 49         Item item = InventoryManager.Instance.GetItemById(id);
 50         return StoreItem(item);
 51     }
 52 
 53     /// <summary>
 54     /// 存储物品
 55     /// </summary>
 56     public bool StoreItem(Item item) {
 57         if(item == null) {
 58             Debug.LogWarning("要存储的物品的id不存在");
 59             return false;
 60         }
 61         if(item.Capacity == 1) {
 62             Slot slot = FindEmptySlot();
 63             if(slot == null) {
 64                 Debug.LogWarning("没有空的物品槽");
 65                 return false;
 66             } else {
 67                 slot.StoreItem(item);//把物品存储到这个空的物品槽里面
 68             }
 69         } else {
 70             Slot slot = FindSameIdSlot(item);
 71             if(slot != null) {
 72                 slot.StoreItem(item);
 73             } else {
 74                 Slot emptySlot = FindEmptySlot();
 75                 if(emptySlot != null) {
 76                     emptySlot.StoreItem(item);
 77                 } else {
 78                     Debug.LogWarning("没有空的物品槽");
 79                     return false;
 80                 }
 81             }
 82         }
 83         return true;
 84     }
 85 
 86     /// <summary>
 87     /// 查找空插槽
 88     /// </summary>
 89     private Slot FindEmptySlot() {
 90         //遍历插槽列表
 91         foreach(Slot slot in slotList) {
 92             //没有子物体
 93             if(slot.transform.childCount == 0) {
 94                 //返回这个插槽
 95                 return slot;
 96             }
 97         }
 98         return null;
 99     }
100 
101     /// <summary>
102     /// 查抄相同ID的插槽
103     /// </summary>
104     private Slot FindSameIdSlot(Item item) {
105         //遍历插槽列表
106         foreach(Slot slot in slotList) {
107             //有子物体,ID相同,插槽没满
108             if(slot.transform.childCount >= 1 && slot.GetItemId() == item.ID && slot.IsFilled() == false) {
109                 //返回插槽
110                 return slot;
111             }
112         }
113         return null;
114     }
115 
116     /// <summary>
117     /// 显示
118     /// </summary>
119     public void Show() {
120         canvasGroup.blocksRaycasts = true;
121         targetAlpha = 1;
122     }
123 
124     /// <summary>
125     /// 隐藏
126     /// </summary>
127     public void Hide() {
128         canvasGroup.blocksRaycasts = false;
129         targetAlpha = 0;
130     }
131 
132     /// <summary>
133     /// 切换显示/隐藏
134     /// </summary>
135     public void DisplaySwitch() {
136         if(targetAlpha == 0) {
137             Show();
138         } else {
139             Hide();
140         }
141     }
142 
143     /// <summary>
144     /// 保存信息
145     /// </summary>
146     public void SaveInventory() {
147         StringBuilder sb = new StringBuilder();
148         //遍历插槽
149         foreach(Slot slot in slotList) {
150             //有子物体
151             if(slot.transform.childCount > 0) {
152                 //获取物品UI
153                 ItemUI itemUI = slot.transform.GetChild(0).GetComponent<ItemUI>();
154                 sb.Append(itemUI.Item.ID + "," + itemUI.Amount + "-");
155             } else {
156                 sb.Append("0-");
157             }
158         }
159         PlayerPrefs.SetString(this.gameObject.name,sb.ToString());
160     }
161 
162     /// <summary>
163     /// 加载信息
164     /// </summary>
165     public void LoadInventory() {
166         if(PlayerPrefs.HasKey(this.gameObject.name) == false) return;
167         string str = PlayerPrefs.GetString(this.gameObject.name);
168         string[] itemArray = str.Split('-');
169         for(int i = 0;i < itemArray.Length - 1;i++) {
170             string itemStr = itemArray[i];
171             if(itemStr != "0") {
172                 print(itemStr);
173                 string[] temp = itemStr.Split(',');
174                 int id = int.Parse(temp[0]);
175                 Item item = InventoryManager.Instance.GetItemById(id);
176                 int amount = int.Parse(temp[1]);
177                 for(int j = 0;j < amount;j++) {
178                     slotList[i].StoreItem(item);
179                 }
180             }
181         }
182     }
183 }
Inventory
  1 using UnityEngine;
  2 using System.Collections;
  3 using UnityEngine.UI;
  4 
  5 /// <summary>
  6 /// 角色面板
  7 /// </summary>
  8 public class CharacterPanel : Inventory
  9 {
 10     /// <summary>
 11     /// 单例
 12     /// </summary>
 13     private static CharacterPanel _instance;
 14 
 15     /// <summary>
 16     /// 单例
 17     /// </summary>
 18     public static CharacterPanel Instance
 19     {
 20         get
 21         {
 22             if (_instance == null)
 23             {
 24                 _instance = GameObject.Find("CharacterPanel").GetComponent<CharacterPanel>();
 25             }
 26             return _instance;
 27         }
 28     }
 29 
 30     /// <summary>
 31     /// 属性文本
 32     /// </summary>
 33     private Text propertyText;
 34     /// <summary>
 35     /// 玩家
 36     /// </summary>
 37     private Player player;
 38 
 39     public override void Start()
 40     {
 41         base.Start();
 42         propertyText = transform.Find("PropertyPanel/Text").GetComponent<Text>();
 43         player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
 44         UpdatePropertyText();
 45         Hide();
 46     }
 47 
 48     /// <summary>
 49     /// 穿装备和武器
 50     /// </summary>
 51     public void PutOn(Item item)
 52     {
 53         Item exitItem = null;
 54         foreach (Slot slot in slotList)
 55         {
 56             EquipmentSlot equipmentSlot = (EquipmentSlot)slot;
 57             if (equipmentSlot.IsRightItem(item))
 58             {
 59                 if (equipmentSlot.transform.childCount > 0)
 60                 {
 61                     ItemUI currentItemUI= equipmentSlot.transform.GetChild(0).GetComponent<ItemUI>();
 62                     exitItem = currentItemUI.Item;
 63                     currentItemUI.SetItem(item, 1);
 64                 }
 65                 else
 66                 {
 67                     equipmentSlot.StoreItem(item);
 68                 }
 69                 break;
 70             }
 71         }
 72         if(exitItem!=null)
 73             KnapsackPanel.Instance.StoreItem(exitItem);
 74 
 75         UpdatePropertyText();
 76     }
 77     
 78     public void PutOff(Item item)
 79     {
 80         KnapsackPanel.Instance.StoreItem(item);
 81         UpdatePropertyText();
 82     }
 83 
 84     private void UpdatePropertyText()
 85     {
 86         //Debug.Log("UpdatePropertyText");
 87         int strength = 0, intellect = 0, agility = 0, stamina = 0, damage = 0;
 88         foreach(EquipmentSlot slot in slotList){
 89             if (slot.transform.childCount > 0)
 90             {
 91                 Item item = slot.transform.GetChild(0).GetComponent<ItemUI>().Item;
 92                 if (item is Equipment)
 93                 {
 94                     Equipment e = (Equipment)item;
 95                     strength += e.Strength;
 96                     intellect += e.Intellect;
 97                     agility += e.Agility;
 98                     stamina += e.Stamina;
 99                 }
100                 else if (item is Weapon)
101                 {
102                     damage += ((Weapon)item).Damage;
103                 }
104             }
105         }
106         strength += player.BasicStrength;
107         intellect += player.BasicIntellect;
108         agility += player.BasicAgility;
109         stamina += player.BasicStamina;
110         damage += player.BasicDamage;
111         string text = string.Format("力量:{0}\n智力:{1}\n敏捷:{2}\n体力:{3}\n攻击力:{4} ", strength, intellect, agility, stamina, damage);
112         propertyText.text = text;
113     }
114 
115 }
CharacterPanel
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 箱子面板
 6 /// </summary>
 7 public class ChestPanel : Inventory {
 8 
 9     /// <summary>
10     /// 单例
11     /// </summary>
12     private static ChestPanel _instance;
13 
14     /// <summary>
15     /// 单例
16     /// </summary>
17     public static ChestPanel Instance
18     {
19         get
20         {
21             if (_instance == null)
22             {
23                 _instance = GameObject.Find("ChestPanel").GetComponent<ChestPanel>();
24             }
25             return _instance;
26         }
27     }
28 }
ChestPanel
  1 using UnityEngine;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 
  5 /// <summary>
  6 /// 锻造面板
  7 /// </summary>
  8 public class ForgePanel : Inventory {
  9 
 10     /// <summary>
 11     /// 单例
 12     /// </summary>
 13     private static ForgePanel _instance;
 14 
 15     /// <summary>
 16     /// 单例
 17     /// </summary>
 18     public static ForgePanel Instance
 19     {
 20         get
 21         {
 22             if (_instance == null)
 23             {
 24                 _instance = GameObject.Find("ForgePanel").GetComponent<ForgePanel>();
 25             }
 26             return _instance;
 27         }
 28     }
 29 
 30     /// <summary>
 31     /// 
 32     /// </summary>
 33     private List<Formula> formulaList;
 34 
 35     public override void Start()
 36     {
 37         base.Start();
 38         ParseFormulaJson();
 39     }
 40 
 41     void ParseFormulaJson()
 42     {
 43         formulaList = new List<Formula>();
 44         TextAsset formulasText = Resources.Load<TextAsset>("Formulas");
 45         string formulasJson = formulasText.text;//配方信息的Json数据
 46         JSONObject jo = new JSONObject(formulasJson);
 47         foreach (JSONObject temp in jo.list)
 48         {
 49             int item1ID = (int)temp["Item1ID"].n;
 50             int item1Amount = (int)temp["Item1Amount"].n;
 51             int item2ID = (int)temp["Item2ID"].n;
 52             int item2Amount = (int)temp["Item2Amount"].n;
 53             int resID = (int)temp["ResID"].n;
 54             Formula formula = new Formula(item1ID, item1Amount, item2ID, item2Amount, resID);
 55             formulaList.Add(formula);
 56         }
 57         //Debug.Log(formulaList[1].ResID);
 58     }
 59 
 60     public void ForgeItem(){
 61         // 得到当前有哪些材料
 62         // 判断满足哪一个秘籍的要求
 63 
 64         List<int> haveMaterialIDList = new List<int>();//存储当前拥有的材料的id
 65         foreach (Slot slot in slotList)
 66         {
 67             if (slot.transform.childCount > 0)
 68             {
 69                 ItemUI currentItemUI = slot.transform.GetChild(0).GetComponent<ItemUI>();
 70                 for (int i = 0; i < currentItemUI.Amount; i++)
 71                 {
 72                     haveMaterialIDList.Add(currentItemUI.Item.ID);//这个格子里面有多少个物品 就存储多少个id
 73                 }
 74             }
 75         }
 76 
 77         Formula matchedFormula = null;
 78         foreach (Formula formula in formulaList)
 79         {
 80             bool isMatch = formula.Match(haveMaterialIDList);
 81             if (isMatch)
 82             {
 83                 matchedFormula = formula; break;
 84             }
 85         }
 86         if (matchedFormula != null)
 87         {
 88             KnapsackPanel.Instance.StoreItem(matchedFormula.ResID);
 89             //去掉消耗的材料
 90             foreach(int id in matchedFormula.NeedIdList){
 91                 foreach (Slot slot in slotList)
 92                 {
 93                     if (slot.transform.childCount > 0)
 94                     {
 95                         ItemUI itemUI = slot.transform.GetChild(0).GetComponent<ItemUI>();
 96                         if (itemUI.Item.ID == id&& itemUI.Amount > 0)
 97                         {
 98                             itemUI.ReduceAmount(); 
 99                             if (itemUI.Amount <= 0)
100                             {
101                                 DestroyImmediate(itemUI.gameObject);
102                             }
103                             break;
104                         }
105                     }
106                 }
107             }
108 
109         }
110 
111     }
112 }
ForgePanel
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 背包
 6 /// </summary>
 7 public class KnapsackPanel : Inventory
 8 {
 9     /// <summary>
10     /// 单例
11     /// </summary>
12     private static KnapsackPanel _instance;
13 
14     /// <summary>
15     /// 单例
16     /// </summary>
17     public static KnapsackPanel Instance
18     {
19         get
20         {
21             if (_instance == null)
22             {
23                 _instance =  GameObject.Find("KnapsackPanel").GetComponent<KnapsackPanel>();
24             }
25             return _instance;
26         }
27     }
28 }
KnapsackPanel
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 商店面板
 6 /// </summary>
 7 public class VendorPanel : Inventory {
 8 
 9     /// <summary>
10     /// 单例
11     /// </summary>
12     private static VendorPanel _instance;
13 
14     /// <summary>
15     /// 单例
16     /// </summary>
17     public static VendorPanel Instance
18     {
19         get
20         {
21             if (_instance == null)
22             {
23                 _instance = GameObject.Find("VendorPanel").GetComponent<VendorPanel>();
24             }
25             return _instance;
26         }
27     }
28 
29     /// <summary>
30     /// 物品ID列表
31     /// </summary>
32     public int[] itemIdArray;
33 
34     /// <summary>
35     /// 玩家
36     /// </summary>
37     private Player player;
38 
39     public override void Start()
40     {
41         base.Start();
42         InitShop();
43         player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
44         Hide();
45     }
46 
47     private void InitShop()
48     {
49         foreach (int itemId in itemIdArray)
50         {
51             StoreItem(itemId);
52         }
53     }
54     /// <summary>
55     /// 主角购买
56     /// </summary>
57     /// <param name="item"></param>
58     public void BuyItem(Item item)
59     {
60         bool isSuccess = player.ConsumeCoin(item.BuyPrice);
61         if (isSuccess)
62         {
63             KnapsackPanel.Instance.StoreItem(item);
64         }
65     }
66     /// <summary>
67     /// 主角出售物品
68     /// </summary>
69     public void SellItem()
70     {
71         int sellAmount = 1;
72         if (Input.GetKey(KeyCode.LeftControl))
73         {
74             sellAmount = 1;
75         }
76         else
77         {
78             sellAmount = InventoryManager.Instance.PickedItemUI.Amount;
79         }
80 
81         int coinAmount = InventoryManager.Instance.PickedItemUI.Item.SellPrice * sellAmount;
82         player.EarnCoin(coinAmount);
83         InventoryManager.Instance.RemoveItem(sellAmount);
84     }
85 }
VendorPanel

Item

  1 using UnityEngine;
  2 using System.Collections;
  3 using UnityEngine.UI;
  4 
  5 /// <summary>
  6 /// UI物品
  7 /// </summary>
  8 public class ItemUI:MonoBehaviour {
  9 
 10     /// <summary>
 11     /// 关联的物品
 12     /// </summary>
 13     public Item Item { get; private set; }
 14     /// <summary>
 15     /// 关联的数量
 16     /// </summary>
 17     public int Amount { get; private set; }
 18 
 19     /// <summary>
 20     /// 物品图片
 21     /// </summary>
 22     private Image itemImage;
 23     /// <summary>
 24     /// 数量文本
 25     /// </summary>
 26     private Text amountText;
 27 
 28     /// <summary>
 29     /// 物品图片
 30     /// </summary>
 31     private Image ItemImage {
 32         get {
 33             if(itemImage == null) {
 34                 itemImage = GetComponent<Image>();
 35             }
 36             return itemImage;
 37         }
 38     }
 39 
 40     /// <summary>
 41     /// 数量文本
 42     /// </summary>
 43     private Text AmountText {
 44         get {
 45             if(amountText == null) {
 46                 amountText = GetComponentInChildren<Text>();
 47             }
 48             return amountText;
 49         }
 50     }
 51 
 52     /// <summary>
 53     /// 目标缩放
 54     /// </summary>
 55     private float targetScale = 1f;
 56 
 57     /// <summary>
 58     /// 动画缩放
 59     /// </summary>
 60     private Vector3 animationScale = new Vector3(1.4f,1.4f,1.4f);
 61 
 62     /// <summary>
 63     /// 动画速度
 64     /// </summary>
 65     private float smoothing = 4;
 66 
 67     void Update() {
 68         //当前缩放不等于目标缩放
 69         if(transform.localScale.x != targetScale) {
 70             //计算缩放插值
 71             float scale = Mathf.Lerp(transform.localScale.x,targetScale,smoothing * Time.deltaTime);
 72             //修改缩放
 73             transform.localScale = new Vector3(scale,scale,scale);
 74             //当前缩放和目标缩放的差值小于0.2
 75             if(Mathf.Abs(transform.localScale.x - targetScale) < .02f) {
 76                 //修改为目标缩放
 77                 transform.localScale = new Vector3(targetScale,targetScale,targetScale);
 78             }
 79         }
 80     }
 81 
 82     /// <summary>
 83     /// 设置物品
 84     /// </summary>
 85     public void SetItem(Item item,int amount = 1) {
 86         //修改缩放
 87         transform.localScale = animationScale;
 88         //设置物品
 89         this.Item = item;
 90         //设置数量
 91         this.Amount = amount;
 92         //根据精灵名称加载精灵
 93         ItemImage.sprite = Resources.Load<Sprite>(item.Sprite);
 94         //容量大于1
 95         if(Item.Capacity > 1)
 96             //修改数量文本
 97             AmountText.text = Amount.ToString();
 98         //容量不大于1
 99         else
100             //不显示数量文本
101             AmountText.text = "";
102     }
103 
104     /// <summary>
105     /// 增加数量
106     /// </summary>
107     public void AddAmount(int amount = 1) {
108         ///修改缩放
109         transform.localScale = animationScale;
110         //增加数量
111         this.Amount += amount;
112         //容量大于1 
113         if(Item.Capacity > 1)
114             //修改数量
115             AmountText.text = Amount.ToString();
116         //容量不大于1
117         else
118             //不显示数量文本
119             AmountText.text = "";
120     }
121 
122     /// <summary>
123     /// 减少数量
124     /// </summary>
125     public void ReduceAmount(int amount = 1) {
126         //修改缩放
127         transform.localScale = animationScale;
128         //减少数量
129         this.Amount -= amount;
130         //根据容量显示数量
131         if(Item.Capacity > 1)
132             AmountText.text = Amount.ToString();
133         else
134             AmountText.text = "";
135     }
136 
137     /// <summary>
138     /// 设置数量
139     /// </summary>
140     public void SetAmount(int amount) {
141         transform.localScale = animationScale;
142         this.Amount = amount;
143         //根据容量显示数量
144         if(Item.Capacity > 1)
145             AmountText.text = Amount.ToString();
146         else
147             AmountText.text = "";
148     }
149 
150     /// <summary>
151     /// 交换物品UI
152     /// </summary>
153     public void Exchange(ItemUI itemUI) {
154         Item itemTemp = itemUI.Item;
155         int amountTemp = itemUI.Amount;
156         itemUI.SetItem(this.Item,this.Amount);
157         this.SetItem(itemTemp,amountTemp);
158     }
159 
160     /// <summary>
161     /// 显示
162     /// </summary>
163     public void Show() {
164         gameObject.SetActive(true);
165     }
166 
167     /// <summary>
168     /// 隐藏
169     /// </summary>
170     public void Hide() {
171         gameObject.SetActive(false);
172     }
173 
174     /// <summary>
175     /// 设置局部坐标
176     /// </summary>
177     public void SetLocalPosition(Vector3 position) {
178         transform.localPosition = position;
179     }
180 }
ItemUI
  1 using UnityEngine;
  2 using System.Collections;
  3 
  4 /// <summary>
  5 /// 物品基类
  6 /// </summary>
  7 public class Item {
  8 
  9     /// <summary>
 10     /// ID
 11     /// </summary>
 12     public int ID { get; set; }
 13     /// <summary>
 14     /// 名称
 15     /// </summary>
 16     public string Name { get; set; }
 17     /// <summary>
 18     /// 类型
 19     /// </summary>
 20     public ItemType Type { get; set; }
 21     /// <summary>
 22     /// 质量
 23     /// </summary>
 24     public ItemQuality Quality { get; set; }
 25     /// <summary>
 26     /// 描述
 27     /// </summary>
 28     public string Description { get; set; }
 29     /// <summary>
 30     /// 容量
 31     /// </summary>
 32     public int Capacity { get; set; }
 33     /// <summary>
 34     /// 买的价格
 35     /// </summary>
 36     public int BuyPrice { get; set; }
 37     /// <summary>
 38     /// 卖的价格
 39     /// </summary>
 40     public int SellPrice { get; set; }
 41     /// <summary>
 42     /// 图片
 43     /// </summary>
 44     public string Sprite { get; set; }
 45 
 46 
 47     public Item() {
 48         //默认ID-1
 49         this.ID = -1;
 50     }
 51 
 52     public Item(int id,string name,ItemType type,ItemQuality quality,string des,int capacity,int buyPrice,int sellPrice,string sprite) {
 53         this.ID = id;
 54         this.Name = name;
 55         this.Type = type;
 56         this.Quality = quality;
 57         this.Description = des;
 58         this.Capacity = capacity;
 59         this.BuyPrice = buyPrice;
 60         this.SellPrice = sellPrice;
 61         this.Sprite = sprite;
 62     }
 63 
 64 
 65     /// <summary>
 66     /// 物品类型
 67     /// </summary>
 68     public enum ItemType {
 69         /// <summary>
 70         /// 消耗品
 71         /// </summary>
 72         Consumable,
 73         /// <summary>
 74         /// 装备
 75         /// </summary>
 76         Equipment,
 77         /// <summary>
 78         /// 武器
 79         /// </summary>
 80         Weapon,
 81         /// <summary>
 82         /// 材料
 83         /// </summary>
 84         Material
 85     }
 86 
 87     /// <summary>
 88     /// 品质
 89     /// </summary>
 90     public enum ItemQuality {
 91         /// <summary>
 92         /// 普通
 93         /// </summary>
 94         Common,
 95         /// <summary>
 96         /// 罕见
 97         /// </summary>
 98         Uncommon,
 99         /// <summary>
100         /// 稀有
101         /// </summary>
102         Rare,
103         /// <summary>
104         /// 史诗
105         /// </summary>
106         Epic,
107         /// <summary>
108         /// 传奇
109         /// </summary>
110         Legendary,
111         /// <summary>
112         /// 远古
113         /// </summary>
114         Artifact
115     }
116 
117     /// <summary> 
118     /// 获取提示文本
119     /// </summary>
120     /// <returns></returns>
121     public virtual string GetToolTipText() {
122         string color = "";
123         switch(Quality) {
124         case ItemQuality.Common:
125             color = "white";
126             break;
127         case ItemQuality.Uncommon:
128             color = "lime";
129             break;
130         case ItemQuality.Rare:
131             color = "navy";
132             break;
133         case ItemQuality.Epic:
134             color = "magenta";
135             break;
136         case ItemQuality.Legendary:
137             color = "orange";
138             break;
139         case ItemQuality.Artifact:
140             color = "red";
141             break;
142         }
143         string text = string.Format("<color={4}>{0}</color>\n<size=10><color=green>购买价格:{1} 出售价格:{2}</color></size>\n<color=yellow><size=10>{3}</size></color>",Name,BuyPrice,SellPrice,Description,color);
144         return text;
145     }
146 }
Item
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 消耗品类
 6 /// </summary>
 7 public class Consumable:Item {
 8 
 9     /// <summary>
10     /// 增加的血量 
11     /// </summary>
12     public int HP { get; set; }
13     /// <summary>
14     /// 增加的蓝量
15     /// </summary>
16     public int MP { get; set; }
17 
18 
19     public Consumable(int id,string name,ItemType type,ItemQuality quality,string des,int capacity,int buyPrice,int sellPrice,string sprite,int hp,int mp)
20         : base(id,name,type,quality,des,capacity,buyPrice,sellPrice,sprite) {
21         this.HP = hp;
22         this.MP = mp;
23     }
24 
25     public override string GetToolTipText() {
26         string text = base.GetToolTipText();
27 
28         string newText = string.Format("{0}\n\n<color=blue>加血:{1}\n加蓝:{2}</color>",text,HP,MP);
29 
30         return newText;
31     }
32 
33     public override string ToString() {
34         string s = "";
35         s += ID.ToString();
36         s += Type;
37         s += Quality;
38         s += Description;
39         s += Capacity;
40         s += BuyPrice;
41         s += SellPrice;
42         s += Sprite;
43         s += HP;
44         s += MP;
45         return s;
46     }
47 
48 }
Consumable
  1 using UnityEngine;
  2 using System.Collections;
  3 
  4 /// <summary>
  5 /// 装备
  6 /// </summary>
  7 public class Equipment:Item {
  8 
  9     /// <summary>
 10     /// 力量
 11     /// </summary>
 12     public int Strength { get; set; }
 13     /// <summary>
 14     /// 智力
 15     /// </summary>
 16     public int Intellect { get; set; }
 17     /// <summary>
 18     /// 敏捷
 19     /// </summary>
 20     public int Agility { get; set; }
 21     /// <summary>
 22     /// 体力
 23     /// </summary>
 24     public int Stamina { get; set; }
 25     /// <summary>
 26     /// 装备类型
 27     /// </summary>
 28     public EquipmentType EquipType { get; set; }
 29 
 30     public Equipment(int id,string name,ItemType type,ItemQuality quality,string des,int capacity,int buyPrice,int sellPrice,string sprite,
 31         int strength,int intellect,int agility,int stamina,EquipmentType equipType)
 32         : base(id,name,type,quality,des,capacity,buyPrice,sellPrice,sprite) {
 33         this.Strength = strength;
 34         this.Intellect = intellect;
 35         this.Agility = agility;
 36         this.Stamina = stamina;
 37         this.EquipType = equipType;
 38     }
 39 
 40     /// <summary>
 41     /// 装备类型
 42     /// </summary>
 43     public enum EquipmentType {
 44         /// <summary>
 45         /// 默认
 46         /// </summary>
 47         None,
 48         /// <summary>
 49         /// 50         /// </summary>
 51         Head,
 52         /// <summary>
 53         /// 脖子
 54         /// </summary>
 55         Neck,
 56         /// <summary>
 57         /// 58         /// </summary>
 59         Chest,
 60         /// <summary>
 61         /// 戒指
 62         /// </summary>
 63         Ring,
 64         /// <summary>
 65         /// 66         /// </summary>
 67         Leg,
 68         /// <summary>
 69         /// 护腕
 70         /// </summary>
 71         Bracer,
 72         /// <summary>
 73         /// 鞋子
 74         /// </summary>
 75         Boots,
 76         /// <summary>
 77         /// 肩膀
 78         /// </summary>
 79         Shoulder,
 80         /// <summary>
 81         /// 腰带
 82         /// </summary>
 83         Belt,
 84         /// <summary>
 85         /// 副手
 86         /// </summary>
 87         OffHand
 88     }
 89 
 90     public override string GetToolTipText() {
 91         string text = base.GetToolTipText();
 92 
 93         string equipTypeText = "";
 94         switch(EquipType) {
 95         case EquipmentType.Head:
 96             equipTypeText = "头部";
 97             break;
 98         case EquipmentType.Neck:
 99             equipTypeText = "脖子";
100             break;
101         case EquipmentType.Chest:
102             equipTypeText = "胸部";
103             break;
104         case EquipmentType.Ring:
105             equipTypeText = "戒指";
106             break;
107         case EquipmentType.Leg:
108             equipTypeText = "腿部";
109             break;
110         case EquipmentType.Bracer:
111             equipTypeText = "护腕";
112             break;
113         case EquipmentType.Boots:
114             equipTypeText = "靴子";
115             break;
116         case EquipmentType.Shoulder:
117             equipTypeText = "护肩";
118             break;
119         case EquipmentType.Belt:
120             equipTypeText = "腰带";
121             break;
122         case EquipmentType.OffHand:
123             equipTypeText = "副手";
124             break;
125         }
126 
127         string newText = string.Format("{0}\n\n<color=blue>装备类型:{1}\n力量:{2}\n智力:{3}\n敏捷:{4}\n体力:{5}</color>",text,equipTypeText,Strength,Intellect,Agility,Stamina);
128 
129         return newText;
130     }
131 }
Equipment
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 材料类
 6 /// </summary>
 7 public class Material:Item {
 8 
 9     public Material(int id,string name,ItemType type,ItemQuality quality,string des,int capacity,int buyPrice,int sellPrice,string sprite)
10         : base(id,name,type,quality,des,capacity,buyPrice,sellPrice,sprite) {
11     }
12 }
Material
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 武器
 6 /// </summary>
 7 public class Weapon:Item {
 8 
 9     /// <summary>
10     /// 伤害
11     /// </summary>
12     public int Damage { get; set; }
13 
14     /// <summary>
15     /// 武器类型
16     /// </summary>
17     public WeaponType WpType { get; set; }
18 
19     public Weapon(int id,string name,ItemType type,ItemQuality quality,string des,int capacity,int buyPrice,int sellPrice,string sprite,
20        int damage,WeaponType wpType)
21         : base(id,name,type,quality,des,capacity,buyPrice,sellPrice,sprite) {
22         this.Damage = damage;
23         this.WpType = wpType;
24     }
25 
26     /// <summary>
27     /// 武器类型
28     /// </summary>
29     public enum WeaponType {
30         /// <summary>
31         /// 默认
32         /// </summary>
33         None,
34         /// <summary>
35         /// 副手
36         /// </summary>
37         OffHand,
38         /// <summary>
39         /// 主手
40         /// </summary>
41         MainHand
42     }
43 
44     /// <summary>
45     /// 获取提示信息
46     /// </summary>
47     public override string GetToolTipText() {
48         string text = base.GetToolTipText();
49 
50         string wpTypeText = "";
51 
52         switch(WpType) {
53         case WeaponType.OffHand:
54             wpTypeText = "副手";
55             break;
56         case WeaponType.MainHand:
57             wpTypeText = "主手";
58             break;
59         }
60 
61         string newText = string.Format("{0}\n\n<color=blue>武器类型:{1}\n攻击力:{2}</color>",text,wpTypeText,Damage);
62 
63         return newText;
64     }
65 }
Weapon

Slot

  1 using UnityEngine;
  2 using System.Collections;
  3 using UnityEngine.EventSystems;
  4 
  5 /// <summary>
  6 /// 物品槽
  7 /// </summary>
  8 public class Slot:MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler {
  9 
 10     /// <summary>
 11     /// 物品预设
 12     /// </summary>
 13     public GameObject itemPrefab;
 14 
 15     /// <summary>
 16     /// 存储物品
 17     /// </summary>
 18     public void StoreItem(Item item) {
 19         //没有子物体
 20         if(transform.childCount == 0) {
 21             //实例化预设
 22             GameObject itemGameObject = Instantiate(itemPrefab) as GameObject;
 23             //设置父物体
 24             itemGameObject.transform.SetParent(this.transform);
 25             //设置缩放
 26             itemGameObject.transform.localScale = Vector3.one;
 27             //设置位置
 28             itemGameObject.transform.localPosition = Vector3.zero;
 29             
 30             itemGameObject.GetComponent<ItemUI>().SetItem(item);
 31         //有子物体
 32         } else {
 33             //物品数量加1
 34             transform.GetChild(0).GetComponent<ItemUI>().AddAmount();
 35         }
 36     }
 37 
 38 
 39     /// <summary>
 40     /// 得到当前物品槽存储的物品类型
 41     /// </summary>
 42     public Item.ItemType GetItemType() {
 43         return transform.GetChild(0).GetComponent<ItemUI>().Item.Type;
 44     }
 45 
 46     /// <summary>
 47     /// 得到物品的id
 48     /// </summary>
 49     /// <returns></returns>
 50     public int GetItemId() {
 51         return transform.GetChild(0).GetComponent<ItemUI>().Item.ID;
 52     }
 53 
 54     /// <summary>
 55     /// 是否满了
 56     /// </summary>
 57     /// <returns></returns>
 58     public bool IsFilled() {
 59         ItemUI itemUI = transform.GetChild(0).GetComponent<ItemUI>();
 60         //当前的数量是否大于等于容量
 61         return itemUI.Amount >= itemUI.Item.Capacity;
 62     }
 63 
 64     /// <summary>
 65     /// 鼠标进入
 66     /// </summary>
 67     public void OnPointerEnter(PointerEventData eventData) {
 68         //子物体数量大于0
 69         if(transform.childCount > 0) {
 70             //获取物品的提示文本
 71             string toolTipText = transform.GetChild(0).GetComponent<ItemUI>().Item.GetToolTipText();
 72             //显示提示文本
 73             InventoryManager.Instance.ShowToolTip(toolTipText);
 74         }
 75     }
 76 
 77     /// <summary>
 78     /// 鼠标移除
 79     /// </summary>
 80     public void OnPointerExit(PointerEventData eventData) {
 81         //子物体数量大于0
 82         if(transform.childCount > 0)
 83             //隐藏提示文本
 84             InventoryManager.Instance.HideToolTip();
 85     }
 86 
 87     /// <summary>
 88     /// 鼠标按下
 89     /// </summary>
 90     public virtual void OnPointerDown(PointerEventData eventData) {
 91         //右键点击
 92         if(eventData.button == PointerEventData.InputButton.Right) {
 93             //没有选取的物品且子物体数量大于0
 94             if(InventoryManager.Instance.IsPickedItem == false && transform.childCount > 0) {
 95                 //获取物品UI
 96                 ItemUI currentItemUI = transform.GetChild(0).GetComponent<ItemUI>();
 97                 //如果物品是装备或武器
 98                 if(currentItemUI.Item is Equipment || currentItemUI.Item is Weapon) {
 99                     //数量减1
100                     currentItemUI.ReduceAmount(1);
101                     //获取物品
102                     Item currentItem = currentItemUI.Item;
103                     //物品UI数量小于等于0
104                     if(currentItemUI.Amount <= 0) {
105                         //删除物品UI
106                         DestroyImmediate(currentItemUI.gameObject);
107                         //隐藏提示文本
108                         InventoryManager.Instance.HideToolTip();
109                     }
110                     //穿装备和武器
111                     CharacterPanel.Instance.PutOn(currentItem);
112                 }
113             }
114         }
115         //不是左键,返回
116         if(eventData.button != PointerEventData.InputButton.Left) return;
117         //子物体数量大于0
118         if(transform.childCount > 0) {
119             //获取当前ItemUI
120             ItemUI currentItemUI = transform.GetChild(0).GetComponent<ItemUI>();
121 
122             //鼠标上没有物品
123             if(InventoryManager.Instance.IsPickedItem == false)
124             {
125                 //同时按下左Control
126                 if(Input.GetKey(KeyCode.LeftControl)) {
127                     //计算拾取物品的数量
128                     int amountPicked = (currentItemUI.Amount + 1) / 2;
129                     //鼠标拾取物品
130                     InventoryManager.Instance.PickupItem(currentItemUI.Item,amountPicked);
131                     //计算剩余数量
132                     int amountRemained = currentItemUI.Amount - amountPicked;
133                     //剩余数量小于等于0
134                     if(amountRemained <= 0) {
135                         //销毁当前物品UI
136                         Destroy(currentItemUI.gameObject);//销毁当前物品
137                     //剩余数量大于0
138                     } else {
139                         //修改剩余数量
140                         currentItemUI.SetAmount(amountRemained);
141                     }
142                 //没有按下左Control
143                 } else {
144                     //拾取物品
145                     InventoryManager.Instance.PickupItem(currentItemUI.Item,currentItemUI.Amount);
146                     //销毁当前物品
147                     Destroy(currentItemUI.gameObject);
148                 }
149             //鼠标上有物品
150             } else {
151                 //拾取物品ID等于当前物品ID        
152                 if(currentItemUI.Item.ID == InventoryManager.Instance.PickedItemUI.Item.ID) {
153                     //同时按下左Control
154                     if(Input.GetKey(KeyCode.LeftControl)) {
155                         //当前物品UI的容量大于当前物品UI的数量
156                         if(currentItemUI.Item.Capacity > currentItemUI.Amount)
157                         {
158                             //当前物品UI增加数量
159                             currentItemUI.AddAmount();
160                             //减少鼠标物品UI的数量
161                             InventoryManager.Instance.RemoveItem();
162                         //容量小于数量,不处理
163                         } else {
164                             return;
165                         }
166                     //没有按下左Control
167                     } else {
168                         //当前物品UI的容量大于当前物品UI的数量
169                         if(currentItemUI.Item.Capacity > currentItemUI.Amount) {
170                             //获取当前物品UI的剩余数量
171                             int amountRemain = currentItemUI.Item.Capacity - currentItemUI.Amount;//当前物品槽剩余的空间
172                             //当前物品UI的剩余数量大于等于鼠标物品UI的数量
173                             if(amountRemain >= InventoryManager.Instance.PickedItemUI.Amount) {
174                                 //修改当前物品UI数量
175                                 currentItemUI.SetAmount(currentItemUI.Amount + InventoryManager.Instance.PickedItemUI.Amount);
176                                 //鼠标物品UI移除相应数量
177                                 InventoryManager.Instance.RemoveItem(InventoryManager.Instance.PickedItemUI.Amount);
178                             //当前物品UI的容量小于等于当前物品UI的数量
179                             } else {
180                                 //修改当前物品UI数量
181                                 currentItemUI.SetAmount(currentItemUI.Amount + amountRemain);
182                                 //鼠标物品UI移除相应数量
183                                 InventoryManager.Instance.RemoveItem(amountRemain);
184                             }
185                         //当前物品UI的容量小于等于当前物品UI的数量,不处理
186                         } else {
187                             return;
188                         }
189                     }
190                 //拾取物品ID不等于当前物品ID
191                 } else {
192                     //获取当前物品
193                     Item item = currentItemUI.Item;
194                     //获取当前物品数量
195                     int amount = currentItemUI.Amount;
196                     //设置当前物品为鼠标拾取物品
197                     currentItemUI.SetItem(InventoryManager.Instance.PickedItemUI.Item,InventoryManager.Instance.PickedItemUI.Amount);
198                     //设置鼠标拾取物品为当前物品
199                     InventoryManager.Instance.PickedItemUI.SetItem(item,amount);
200                 }
201 
202             }
203         //子物体数量小于等于0
204         } else {
205             //鼠标上有拾取物品
206             if(InventoryManager.Instance.IsPickedItem == true) {
207                 //同时按下左Control
208                 if(Input.GetKey(KeyCode.LeftControl)) {
209                     //存储物品
210                     this.StoreItem(InventoryManager.Instance.PickedItemUI.Item);
211                     //鼠标拾取物体数量减1
212                     InventoryManager.Instance.RemoveItem();
213                 //没有按下左Control
214                 } else {
215                     //循环添加物品
216                     for(int i = 0;i < InventoryManager.Instance.PickedItemUI.Amount;i++) {
217                         this.StoreItem(InventoryManager.Instance.PickedItemUI.Item);
218                     }
219                     //移除鼠标拾取物品
220                     InventoryManager.Instance.RemoveItem(InventoryManager.Instance.PickedItemUI.Amount);
221                 }
222             } else {
223                 return;
224             }
225         }
226     }
227 }
Slot
  1 using UnityEngine;
  2 using System.Collections;
  3 using UnityEngine.EventSystems;
  4 
  5 /// <summary>
  6 /// 装备槽
  7 /// </summary>
  8 public class EquipmentSlot : Slot {
  9 
 10     /// <summary>
 11     /// 装备类型
 12     /// </summary>
 13     public Equipment.EquipmentType equipType;
 14     /// <summary>
 15     /// 武器类型
 16     /// </summary>
 17     public Weapon.WeaponType wpType;
 18 
 19     /// <summary>
 20     /// 重写鼠标按下
 21     /// </summary>
 22     /// <param name="eventData"></param>
 23     public override void OnPointerDown(UnityEngine.EventSystems.PointerEventData eventData)
 24     {
 25         //右键按下
 26         if (eventData.button == PointerEventData.InputButton.Right)
 27         {
 28             //鼠标上没有拾取物,有子物体
 29             if (InventoryManager.Instance.IsPickedItem == false && transform.childCount > 0)
 30             {
 31                 ItemUI currentItemUI = transform.GetChild(0).GetComponent<ItemUI>();
 32                 Item itemTemp = currentItemUI.Item;
 33                 //删除当前物品UI
 34                 DestroyImmediate(currentItemUI.gameObject);
 35                 //脱掉放到背包里面
 36                 transform.parent.SendMessage("PutOff", itemTemp);
 37                 //隐藏提示
 38                 InventoryManager.Instance.HideToolTip();
 39             }
 40         }
 41         //不是左键按下 返回
 42         if (eventData.button != PointerEventData.InputButton.Left) return;
 43         // 手上有 东西
 44                         //当前装备槽 有装备
 45                         //无装备
 46         // 手上没 东西
 47                         //当前装备槽 有装备 
 48                         //无装备  不做处理
 49         bool isUpdateProperty = false;
 50         if (InventoryManager.Instance.IsPickedItem == true)
 51         {
 52             //手上有东西的情况
 53             ItemUI pickedItem = InventoryManager.Instance.PickedItemUI;
 54             if (transform.childCount > 0)
 55             {
 56                 ItemUI currentItemUI  = transform.GetChild(0).GetComponent<ItemUI>();//当前装备槽里面的物品
 57 
 58                 if( IsRightItem(pickedItem.Item) ){
 59                     InventoryManager.Instance.PickedItemUI.Exchange(currentItemUI);
 60                     isUpdateProperty = true;
 61                 }
 62             }
 63             else
 64             {
 65                 if (IsRightItem(pickedItem.Item))
 66                 {
 67                     this.StoreItem(InventoryManager.Instance.PickedItemUI.Item);
 68                     InventoryManager.Instance.RemoveItem(1);
 69                     isUpdateProperty = true;
 70                 }
 71 
 72             }
 73         }
 74         else
 75         {
 76             //手上没东西的情况
 77             if (transform.childCount > 0)
 78             {
 79                 ItemUI currentItemUI = transform.GetChild(0).GetComponent<ItemUI>();
 80                 InventoryManager.Instance.PickupItem(currentItemUI.Item, currentItemUI.Amount);
 81                 Destroy(currentItemUI.gameObject);
 82                 isUpdateProperty = true;
 83             }
 84         }
 85         if (isUpdateProperty)
 86         {
 87             transform.parent.SendMessage("UpdatePropertyText");
 88         }
 89     }
 90 
 91     //判断item是否适合放在这个位置
 92     public bool IsRightItem(Item item)
 93     {
 94         if ((item is Equipment && ((Equipment)(item)).EquipType == this.equipType) ||
 95                     (item is Weapon && ((Weapon)(item)).WpType == this.wpType))
 96         {
 97             return true;
 98         }
 99         return false;
100     }
101 }
EquipmentSlot
 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEngine.EventSystems;
 4 
 5 /// <summary>
 6 /// 商店插槽
 7 /// </summary>
 8 public class VendorSlot:Slot {
 9 
10     /// <summary>
11     /// 重写鼠标按下
12     /// </summary>
13     public override void OnPointerDown(UnityEngine.EventSystems.PointerEventData eventData) {
14         //按下鼠标右键且鼠标上没有拾取物
15         if(eventData.button == PointerEventData.InputButton.Right && InventoryManager.Instance.IsPickedItem == false) {
16             //子物体数量大于0
17             if(transform.childCount > 0) {
18                 //获取当前物品
19                 Item currentItem = transform.GetChild(0).GetComponent<ItemUI>().Item;
20                 //
21                 transform.parent.parent.SendMessage("BuyItem",currentItem);
22             }
23         //按下鼠标左键且鼠标上有拾取物
24         } else if(eventData.button == PointerEventData.InputButton.Left && InventoryManager.Instance.IsPickedItem == true) {
25             transform.parent.parent.SendMessage("SellItem");
26         }
27 
28     }
29 }
VendorSlot

Other

 1 using UnityEngine;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 
 5 /// <summary>
 6 /// 配方
 7 /// </summary>
 8 public class Formula  {
 9 
10     /// <summary>
11     /// 物品1ID
12     /// </summary>
13     public int Item1ID { get;private set; }
14     /// <summary>
15     /// 物品1数量
16     /// </summary>
17     public int Item1Amount { get;private set; }
18     /// <summary>
19     /// 物品2ID
20     /// </summary>
21     public int Item2ID { get;private set; }
22     /// <summary>
23     /// 物品2数量
24     /// </summary>
25     public int Item2Amount { get;private set; }
26     
27     /// <summary>
28     /// 锻造结果的物品ID
29     /// </summary>
30     public int ResID { get;private set; }
31 
32     /// <summary>
33     /// 所需物品ID列表
34     /// </summary>
35     private List<int> needIdList = new List<int>();
36 
37     /// <summary>
38     /// 所需物品ID列表
39     /// </summary>
40     public List<int> NeedIdList
41     {
42         get
43         {
44             return needIdList;
45         }
46     }
47 
48     public Formula(int item1ID, int item1Amount, int item2ID, int item2Amount, int resID)
49     {
50         this.Item1ID = item1ID;
51         this.Item1Amount = item1Amount;
52         this.Item2ID = item2ID;
53         this.Item2Amount = item2Amount;
54         this.ResID = resID;
55 
56         for (int i = 0; i < Item1Amount; i++)
57         {
58             needIdList.Add(Item1ID);
59         }
60         for (int i = 0; i < Item2Amount; i++)
61         {
62             needIdList.Add(Item2ID);
63         }
64     }
65 
66     /// <summary>
67     /// 提供的物品ID是否匹配所需物品ID
68     /// </summary>
69     public bool Match(List<int> idList )
70     {
71         List<int> tempIDList = new List<int>(idList);
72         foreach(int id in needIdList){
73             bool isSuccess = tempIDList.Remove(id);
74             if (isSuccess == false)
75             {
76                 return false;
77             }
78         }
79         return true;
80     }
81 } 
Formula
  1 using UnityEngine;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using UnityEngine.UI;
  5 
  6 /// <summary>
  7 /// 管理
  8 /// </summary>
  9 public class InventoryManager:MonoBehaviour {
 10 
 11     /// <summary>
 12     /// 单例
 13     /// </summary>
 14     private static InventoryManager _instance;
 15 
 16     /// <summary>
 17     /// 单例
 18     /// </summary>
 19     public static InventoryManager Instance {
 20         get {
 21             if(_instance == null) {
 22                 _instance = GameObject.Find("InventoryManager").GetComponent<InventoryManager>();
 23             }
 24             return _instance;
 25         }
 26     }
 27 
 28     /// <summary>
 29     /// 物品列表
 30     /// </summary>
 31     private List<Item> itemList;
 32 
 33     /// <summary>
 34     /// 提示
 35     /// </summary>
 36     private ToolTip toolTip;
 37     /// <summary>
 38     /// 提示是否显示
 39     /// </summary>
 40     private bool isToolTipShow = false;
 41     /// <summary>
 42     /// 提示位置
 43     /// </summary>
 44     private Vector2 toolTipPosionOffset = new Vector2(10,-10);
 45 
 46     /// <summary>
 47     /// Canvas
 48     /// </summary>
 49     private Canvas canvas;
 50 
 51     /// <summary>
 52     /// 鼠标上是否有物品UI
 53     /// </summary>
 54     private bool isPickedItem = false;
 55 
 56     /// <summary>
 57     /// 鼠标上是否有物品UI
 58     /// </summary>
 59     public bool IsPickedItem {
 60         get {
 61             return isPickedItem;
 62         }
 63     }
 64 
 65     /// <summary>
 66     /// 鼠标上的物品UI
 67     /// </summary>
 68     private ItemUI pickedItemUI;
 69 
 70     /// <summary>
 71     /// 鼠标上的物品UI
 72     /// </summary>
 73     public ItemUI PickedItemUI {
 74         get {
 75             return pickedItemUI;
 76         }
 77     }
 78 
 79 
 80     void Awake() {
 81         ParseItemJson();
 82     }
 83 
 84     void Start() {
 85         toolTip = GameObject.FindObjectOfType<ToolTip>();
 86         canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
 87         pickedItemUI = GameObject.Find("PickedItem").GetComponent<ItemUI>();
 88         pickedItemUI.Hide();
 89     }
 90 
 91     void Update() {
 92         //鼠标上有拾取物品UI
 93         if(isPickedItem) {
 94             //让物品UI跟随鼠标位置
 95             Vector2 position;
 96             RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,Input.mousePosition,null,out position);
 97             pickedItemUI.SetLocalPosition(position);
 98         //显示提示
 99         } else if(isToolTipShow) {
100             //提示跟随鼠标位置
101             Vector2 position;
102             RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,Input.mousePosition,null,out position);
103             toolTip.SetLocalPotion(position + toolTipPosionOffset);
104         }
105 
106         //丢弃物品
107         if(isPickedItem && Input.GetMouseButtonDown(0) && UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(-1) == false) {
108             isPickedItem = false;
109             PickedItemUI.Hide();
110         }
111     }
112 
113     /// <summary>
114     /// 解析物品信息
115     /// </summary>
116     void ParseItemJson() {
117         itemList = new List<Item>();
118         //加载文本
119         TextAsset itemText = Resources.Load<TextAsset>("Items");
120         string itemsJson = itemText.text;
121         //用文本信息创建JSONObject
122         JSONObject j = new JSONObject(itemsJson);
123         //遍历json
124         foreach(JSONObject temp in j.list) {
125             //获取类型字符串
126             string typeStr = temp["type"].str;
127             //将字符串转换为枚举
128             Item.ItemType type = (Item.ItemType)System.Enum.Parse(typeof(Item.ItemType),typeStr);
129 
130             //转换共有属性
131             int id = (int)(temp["id"].n);
132             string name = temp["name"].str;
133             Item.ItemQuality quality = (Item.ItemQuality)System.Enum.Parse(typeof(Item.ItemQuality),temp["quality"].str);
134             string description = temp["description"].str;
135             int capacity = (int)(temp["capacity"].n);
136             int buyPrice = (int)(temp["buyPrice"].n);
137             int sellPrice = (int)(temp["sellPrice"].n);
138             string sprite = temp["sprite"].str;
139 
140             Item item = null;
141             switch(type) {
142             //消耗品
143             case Item.ItemType.Consumable:
144                 int hp = (int)(temp["hp"].n);
145                 int mp = (int)(temp["mp"].n);
146                 item = new Consumable(id,name,type,quality,description,capacity,buyPrice,sellPrice,sprite,hp,mp);
147                 break;
148             //装备
149             case Item.ItemType.Equipment:
150                 int strength = (int)temp["strength"].n;
151                 int intellect = (int)temp["intellect"].n;
152                 int agility = (int)temp["agility"].n;
153                 int stamina = (int)temp["stamina"].n;
154                 Equipment.EquipmentType equipType = (Equipment.EquipmentType)System.Enum.Parse(typeof(Equipment.EquipmentType),temp["equipType"].str);
155                 item = new Equipment(id,name,type,quality,description,capacity,buyPrice,sellPrice,sprite,strength,intellect,agility,stamina,equipType);
156                 break;
157             //武器
158             case Item.ItemType.Weapon:
159                 int damage = (int)temp["damage"].n;
160                 Weapon.WeaponType wpType = (Weapon.WeaponType)System.Enum.Parse(typeof(Weapon.WeaponType),temp["weaponType"].str);
161                 item = new Weapon(id,name,type,quality,description,capacity,buyPrice,sellPrice,sprite,damage,wpType);
162                 break;
163             //材料
164             case Item.ItemType.Material:
165                 item = new Material(id,name,type,quality,description,capacity,buyPrice,sellPrice,sprite);
166                 break;
167             }
168             itemList.Add(item);
169             //Debug.Log(item);
170         }
171     }
172 
173     /// <summary>
174     /// 通过ID获取物品
175     /// </summary>
176     public Item GetItemById(int id) {
177         foreach(Item item in itemList) {
178             if(item.ID == id) {
179                 return item;
180             }
181         }
182         return null;
183     }
184 
185     /// <summary>
186     /// 显示提示
187     /// </summary>
188     public void ShowToolTip(string content) {
189         if(this.isPickedItem) return;
190         isToolTipShow = true;
191         toolTip.Show(content);
192     }
193 
194     /// <summary>
195     /// 隐藏提示
196     /// </summary>
197     public void HideToolTip() {
198         isToolTipShow = false;
199         toolTip.Hide();
200     }
201 
202     /// <summary>
203     /// 鼠标拾取物品
204     /// </summary>
205     public void PickupItem(Item item,int amount) {
206         //设置物品和数量
207         PickedItemUI.SetItem(item,amount);
208         //修改标志位
209         isPickedItem = true;
210         //显示拾取物品
211         PickedItemUI.Show();
212         //隐藏提示文本
213         this.toolTip.Hide();
214 
215         //让物品UI跟随鼠标位置
216         Vector2 position;
217         RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,Input.mousePosition,null,out position);
218         pickedItemUI.SetLocalPosition(position);
219     }
220 
221     /// <summary>
222     /// 从手上拿掉一个物品放在物品槽里面
223     /// </summary>
224     public void RemoveItem(int amount = 1) {
225         //减少数量
226         PickedItemUI.ReduceAmount(amount);
227         //数量小于等于0
228         if(PickedItemUI.Amount <= 0) {
229             //修改标志位
230             isPickedItem = false;
231             //隐藏鼠标拾取物品UI
232             PickedItemUI.Hide();
233         }
234     }
235 
236     /// <summary>
237     /// 保存所有信息
238     /// </summary>
239     public void SaveInventory() {
240         KnapsackPanel.Instance.SaveInventory();
241         ChestPanel.Instance.SaveInventory();
242         CharacterPanel.Instance.SaveInventory();
243         ForgePanel.Instance.SaveInventory();
244         PlayerPrefs.SetInt("CoinAmount",GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().CoinAmount);
245     }
246 
247     /// <summary>
248     /// 加载所有信息
249     /// </summary>
250     public void LoadInventory() {
251         KnapsackPanel.Instance.LoadInventory();
252         ChestPanel.Instance.LoadInventory();
253         CharacterPanel.Instance.LoadInventory();
254         ForgePanel.Instance.LoadInventory();
255         if(PlayerPrefs.HasKey("CoinAmount")) {
256             GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().CoinAmount = PlayerPrefs.GetInt("CoinAmount");
257         }
258     }
259 
260 }
InventoryManager
  1 using UnityEngine;
  2 using System.Collections;
  3 using UnityEngine.UI;
  4 
  5 /// <summary>
  6 /// 玩家
  7 /// </summary>
  8 public class Player : MonoBehaviour
  9 {
 10 
 11     /// <summary>
 12     /// 基础力量
 13     /// </summary>
 14     private int basicStrength = 10;
 15     /// <summary>
 16     /// 基础智力
 17     /// </summary>
 18     private int basicIntellect = 10;
 19     /// <summary>
 20     /// 基础敏捷
 21     /// </summary>
 22     private int basicAgility = 10;
 23     /// <summary>
 24     /// 基础体力
 25     /// </summary>
 26     private int basicStamina = 10;
 27     /// <summary>
 28     /// 基础伤害
 29     /// </summary>
 30     private int basicDamage = 10;
 31 
 32     /// <summary>
 33     /// 基础力量
 34     /// </summary>
 35     public int BasicStrength
 36     {
 37         get
 38         {
 39             return basicStrength;
 40         }
 41     }
 42 
 43     /// <summary>
 44     /// 基础智力
 45     /// </summary>
 46     public int BasicIntellect
 47     {
 48         get
 49         {
 50             return basicIntellect;
 51         }
 52     }
 53 
 54     /// <summary>
 55     /// 基础敏捷
 56     /// </summary>
 57     public int BasicAgility
 58     {
 59         get
 60         {
 61             return basicAgility;
 62         }
 63     }
 64 
 65     /// <summary>
 66     /// 基础体力
 67     /// </summary>
 68     public int BasicStamina
 69     {
 70         get
 71         {
 72             return basicStamina;
 73         }
 74     }
 75 
 76     /// <summary>
 77     /// 基础伤害
 78     /// </summary>
 79     public int BasicDamage
 80     {
 81         get
 82         {
 83             return basicDamage;
 84         }
 85     }
 86 
 87     /// <summary>
 88     /// 金币数量
 89     /// </summary>
 90     private int coinAmount = 100;
 91 
 92     /// <summary>
 93     /// 金币数量文本
 94     /// </summary>
 95     private Text coinText;
 96 
 97     /// <summary>
 98     /// 金币数量
 99     /// </summary>
100     public int CoinAmount
101     {
102         get
103         {
104             return coinAmount;
105         }
106         set
107         {
108             coinAmount = value;
109             //同时更新文本
110             coinText.text = coinAmount.ToString();
111         }
112     }
113 
114     void Start()
115     {
116         //查找金币文本
117         coinText = GameObject.Find("Coin").GetComponentInChildren<Text>();
118         //更新金币文本
119         coinText.text = coinAmount.ToString();
120     }
121 
122     void Update () {
123         //G 随机得到一个物品放到背包里面
124         if (Input.GetKeyDown(KeyCode.G))
125         {
126             int id = Random.Range(1, 18);
127             KnapsackPanel.Instance.StoreItem(id);
128         }
129 
130         //T 控制背包的显示和隐藏
131         if (Input.GetKeyDown(KeyCode.T))
132         {
133             KnapsackPanel.Instance.DisplaySwitch();
134         }
135 
136         //Y 控制箱子的显示和隐藏
137         if (Input.GetKeyDown(KeyCode.Y))
138         {
139             ChestPanel.Instance.DisplaySwitch();
140         }
141         //U 控制角色面板的 显示和隐藏
142         if (Input.GetKeyDown(KeyCode.U))
143         {
144             CharacterPanel.Instance.DisplaySwitch();
145         }
146         //I 控制商店显示和隐藏 
147         if (Input.GetKeyDown(KeyCode.I))
148         {
149             VendorPanel.Instance.DisplaySwitch();
150         }
151         //O 控制锻造界面显示和隐藏 
152         if (Input.GetKeyDown(KeyCode.O))
153         {
154             ForgePanel.Instance.DisplaySwitch();
155         }
156     }
157 
158     /// <summary>
159     /// 花费金币
160     /// </summary>
161     public bool ConsumeCoin(int amount)
162     {
163         if (coinAmount >= amount)
164         {
165             coinAmount -= amount;
166             coinText.text = coinAmount.ToString();
167             return true;
168         }
169         return false;
170     }
171 
172     /// <summary>
173     /// 赚取金币
174     /// </summary>
175     public void EarnCoin(int amount)
176     {
177         this.coinAmount += amount;
178         coinText.text = coinAmount.ToString();
179     }
180 }
Player
 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEngine.UI;
 4 
 5 /// <summary>
 6 /// 提示
 7 /// </summary>
 8 public class ToolTip : MonoBehaviour {
 9 
10     /// <summary>
11     /// 提示文本
12     /// </summary>
13     private Text toolTipText;
14     /// <summary>
15     /// 内容文本
16     /// </summary>
17     private Text contentText;
18     /// <summary>
19     /// CanvasGroup
20     /// </summary>
21     private CanvasGroup canvasGroup;
22     /// <summary>
23     /// 目标透明度
24     /// </summary>
25     private float targetAlpha = 0 ;
26     /// <summary>
27     /// 动画速度
28     /// </summary>
29     public float smoothing = 1;
30 
31     void Start()
32     {
33         toolTipText = GetComponent<Text>();
34         contentText = transform.Find("Content").GetComponent<Text>();
35         canvasGroup = GetComponent<CanvasGroup>();
36     }
37 
38     void Update()
39     {
40         //缩放动画
41         if (canvasGroup.alpha != targetAlpha)
42         {
43             canvasGroup.alpha = Mathf.Lerp(canvasGroup.alpha, targetAlpha,smoothing*Time.deltaTime);
44             if (Mathf.Abs(canvasGroup.alpha - targetAlpha) < 0.01f)
45             {
46                 canvasGroup.alpha = targetAlpha;
47             }
48         }
49     }
50 
51     /// <summary>
52     /// 显示
53     /// </summary>
54     public void Show(string text)
55     {
56         toolTipText.text = text;
57         contentText.text = text;
58         targetAlpha = 1;
59     }
60     
61     /// <summary>
62     /// 隐藏
63     /// </summary>
64     public void Hide()
65     {
66         targetAlpha = 0;
67     }
68 
69     /// <summary>
70     /// 设置局部坐标
71     /// </summary>
72     public void SetLocalPotion(Vector3 position)
73     {
74         transform.localPosition = position;
75     }
76     
77 }
ToolTip

资源

  1 [
  2   {
  3     "id": 1,
  4     "name": "血瓶",
  5     "type": "Consumable",
  6     "quality": "Common",
  7     "description": "这个是用来加血的",
  8     "capacity": 10,
  9     "buyPrice": 10,
 10     "sellPrice": 5,
 11     "hp": 10,
 12     "mp": 0,
 13     "sprite": "Sprites/Items/hp"
 14   },
 15   {
 16     "id": 2,
 17     "name": "蓝瓶",
 18     "type": "Consumable",
 19     "quality": "Common",
 20     "description": "这个是用来加蓝的",
 21     "capacity": 10,
 22     "buyPrice": 10,
 23     "sellPrice": 5,
 24     "hp": 0,
 25     "mp": 10,
 26     "sprite": "Sprites/Items/mp"
 27   },
 28   {
 29     "id": 3,
 30     "name": "胸甲",
 31     "type": "Equipment",
 32     "quality": "Rare",
 33     "description": "这个胸甲很牛B",
 34     "capacity": 1,
 35     "buyPrice": 20,
 36     "sellPrice": 10,
 37     "sprite": "Sprites/Items/armor",
 38     "strength": 10,
 39     "intellect": 4,
 40     "agility": 9,
 41     "stamina": 1,
 42     "equipType": "Chest"
 43   },
 44   {
 45     "id": 4,
 46     "name": "皮腰带",
 47     "type": "Equipment",
 48     "quality": "Epic",
 49     "description": "这个腰带可以加速",
 50     "capacity": 1,
 51     "buyPrice": 20,
 52     "sellPrice": 10,
 53     "sprite": "Sprites/Items/belts",
 54     "strength": 1,
 55     "intellect": 6,
 56     "agility": 10,
 57     "stamina": 10,
 58     "equipType": "Belt"
 59   },
 60   {
 61     "id": 5,
 62     "name": "靴子",
 63     "type": "Equipment",
 64     "quality": "Legendary",
 65     "description": "这个靴子可以加速",
 66     "capacity": 1,
 67     "buyPrice": 20,
 68     "sellPrice": 10,
 69     "sprite": "Sprites/Items/boots",
 70     "strength": 10,
 71     "intellect": 5,
 72     "agility": 0,
 73     "stamina": 10,
 74     "equipType": "Boots"
 75   },
 76   {
 77     "id": 6,
 78     "name": "护腕",
 79     "type": "Equipment",
 80     "quality": "Rare",
 81     "description": "这个护腕可以增加防御",
 82     "capacity": 1,
 83     "buyPrice": 20,
 84     "sellPrice": 10,
 85     "sprite": "Sprites/Items/bracers",
 86     "strength": 1,
 87     "intellect": 2,
 88     "agility": 3,
 89     "stamina": 4,
 90     "equipType": "Bracer"
 91   },
 92   {
 93     "id": 7,
 94     "name": "神启手套",
 95     "type": "Equipment",
 96     "quality": "Common",
 97     "description": "很厉害的手套",
 98     "capacity": 1,
 99     "buyPrice": 10,
100     "sellPrice": 5,
101     "sprite": "Sprites/Items/gloves",
102     "strength": 2,
103     "intellect": 3,
104     "agility": 4,
105     "stamina": 4,
106     "equipType": "OffHand"
107   },
108   {
109     "id": 8,
110     "name": "头盔",
111     "type": "Equipment",
112     "quality": "Artifact",
113     "description": "很厉害的头盔",
114     "capacity": 1,
115     "buyPrice": 10,
116     "sellPrice": 5,
117     "sprite": "Sprites/Items/helmets",
118     "strength": 2,
119     "intellect": 3,
120     "agility": 4,
121     "stamina": 4,
122     "equipType": "Head"
123   },
124   {
125     "id": 9,
126     "name": "项链",
127     "type": "Equipment",
128     "quality": "Rare",
129     "description": "很厉害的项链",
130     "capacity": 1,
131     "buyPrice": 10,
132     "sellPrice": 5,
133     "sprite": "Sprites/Items/necklace",
134     "strength": 2,
135     "intellect": 3,
136     "agility": 4,
137     "stamina": 4,
138     "equipType": "Neck"
139   },
140   {
141     "id": 10,
142     "name": "戒指",
143     "type": "Equipment",
144     "quality": "Common",
145     "description": "很厉害的戒指",
146     "capacity": 1,
147     "buyPrice": 20,
148     "sellPrice": 20,
149     "sprite": "Sprites/Items/rings",
150     "strength": 20,
151     "intellect": 3,
152     "agility": 4,
153     "stamina": 4,
154     "equipType": "Ring"
155   },
156   {
157     "id": 11,
158     "name": "裤子",
159     "type": "Equipment",
160     "quality": "Uncommon",
161     "description": "很厉害的裤子",
162     "capacity": 1,
163     "buyPrice": 40,
164     "sellPrice": 20,
165     "sprite": "Sprites/Items/pants",
166     "strength": 20,
167     "intellect": 30,
168     "agility": 40,
169     "stamina": 40,
170     "equipType": "Leg"
171   },
172   {
173     "id": 12,
174     "name": "护肩",
175     "type": "Equipment",
176     "quality": "Legendary",
177     "description": "很厉害的护肩",
178     "capacity": 1,
179     "buyPrice": 100,
180     "sellPrice": 20,
181     "sprite": "Sprites/Items/shoulders",
182     "strength": 2,
183     "intellect": 3,
184     "agility": 4,
185     "stamina": 4,
186     "equipType": "Shoulder"
187   },
188   {
189     "id": 13,
190     "name": "开天斧",
191     "type": "Weapon",
192     "quality": "Rare",
193     "description": "渔翁移山用的斧子",
194     "capacity": 1,
195     "buyPrice": 50,
196     "sellPrice": 20,
197     "sprite": "Sprites/Items/axe",
198     "damage": 100,
199     "weaponType": "MainHand"
200   },
201   {
202     "id": 14,
203     "name": "阴阳剑",
204     "type": "Weapon",
205     "quality": "Rare",
206     "description": "非常厉害的剑",
207     "capacity": 1,
208     "buyPrice": 15,
209     "sellPrice": 5,
210     "sprite": "Sprites/Items/sword",
211     "damage": 20,
212     "weaponType": "OffHand"
213   },
214   {
215     "id": 15,
216     "name": "开天斧的锻造秘籍",
217     "type": "Material",
218     "quality": "Artifact",
219     "description": "用来锻造开天斧的秘籍",
220     "capacity": 2,
221     "buyPrice": 100,
222     "sellPrice": 99,
223     "sprite": "Sprites/Items/book"
224   },
225   {
226     "id": 16,
227     "name": "头盔的锻造秘籍",
228     "type": "Material",
229     "quality": "Common",
230     "description": "用来锻造头盔的秘籍",
231     "capacity": 2,
232     "buyPrice": 50,
233     "sellPrice": 10,
234     "sprite": "Sprites/Items/scroll"
235   },
236   {
237     "id": 17,
238     "name": "铁块",
239     "type": "Material",
240     "quality": "Common",
241     "description": "用来锻造其他东西的必备材料",
242     "capacity": 20,
243     "buyPrice": 5,
244     "sellPrice": 4,
245     "sprite": "Sprites/Items/ingots"
246   }
247 ]
Items.Json
 1 [
 2   {
 3     "Item1ID": 15,
 4     "Item1Amount": 1,
 5     "Item2ID": 17,
 6     "Item2Amount": 2,
 7     "ResID": 13
 8   },
 9   {
10     "Item1ID": 16,
11     "Item1Amount": 1,
12     "Item2ID": 17,
13     "Item2Amount": 5,
14     "ResID": 8
15   }
16 ]
Formulas.Json

项目:https://pan.baidu.com/s/1gf86aY3

 

转载于:https://www.cnblogs.com/revoid/p/6593025.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值