BZ易风

导航

 

DragDropItem:拖拽事件

OnDragDropMove(Vector3 delta):拖拽移动时会调用

OnDragDropStart(): 拖拽开始时调用

OnDragDropRelease(GameObject surface): 拖拽结束时调用   surface: 放在哪个游戏物体上  该游戏物体需要有BoxCollider 不然检测不到就会提示Null

背包案例

1.创建背包界面 UIBagPanel

2.创建背包背景BagBg, 添加BoxCollider和DragObject脚本使背包窗口可以拖动, 添加Grid组件,使背包的格子自动布局

3.创建背包格子预制体CellItem,tag改为cell,添加BoxCollider为了方便拖拽事件检测

4.创建物品预制体EquipItem, tag改为item, 添加BoxCollider和自写脚本DragItem, 继承自DragDropItem拖拽事件的脚本

DragItem.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragItem : UIDragDropItem      //继承UIDragDropItem 
{
    public UISprite icon;       //图片
    public UILabel numText;     //数量文本
    private int count = 1;      //默认数量

    public void AddCount(int num=1)     //添加物品 增加数量
    {
        count += num;
        numText.text = count.ToString();
    }

    private void Start()
    {
        icon = GetComponent<UISprite>();
        numText = transform.GetComponentInChildren<UILabel>();
        numText.text = count.ToString();
    }
    protected override void OnDragDropRelease(GameObject surface)       //重写虚函数 拖拽放下 写我们需要的逻辑
    {
        base.OnDragDropRelease(surface);
        print("tag是:"+surface.tag);
        if(surface.tag == "cell")               //如果放下的物品是cell则放到格子内
        {

            transform.SetParent(surface.transform);
            transform.localPosition = Vector3.zero;
        }
        else if (surface.tag == "item")                 //如果检测到放到的物品的tag是item 则交换
        {
            Transform tmpParent = surface.transform.parent;
            surface.transform.parent = transform.parent;
            surface.transform.localPosition = Vector3.zero;
            transform.parent = tmpParent;
            transform.localPosition = Vector3.zero;
        }
    }
}

5.写背包界面的逻辑UIBagPanel.cs挂载再UIBagPanel上

UIBagPanel.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIBagPanel : MonoBehaviour
{
    private Transform bagTrans;             //背包
    private GameObject cell;                //格子
    private List<GameObject> cells = new List<GameObject>(); //格子列表
    private GameObject equipItem;                           //装备Item
    private List<DragItem> equips = new List<DragItem>(); 
    private UIGrid grid;
    private string[] equipName = {"Orc Armor - Boots",
                                    "Orc Armor - Bracers",
                                    "Orc Armor - Shoulders"
                                    };                      //装备名数组

    private int widthMax;       //最大列数
    private int heightMax;      //最大行数

    private void Awake()
    {
        bagTrans = transform.Find("BagBg");                 //获取背包
        cell = transform.Find("CellItem").gameObject;       //获取格子
        equipItem = transform.Find("EquipItem").gameObject; //获取装备Item
        cell.SetActive(false);                              //隐藏格子
        equipItem.SetActive(false);                         //隐藏装备
        grid = bagTrans.GetComponent<UIGrid>();
    }

    private void Start()
    {
        widthMax = (int)(bagTrans.GetComponent<UISprite>().localSize.x / cell.GetComponent<UISprite>().localSize.x);      //根据背包宽度/格子宽度 获取最大列数
        heightMax = (int)(bagTrans.GetComponent<UISprite>().localSize.y / cell.GetComponent<UISprite>().localSize.y);     //根据背包高度/格子高度 获取最大行数
        Init();
    }

    public void Init()
    {
        for (int i = 0; i < widthMax * heightMax; i++)          //最大列数*最大行数 = 总格子数
        {

            GameObject go = Resources.Load<GameObject>("Prefabs/CellItem");     //加载格子预制体
            cell = Instantiate(go, bagTrans);                                   //克隆到背包下
            cell.name = "cell_" + i.ToString("D2");            // ToString("D2") 十进位数两位
            //cell.name = "cell";                                                 //改名为cell
            cells.Add(cell);                                                    //添加到格子列表中
            cells[i].SetActive(true);                                           //激活
        }
        grid.enabled = true;
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.X))
        {
            PickUp();
        }
    }
    public void PickUp()
    {
        GameObject item = Resources.Load<GameObject>("Prefabs/equipItem");      //加载装备
        string name = equipName[Random.Range(0, equipName.Length)];             //随机获取一件装备
        bool isFind = false;            //是否找到
        
      

        for (int i = 0; i < cells.Count; i++)                                   //遍历格子总数
        {
            if (cells[i].transform.childCount>0)                               //判断该格子下方是否有子物体,如果有 则表示不为空,该格子有物品 反之则无 那么获取的物品放到该格子内
            {
                //如果有 判断当前物体的名字
                DragItem equipItem = cells[i].GetComponentInChildren<DragItem>();  //获取子物体的DragItem组件
                //判断当前物品的名字跟捡到的物体名字是否一样
                if (equipItem.icon.spriteName == name)
                {
                    isFind = true;
                    equipItem.AddCount(1);
                    break;
                }
            }
           
            
        }
        if(!isFind)
        {
            for (int i = 0; i < cells.Count; i++)                                   //遍历格子总数
            {
                if(cells[i].transform.childCount == 0)
                {
                    //添加物品
                    GameObject obj = NGUITools.AddChild(cells[i], item);            //NGUITool.AddChild(父物体,要放的物体) 往指定物体下面添加物体
                                                                                    //GameObject obj = Instantiate(go, cells[i].transform);
                    obj.GetComponent<UISprite>().spriteName = name;                 //换图片
                    obj.transform.localPosition = Vector3.zero;                     //位置归零
                    break;
                }
            }
        }

    }

}

 

posted on 2021-11-07 12:14  BZ易风  阅读(370)  评论(0编辑  收藏  举报