unity 背包选中_Unity UGUI背包系统(三) 之 实现物品存储到背包

“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);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值