コード例 #1
0
ファイル: BagController.cs プロジェクト: songqinhan/Unity-Bag
        //交换item
        public void ChangeItem(Transform parentSlot, Transform item, GameObject drop, int itemId)
        {
            string tag          = parentSlot.tag;
            int    parentSlotId = parentSlot.GetComponent <BagGrIdIns>().id;
            int    dropSlotId   = drop.transform.parent.GetComponent <BagGrIdIns>().id;
            //获取交换对象的id
            int dropId = drop.transform.GetComponent <BagItemController>().DicKey;
            //拷贝这一个槽的item数值
            BagItemInfo aim_It = new BagItemInfo(m_bagModel.BagItemsDic[dropId]);

            //如果是从背包栏中拖拽过来的
            if (tag == "Grid")
            {
                //拷贝上一个槽的item数值
                BagItemInfo drop_It = new BagItemInfo(m_bagModel.BagItemsDic[itemId]);
                //直接交换两个槽的item
                MountPrefabToSlot(drop_It, dropSlotId);
                MountPrefabToSlot(aim_It, parentSlotId);
            }
            //从装备栏中拖过来的,不进行交换,直接添加入背包
            else if (tag == "EquipSlot")
            {
                int gunId = parentSlot.GetComponent <BagGrIdIns>().parentId;
                //拷贝上一个槽的item数值
                BagItemInfo drop_It = new BagItemInfo(m_equipModel.gunItems[gunId].equipItems[itemId]);
                //更新上一个槽所属的枪械的数值
                ChangeGunInfo(gunId, drop_It.type, -drop_It.gain);
                //背包添加此item
                AddItem(drop_It, -1);
                //删除装备槽的item
                bool res = m_equipModel.RemoveEquip(gunId, itemId);
                Debug.Log(res);
            }
        }
コード例 #2
0
ファイル: BagController.cs プロジェクト: songqinhan/Unity-Bag
 //更改item的形态
 private void ChangeItemSize(BagItemInfo itemInfo, ItemSize size)
 {
     if (size == ItemSize.bagSize)
     {
         //转为背包形态
         //itemInfo.Obj.transform.localScale = new Vector3(1, 1, 1);
         itemInfo.Obj.GetComponent <Image>().sprite = null;
         //将子物体设置为不可见
         for (int i = 0; i < itemInfo.Obj.transform.childCount; i++)
         {
             itemInfo.Obj.transform.GetChild(i).gameObject.SetActive(true);
         }
     }
     else if (size == ItemSize.equipSize)
     {
         //转为装备形态
         //itemInfo.Obj.transform.localScale = new Vector3(0.28f, 0.64f, 1);
         itemInfo.Obj.GetComponent <Image>().sprite = itemInfo.Image;
         //将子物体设置为不可见
         for (int i = 0; i < itemInfo.Obj.transform.childCount; i++)
         {
             itemInfo.Obj.transform.GetChild(i).gameObject.SetActive(false);
         }
     }
 }
コード例 #3
0
ファイル: BagController.cs プロジェクト: songqinhan/Unity-Bag
        /// <summary>
        /// 从装备栏挂载到背包栏
        /// </summary>
        /// <param name="gun"></param>
        /// <param name="itemId">装备槽的id</param>
        private void EquipItemToBagItem(GunItem gun, int itemId, bool flag = true)
        {
            //拷贝上一个槽的item数值
            BagItemInfo tempItem = new BagItemInfo(gun.equipItems[itemId]);

            //挂载item到新的槽(其实是将上一个槽的数值赋给新的槽)
            AddItem(tempItem, 0);
            //清空上一个槽的数值
            if (flag == true)
            {
                m_equipModel.RemoveEquip(gun, itemId);
            }
        }
