コード例 #1
0
ファイル: BagModel.cs プロジェクト: songqinhan/Unity-Bag
 public override void ClearItem()
 {
     base.ClearItem();
     this.Image   = null;
     this.Obj     = null;
     this.ItemCtl = null;
 }
コード例 #2
0
ファイル: BagModel.cs プロジェクト: songqinhan/Unity-Bag
 public BagItemInfo(int id, string name, Sprite sprite, int count, EquipType type, GameObject obj, string prefabPath, float gain) : base(id, name, count, type, gain, "", prefabPath)
 {
     this.Obj   = obj;
     this.Image = sprite;
     ItemCtl    = Obj.GetComponent <BagItemController>();
     if (ItemCtl == null)
     {
         Debug.LogError("创建BagItemInfo失败,传入GameObject没有BagItemController组件");
     }
 }
コード例 #3
0
ファイル: BagModel.cs プロジェクト: songqinhan/Unity-Bag
 public BagItemInfo(EquipListItem it, BagController ctl)
 {
     GameObject.Destroy(it.obj);
     Obj     = ResManager.Instance.LoadPrefabFromRes("Prefab/Bagitem", true);
     Image   = it.sprite;
     ItemCtl = Obj.GetComponent <BagItemController>();
     ItemCtl.SetController(ctl);
     this.id       = it.id;
     this.name     = it.name;
     this.type     = it.type;
     this.count    = it.count;
     this.gain     = it.gain;
     this.discribe = it.discribe;
 }
コード例 #4
0
ファイル: BagModel.cs プロジェクト: songqinhan/Unity-Bag
 //拷贝构造函数
 public BagItemInfo(BagItemInfo it) : base(it)
 {
     this.Image   = it.Image;
     this.Obj     = it.Obj;
     this.ItemCtl = it.ItemCtl;
 }
コード例 #5
0
ファイル: BagController.cs プロジェクト: songqinhan/Unity-Bag
        //添加item
        public bool AddItem(BaseItem it)
        {
            int id             = it.id;
            int itemNum        = m_bagModel.BagItemsDic.Count;
            int firstEmptySlot = -1;
            //背包中已经有这个id的物品
            var enumerator = m_bagModel.BagItemsDic.GetEnumerator();

            while (enumerator.MoveNext())
            {
                if (m_bagModel.BagItemsDic[enumerator.Current.Key].id == id &&
                    (it.type == EquipType.bullet_556 || it.type == EquipType.medicine_s))
                {
                    //只有药品和子弹的数量可以叠加
                    if (it.type == EquipType.bullet_556)
                    {
                        enumerator.Current.Value.count += it.count;
                        ChangeItemNum(enumerator.Current.Value, enumerator.Current.Value.count);
                        Debug.Log(it.count);
                        ChangeBulletCount(EquipType.bullet_556, it.count);
                        return(true);
                    }
                    else if (it.type == EquipType.medicine_s)
                    {
                        enumerator.Current.Value.count += it.count;
                        ChangeItemNum(enumerator.Current.Value, enumerator.Current.Value.count);
                        ChangeMedicineCount(EquipType.medicine_s, it.count);
                        return(true);
                    }
                }
            }
            //背包中没有这个物品,遍历背包槽,找到第一个空槽
            for (int i = 0; i < m_bagView.m_slots.Count; i++)
            {
                //Debug.Log(i + " : " + m_bagView.m_slots[i].isEmpty);
                if (m_bagView.m_slots[i].isEmpty == true)
                {
                    firstEmptySlot = i;
                    break;
                }
            }
            //背包还有空位
            if (firstEmptySlot != -1)
            {
                //更新背包存放item的字典
                //实例化一个item
                GameObject obj    = ResManager.Instance.LoadPrefabFromRes("Prefab/BagItem", true);
                Sprite     sprite = ResManager.Instance.LoadSpriteFromRes(it.imageName);
                //初始化item,加载名字,数量,图片
                Transform numberObj = obj.transform.GetChild(0);
                Text      number    = numberObj.GetComponent <Text>();
                number.text = it.count.ToString();

                Image img = obj.transform.GetChild(1).GetComponent <Image>();
                img.sprite = sprite;

                Text name = obj.transform.GetChild(2).GetComponent <Text>();
                name.text = it.name;
                //赋值id,传递bagController
                BagItemController itemView = obj.GetComponent <BagItemController>();
                itemView.SetController(this);
                itemView.SetDicKey(firstEmptySlot);
                //实例化一个BagItemInfo
                BagItemInfo itInfo = new BagItemInfo(id, it.name, sprite, it.count, it.type, obj, it.ScenePrefabPath, it.gain);
                //挂载item到空槽中
                MountPrefabToSlot(itInfo, firstEmptySlot);
            }
            return(true);
        }