unity简单的背包制作

背包系统基本功能:添加物品、删除物品、拖拽物品、交换物品位置、排列物品等等。

首先在unity将背包的UI界面制作完成,如图:

 Text是用来显示当前格子物品的数量。

然后将需要生成的物品UI制作成预制物,如图所示:

 接下来上代码

1、添加物品

添加物品的时候需要注意的是(1)当前格子是否为空(2)当前格子是否有跟需要添加相同的物体(3)格子为空的时候需要按照空格子的顺序添加物体(4)当前格子有需要添加的物体时,只需要数量++

    /// <summary>
    /// 添加物品
    /// </summary>
    /// <param name="objnum"></param>
    void LoadObjs(int objnum)
    {
        switch (objnum)
        {
            case 101: //red
                GameObject objred = GameObject.Find("red");
                if (objred != null)
                {
                    int num = int.Parse(objred.transform.parent.GetChild(0).GetComponent<Text>().text);
                    num++;
                    objred.transform.parent.GetChild(0).GetComponent<Text>().text = num.ToString();
                    return;
                }

                for (int i = 0; i < bagBakcGround.transform.childCount; i++)
                {
                    if (bagBakcGround.transform.GetChild(i).childCount <= 1)  //格子为空   创建新物体
                    {
                        GameObject go = Instantiate(Resources.Load<GameObject>("objs/red"));
                        go.transform.parent = bagBakcGround.transform.GetChild(i);
                        go.transform.localPosition = new Vector3(0, 0, 0);
                        go.transform.name = "red";
                        bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text = "1";
                        break;
                    }
                    //else  //格子不为空  添加物体数量
                    //{
                    //    if (bagBakcGround.transform.GetChild(i).GetChild(1).name == "red")
                    //    {
                    //        int num = int.Parse(bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text);
                    //        num++;
                    //        bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text = num.ToString();
                    //        return;
                    //    }
                    //}
                }
                break;
            case 102:  //yellow
                GameObject objyellow = GameObject.Find("yellow");
                if (objyellow != null)
                {
                    int num = int.Parse(objyellow.transform.parent.GetChild(0).GetComponent<Text>().text);
                    num++;
                    objyellow.transform.parent.GetChild(0).GetComponent<Text>().text = num.ToString();
                    return;
                }

                for (int i = 0; i < bagBakcGround.transform.childCount; i++)
                {
                    if (bagBakcGround.transform.GetChild(i).childCount <= 1)  //格子为空   创建新物体
                    {
                        GameObject go = Instantiate(Resources.Load<GameObject>("objs/yellow"));
                        go.transform.parent = bagBakcGround.transform.GetChild(i);
                        go.transform.localPosition = new Vector3(0, 0, 0);
                        go.transform.name = "yellow";
                        bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text = "1";
                        break;
                    }
                    //else  //格子不为空  添加物体数量
                    //{
                    //    if (bagBakcGround.transform.GetChild(i).GetChild(1).name == "yellow")
                    //    {
                    //        int num = int.Parse(bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text);
                    //        num++;
                    //        bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text = num.ToString();
                    //        return;
                    //    }
                    //}
                }
                break;
            case 103:  //black
                GameObject objblack = GameObject.Find("black");
                if (objblack != null)
                {
                    int num = int.Parse(objblack.transform.parent.GetChild(0).GetComponent<Text>().text);
                    num++;
                    objblack.transform.parent.GetChild(0).GetComponent<Text>().text = num.ToString();
                    return;
                }

                for (int i = 0; i < bagBakcGround.transform.childCount; i++)
                {
                    if (bagBakcGround.transform.GetChild(i).childCount <= 1)  //格子为空   创建新物体
                    {
                        GameObject go = Instantiate(Resources.Load<GameObject>("objs/black"));
                        go.transform.parent = bagBakcGround.transform.GetChild(i);
                        go.transform.localPosition = new Vector3(0, 0, 0);
                        go.transform.name = "black";
                        bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text = "1";
                        break;
                    }
                    //else  //格子不为空  添加物体数量
                    //{
                    //    if (bagBakcGround.transform.GetChild(i).GetChild(1).name == "black")
                    //    {
                    //        int num = int.Parse(bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text);
                    //        num++;
                    //        bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text = num.ToString();
                    //        return;
                    //    }
                    //}
                }
                break;
            default:
                break;
        }
    }

2、拖拽物品、删除物品、交换位置。

