“KnapsackManager”脚本的具体内容如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KnapsackManager : MonoBehaviour {
public GridPanel gridPanel;
private Dictionary itemList;
private static KnapsackManager instance;
public static KnapsackManager Instance {
get {
return instance;
}
}
void Awake() {
instance = this;
this.LoadData();
}
public void StoreItem(int itemId) {
if (!itemList.ContainsKey(itemId)) {
return;
}
Transform emptyGrid = gridPanel.GetEmptyGrid();
if (emptyGrid == null) {
Debug.Log("背包已满");
return;
}
Item item = itemList[itemId];
GameObject itemPrefab = Resources.Load("Prefabs/ItemImage");
itemPrefab.GetComponent().UpdateItem(item.Name);
GameObject itemGo = GameObject.Instantiate(itemPrefab);
itemGo.transform.SetParent(emptyGrid);
itemGo.transform.localPosition = Vector3.zero;
itemGo.transform.localScale = Vector3.one;
}
//模拟数据库数据加载
private void LoadData() {
itemList = new Dictionary();
Weapon w1 = new Weapon(0, "牛刀", "牛B的刀", 20, 10, "", 100);
Weapon w2 = new Weapon(1, "羊刀", "杀羊刀。", 15, 10, "", 20);
Weapon w3 = new Weapon(2, "宝剑", "大宝剑!", 120, 50, "", 500);
Weapon w4 = new Weapon(3, "军枪", "可以对敌人射击,很厉害的一把枪。", 1500, 125, "", 720);
Consumable c1 = new Consumable(4, "红瓶", "加血", 25, 11, "", 20, 0);
Consumable c2 = new Consumable(5, "蓝瓶", "加蓝", 39, 19, "", 0, 20);
Armor a1 = new Armor(6, "头盔", "保护脑袋!", 128, 83, "", 5, 40, 1);
Armor a2 = new Armor(7, "护肩", "上古护肩,锈迹斑斑。", 1000, 0, "", 15, 40, 11);
Armor a3 = new Armor(8, "胸甲", "皇上御赐胸甲。", 153, 0, "", 25, 30, 11);
Armor a4 = new Armor(9, "护腿", "预防风寒,从腿做起", 999, 60, "", 19, 30, 51);
itemList.Add(w1.Id, w1);
itemList.Add(w2.Id, w2);
itemList.Add(w3.Id, w3);
itemList.Add(w4.Id, w4);
itemList.Add(c1.Id, c1);
itemList.Add(c2.Id, c2);
itemList.Add(a1.Id, a1);
itemList.Add(a2.Id, a2);
itemList.Add(a3.Id, a3);
itemList.Add(a4.Id, a4);
}
}