コード例 #4
0
ファイル: BagController.cs プロジェクト: songqinhan/Unity-Bag
        //从背包拖去背包,从装备栏拖去背包
        public void DropToEmptySlot(Transform parentSlot, Transform item, GameObject dropSlot, int itemId)
        {
            string tag        = parentSlot.tag;
            int    parentId   = parentSlot.GetComponent <BagGrIdIns>().id;
            int    dropSlotId = dropSlot.GetComponent <BagGrIdIns>().id;

            //如果是从背包栏中托过来的
            if (tag == "Grid")
            {
                //取出上一个槽的item数值引用
                BagItemInfo last_It = m_bagModel.BagItemsDic[itemId];
                //拷贝上一个槽的item数值
                //BagItemInfo temp_It = new BagItemInfo(last_It);
                //Debug.Log("temp_It的id:"+temp_It.id);
                //挂载item到新的槽(其实是将上一个槽的数值赋给新的槽)
                MountPrefabToSlot(last_It, dropSlotId);
                //清空上一个槽的数值
                m_bagModel.BagItemsDic.Remove(parentId);
                m_bagView.m_slots[parentId].isEmpty = true;
            }
            //如果是从装备栏拖过去的
            else if (tag == "EquipSlot")
            {
                //得到该装备槽对应的枪
                int gunId = parentSlot.GetComponent <BagGrIdIns>().parentId;
                //获取
                if (m_equipModel.gunItems.TryGetValue(gunId, out GunItem gun))
                {
                    //更新枪械数值
                    ChangeGunInfo(gunId, gun.equipItems[itemId].type, -gun.equipItems[itemId].gain);
                    //执行挂载与解挂
                    EquipItemToBagItem(gun, itemId);

                    //拷贝上一个槽的item数值
                    //BagItemInfo tempItem = new BagItemInfo(gun.equipItems[itemId]);
                    //挂载item到新的槽(其实是将上一个槽的数值赋给新的槽)
                    //AddItem(tempItem, dropSlotId);
                    //清空上一个槽的数值
                    //m_equipModel.RemoveEquip(gun, itemId);
                }
            }
        }
コード例 #5
0
ファイル: BagController.cs プロジェクト: songqinhan/Unity-Bag
        //将一个实例化后的物体挂载在指定的槽中
        public void MountPrefabToSlot(BagItemInfo it, int slotNum)
        {
            //更新bagItemCtl的id值
            it.ItemCtl.DicKey = slotNum;
            //添加入itemDic中
            if (m_bagModel.BagItemsDic.TryGetValue(slotNum, out BagItemInfo gun))
            {
                m_bagModel.BagItemsDic[slotNum] = it;
            }
            else
            {
                m_bagModel.BagItemsDic.Add(slotNum, it);
            }
            //挂载item到空槽中
            it.Obj.transform.SetParent(m_bagView.m_slots[slotNum].slotObj.transform, false);

            it.Obj.transform.localPosition = Vector3.zero;
            //更新该槽的数据
            m_bagView.SetSlotEmptyOrNot(slotNum, false);
        }
コード例 #6
0
ファイル: BagController.cs プロジェクト: songqinhan/Unity-Bag
        //从装备栏拖入背包统一使用此函数
        private bool AddItem(BagItemInfo itInfo, int slotNum)
        {
            int id             = itInfo.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 &&
                    (itInfo.type == EquipType.bullet_556 || itInfo.type == EquipType.medicine_s))
                {
                    //只有药品和子弹的数量可以叠加
                    if (itInfo.type == EquipType.bullet_556 || itInfo.type == EquipType.medicine_s)
                    {
                        enumerator.Current.Value.count += itInfo.count;
                        ChangeItemNum(enumerator.Current.Value, enumerator.Current.Value.count);
                        return(true);
                    }
                }
            }
            //背包中没有这个物品,遍历背包槽,找到第一个空槽
            for (int i = 0; i < m_bagView.m_slots.Count; i++)
            {
                if (m_bagView.m_slots[i].isEmpty == true)
                {
                    firstEmptySlot = i;
                    break;
                }
            }
            //背包中没有这个物品,将该物体挂载在槽上
            MountPrefabToSlot(itInfo, firstEmptySlot);
            Debug.Log("挂载完成");
            //改变item形态
            ChangeItemSize(itInfo, ItemSize.bagSize);
            return(true);
        }
コード例 #7
0
ファイル: BagModel.cs プロジェクト: songqinhan/Unity-Bag
 //拷贝构造函数
 public BagItemInfo(BagItemInfo it) : base(it)
 {
     this.Image   = it.Image;
     this.Obj     = it.Obj;
     this.ItemCtl = it.ItemCtl;
 }