先上完整代码:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DragUI : MonoBehaviour,IDragHandler,IEndDragHandler
{
    public Vector2 offsetPos;

    public Transform partar;

    /// <summary>
    /// 拖拽物品
    /// </summary>
    /// <param name="eventData"></param>
    public void OnDrag(PointerEventData eventData)
    {
        transform.parent = GameObject.Find("Canvas").gameObject.transform;
        transform.position = eventData.position - offsetPos;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        //--------是否对格子物品进行丢弃------------
        if(Mathf.Abs(Vector3.Distance(transform.position, transform.parent.position)) > 180)
        {
            int num = int.Parse(partar.GetChild(0).GetComponent<Text>().text);
            num--;
            if(num > 0)
            {
                partar.GetChild(0).GetComponent<Text>().text = num.ToString();
            }
            else if(num <= 0)
            {
                partar.GetChild(0).GetComponent<Text>().text = "";
                Destroy(transform.gameObject);
                return;
            }
        }

        //---------格子物品交换-----------
        for (int i = 0; i < bagUI.Instance.bagBakcGround.transform.childCount; i++)
        {
            if(bagUI.Instance.bagBakcGround.transform.GetChild(i).childCount > 1)
            {
                if(Mathf.Abs(Vector3.Distance(transform.position, bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(1).position)) < 2.5f)
                {
                    //----更换物品数量---
                    string targetNum = partar.GetChild(0).GetComponent<Text>().text;
                    string ChangNum = bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text;
                    bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text = targetNum;
                    partar.GetChild(0).GetComponent<Text>().text = ChangNum;

                    //----------更换物品---
                    bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(1).GetComponent<DragUI>().partar = partar;
                    bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(1).parent = partar;
                    partar.GetChild(1).localPosition = new Vector3(0, 0, 0);

                    transform.parent = bagUI.Instance.bagBakcGround.transform.GetChild(i);
                    transform.localPosition = new Vector3(0, 0, 0);

                    partar = transform.parent;
                    return;
                }
            }
            else if(bagUI.Instance.bagBakcGround.transform.GetChild(i).childCount <= 1)
            {
                if(Mathf.Abs(Vector3.Distance(transform.position, bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(0).position)) < 55.0f &&
                    Mathf.Abs(Vector3.Distance(transform.position, bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(0).position)) > 25.0f)
                {
                    string parNum = partar.GetChild(0).GetComponent<Text>().text;
                    partar.GetChild(0).GetComponent<Text>().text = "";
                    transform.parent = bagUI.Instance.bagBakcGround.transform.GetChild(i);
                    transform.localPosition = new Vector3(0, 0, 0);

                    partar = transform.parent;
                    partar.GetChild(0).GetComponent<Text>().text = parNum;
                    return;
                }
            }
        }
        transform.parent = partar;
        transform.localPosition = new Vector3(0, 0, 0);
    }

    private void Start()
    {
        partar = transform.parent;
    }
}

(1)拖拽物品

拖拽物体实现UnityEngine.EventSystems中的接口IDragHandler和IEndDragHandler

OnDrag中的transform.parent赋值是因为UI层级关系,拖拽UI到其他格子最上面,不能被层级关系遮挡。

 partar是用来存放该物品UI的父物体,拖拽结束时需要返回到拖拽前的位置。

OnEndDrag结束时

 (2)删除物品

删除物品是用当前物品的pos和格子pos通过求绝对值来判断时候已经将物品拖拽到背包界面外面来删除该物品;

如果该物品就一个,那么就直接删除物品,如果大于1那么就数量减减。

(3)交换位置

 //---------格子物品交换-----------
        for (int i = 0; i < bagUI.Instance.bagBakcGround.transform.childCount; i++)
        {
            if(bagUI.Instance.bagBakcGround.transform.GetChild(i).childCount > 1)  //如果格子不为空
            {
                if(Mathf.Abs(Vector3.Distance(transform.position, bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(1).position)) < 2.5f)
                {
                    //----更换物品数量---
                    string targetNum = partar.GetChild(0).GetComponent<Text>().text;
                    string ChangNum = bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text;
                    bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text = targetNum;
                    partar.GetChild(0).GetComponent<Text>().text = ChangNum;

                    //----------更换物品---
                    bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(1).GetComponent<DragUI>().partar = partar;
                    bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(1).parent = partar;
                    partar.GetChild(1).localPosition = new Vector3(0, 0, 0);

                    transform.parent = bagUI.Instance.bagBakcGround.transform.GetChild(i);
                    transform.localPosition = new Vector3(0, 0, 0);

                    partar = transform.parent;
                    return;
                }
            }
            else if(bagUI.Instance.bagBakcGround.transform.GetChild(i).childCount <= 1)  //如果格子为空
            {
                if(Mathf.Abs(Vector3.Distance(transform.position, bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(0).position)) < 55.0f &&
                    Mathf.Abs(Vector3.Distance(transform.position, bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(0).position)) > 25.0f)
                {
                    string parNum = partar.GetChild(0).GetComponent<Text>().text;
                    partar.GetChild(0).GetComponent<Text>().text = "";
                    transform.parent = bagUI.Instance.bagBakcGround.transform.GetChild(i);
                    transform.localPosition = new Vector3(0, 0, 0);

                    partar = transform.parent;
                    partar.GetChild(0).GetComponent<Text>().text = parNum;
                    return;
                }
            }
        }

交换物品位置是需要考虑交换到目标的格子中是否为空。

3、排列背包物品

当背包中的物品排列杂乱无章,有的格子有物品,有的格子没有,这时就需要排列背包中的物品。

/// <summary>
    /// 整理背包
    /// </summary>
    void FinishingBag()
    {
        Dictionary<Transform, GameObject> ObjNum = new Dictionary<Transform, GameObject>();
        for (int i = 0; i < bagBakcGround.transform.childCount; i++)
        {
            if(bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text != "")
            {
                ObjNum.Add(bagBakcGround.transform.GetChild(i).GetChild(0), bagBakcGround.transform.GetChild(i).GetChild(1).gameObject);
            }
        }

        List<Transform> ObjKey = new List<Transform>(ObjNum.Keys);

        for (int i = 0; i < bagBakcGround.transform.childCount; i++)
        {
            if(bagBakcGround.transform.GetChild(i).childCount <= 1)
            {
                if(i < ObjNum.Count)
                {
                    bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent<Text>().text = ObjKey[i].GetComponent<Text>().text;
                    ObjNum[ObjKey[i]].transform.parent.GetChild(0).GetComponent<Text>().text = "";

                    ObjNum[ObjKey[i]].transform.parent = bagBakcGround.transform.GetChild(i);
                    ObjNum[ObjKey[i]].transform.GetComponent<DragUI>().partar = bagBakcGround.transform.GetChild(i);
                    bagBakcGround.transform.GetChild(i).GetChild(1).localPosition = new Vector3(0, 0, 0);
                }
            }
        }
    }

效果图如下:

  • 13
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值