コード例 #8
0
ファイル: BagController.cs プロジェクト: songqinhan/Unity-Bag
        //更改界面item的表现
        public void ChangeItemNum(BagItemInfo it, int num)
        {
            Text tx = it.Obj.transform.GetChild(0).GetComponent <Text>();

            tx.text = num.ToString();
        }
コード例 #9
0
ファイル: BagController.cs プロジェクト: songqinhan/Unity-Bag
        //从背包、另一把枪的装备栏 拖去装备栏
        public bool DropToEquipSlot(Transform parentSlot, Transform item, GameObject dropSlot, int dicKey)
        {
            string tag        = parentSlot.tag;
            int    parentId   = parentSlot.GetComponent <BagGrIdIns>().id;
            int    dropSlotId = dropSlot.GetComponent <BagGrIdIns>().id;
            //得到该装备槽对应的枪
            int gunId = dropSlot.GetComponent <BagGrIdIns>().parentId;

            Debug.Log("parentId: " + parentId);
            //如果是从背包栏中托过来的
            if (tag == "Grid")
            {
                //取出上一个槽的item数值引用
                BagItemInfo last_It = m_bagModel.BagItemsDic[dicKey];
                //拷贝上一个槽的item数值
                BagItemInfo temp_It = new BagItemInfo(last_It);
                //挂载item到新的槽(其实是将上一个槽的数值赋给新的槽)
                if (m_equipModel.gunItems.TryGetValue(gunId, out GunItem gun))
                {
                    //确认拖入的装备类型和槽是否对应
                    if (gun.equipSlots[dropSlotId].slotType != temp_It.type)
                    {
                        Debug.Log("类型不对应: " + gun.equipSlots[dropSlotId].slotType + temp_It.type);
                        return(false);
                    }
                    //将item加入该枪的装备字典中
                    gun.equipItems.Add(dropSlotId, temp_It);
                    temp_It.ItemCtl.DicKey = dropSlotId;
                    //改变位置,物理挂载
                    temp_It.Obj.transform.SetParent(gun.equipSlots[dropSlotId].slotTrans, false);
                    temp_It.Obj.transform.localPosition = Vector3.zero;
                    Debug.Log("开始改变枪支数值");
                    ChangeItemSize(temp_It, ItemSize.equipSize);
                    //改变枪支数值
                    ChangeGunInfo(gunId, temp_It.type, temp_It.gain);
                    //清空上一个槽的数值
                    m_bagModel.BagItemsDic.Remove(dicKey);
                    m_bagView.m_slots[dicKey].isEmpty = true;
                    Debug.Log("完成");
                }
                else
                {
                    Debug.Log("没找到枪");
                }
            }
            else if (tag == "EquipSlot")
            {
                int slotGunId = parentSlot.GetComponent <BagGrIdIns>().parentId;
                //获取
                if (m_equipModel.gunItems.TryGetValue(slotGunId, out GunItem gun))
                {
                    //拷贝上一个槽的item数值
                    BagItemInfo lastItem = new BagItemInfo(gun.equipItems[dicKey]);
                    //确认拖入的装备类型和槽是否对应
                    if (gun.equipSlots[dropSlotId].slotType != lastItem.type)
                    {
                        Debug.Log("类型不对应: " + gun.equipSlots[dropSlotId].slotType + lastItem.type);
                        return(false);
                    }
                    //获取dropSlot有关信息
                    int     dropGunId = dropSlot.GetComponent <BagGrIdIns>().parentId;
                    GunItem dropGun   = m_equipModel.gunItems[dropGunId];
                    //挂载
                    lastItem.Obj.transform.SetParent(dropGun.equipSlots[dropSlotId].slotTrans, false);
                    lastItem.Obj.transform.localPosition = Vector3.zero;
                    dropGun.equipItems.Add(dropSlotId, lastItem);
                    lastItem.ItemCtl.DicKey = dropSlotId;
                    //改变上一把枪支数值
                    ChangeGunInfo(slotGunId, lastItem.type, -lastItem.gain);
                    //改变下一把枪的数值
                    ChangeGunInfo(dropGunId, dropGun.type, lastItem.gain);
                    //清空上一个槽的数值
                    gun.equipItems.Remove(parentId);
                }
            }
            return(true);
        }
コード例 #10
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);
        }