unity背包和拾取系统制作(二)

背包UI方面脚本

拾取方法:

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

public class UPickup : MonoBehaviour
{
    private List<string> m_tagname=new List<string>();
    private List<GameObject> m_obj = new List<GameObject>();
    private ArticleManage articleManage;
    private Item m_item;
    private GameObject[] cells1;
    private GameObject[] cells2;
    private GameObject[] cells3;

    public GameObject instantiate;
    public GameObject grid1;
    public GameObject grid2;
    public GameObject grid3;
    public GameObject actor;
    GameObject item;


    Image[] Images;
    Image Imagesingle;

    Text index;
    int IndexInt = 0;
    string IndexStr = "";
    int RandomM = 0;
    string RandomStrInt = "";
    string RandomStr = "";
    int m_count = 0;

    // Use this for initialization
     void Awake()
    {
        articleManage = new ArticleManage();
        articleManage.Awake();
    }
    void Start()
    {
        cells1 = new GameObject[24];
        cells2 = new GameObject[24];
        cells3 = new GameObject[24];

        //grid = this.gameObject;
        for (int p = 0; p < grid1.transform.childCount; p++)
        {
            cells1[p] = grid1.transform.GetChild(p).gameObject;
        }
        for (int p = 0; p < grid2.transform.childCount; p++)
        {
            cells2[p] = grid2.transform.GetChild(p).gameObject;
        }
        for (int p = 0; p < grid3.transform.childCount; p++)
        {
            cells3[p] = grid3.transform.GetChild(p).gameObject;
        }
    }

    // Update is called once per frame
    void Update()
    {
        //按下拾取键之后,搜寻范围内的物体,通过比较tag来判断是否拾取,拾取到哪一个背包
        if (Input.GetKeyDown(KeyCode.G))
        {

            Collider[] cols = Physics.OverlapSphere(actor.transform.position, 10f);

            for (int i = 0; i < cols.Length; i++)

            {

                if (cols[i].CompareTag("equipment"))
                {
                    Pickup(cols[i].gameObject, cols[i].tag,cells1);
                    Destroy(cols[i].gameObject);
                }
                else if (cols[i].CompareTag("consumable"))
                {
                    Pickup(cols[i].gameObject,cols[i].tag,cells2);
                }
                else if (cols[i].CompareTag("taskthing"))
                {
                    Pickup(cols[i].gameObject,cols[i].tag,cells3);
                }

            }
        }

    }

    public void Pickup(GameObject obj ,string obj_tagname,GameObject[] cells)
    {

        bool isFind = false;
        RandomStr = "Image/" + obj.name;//路径
        print(obj_tagname);

        //通过Search方法去存储的地方寻找信息,并进行存储
        m_item= articleManage.Search(obj, obj_tagname);
       
        //获取预制物体
        item = Instantiate(instantiate, transform.position, transform.rotation) as GameObject;

        //设置相应物体的信息
        item.tag = obj_tagname;
        item.GetComponent<ItemManage>().SetItem(obj_tagname, m_item);
        Debug.Log("uppick:"+m_item.Name);

        //获取预制物体的image组件
        Imagesingle = item.transform.GetComponent<Image>();                                                                         
       
        //动态加载image组件的sprite
        Imagesingle.overrideSprite =Resources.Load(RandomStr,typeof(Sprite)) as Sprite;          
        print(Imagesingle.sprite);

        for (int i = 0; i < cells.Length; i++)
        {
            if (cells[i].transform.childCount > 0)
            {//判断当前格子是否有物体
             //如果有,并且一样的
                if (Imagesingle.overrideSprite.name == cells[i].transform.GetChild(0).transform.GetComponent<Image>().overrideSprite.name)
                {
                    //判断的是image加载图片的名字
                    isFind = true;

                    index = cells[i].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>();
                    IndexInt = int.Parse(index.text);
                    IndexInt += 1;
                    IndexStr = IndexInt.ToString();
                    index.text = IndexStr;
                    Destroy(item);
                }
            }
        }
        if (isFind == false)
        {
            for (int i = 0; i < cells.Length; i++)
            {
                if (cells[i].transform.childCount == 0)
                {
                    //当前没有物体,则添加
                    item.transform.SetParent(cells[i].transform);
                    item.transform.localPosition = Vector3.zero;
             
                    break;
                }
            }
        }
    }

}

背包物体交换方法:

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


public class BagExchange : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
    //得到画布
    private Transform canvas;
    //要拖动物体的原始父物体
    private Transform originalParent;
    //组件
    private CanvasGroup canvasGroup;
    //偏移量
    private Vector3 offset;

    void Start()
    {
        canvas = GameObject.Find("BagCanvas").transform;
        canvasGroup = GetComponent<CanvasGroup>();
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        originalParent = transform.parent;
        print(transform.gameObject.name);
        //改变要拖拽物体的父物体为画布,这样物体就会显示在所有背包的最前面
        transform.SetParent(canvas);
        offset = transform.position - Input.mousePosition;
        //在拖拽控件时,鼠标射线可以穿透控件,并被下面的控件所接受
        canvasGroup.blocksRaycasts = false;
    }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = Input.mousePosition + offset;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        print("getname:" +eventData.pointerEnter.gameObject.name);
        //鼠标拖拽控件时进入的物体
        Transform obj;
        //如果鼠标在背包的外面
        if (eventData.pointerEnter == null)
        {
            //把拖拽的物体放回到原来的位置
            transform.SetParent(originalParent);
            transform.localPosition = Vector3.zero;
            canvasGroup.blocksRaycasts = true;
            return;
        }
        else
        {
            obj = eventData.pointerEnter.transform;
        }
        //需要判断OBJ是什么控件,如果是其他物品,就交换,是空格就放上去
        switch (obj.tag)
        {
            case "equipment":
                {
                    Transform temp = obj.parent;
                    obj.SetParent(originalParent);
                    transform.SetParent(temp);
                    obj.localPosition = Vector3.zero;
                }
                break;
            case "consumable":
                {
                    Transform temp = obj.parent;
                    obj.SetParent(originalParent);
                    transform.SetParent(temp);
                    obj.localPosition = Vector3.zero;
                }
                break;
            case "taskthing":
                {
                    Transform temp = obj.parent;
                    obj.SetParent(originalParent);
                    transform.SetParent(temp);
                    obj.localPosition = Vector3.zero;
                }
                break;
            case "Cell":
                {
                    //如果有一个物体
                    if (obj.childCount == 1)
                    {
                        Transform child = obj.GetChild(0);
                        child.SetParent(originalParent);
                        transform.SetParent(obj);
                        child.localPosition = Vector3.zero;
                    }
                    else
                    {//如果没有物体
                        transform.SetParent(obj);
                    }
                }
                break;
            default:
                {
                    transform.SetParent(originalParent);
                }
                break;
        }
        transform.localPosition = Vector3.zero;
        canvasGroup.blocksRaycasts = true;
    }

}

小问题:
1.Resource.Load()方法直接从Resource目录查询,所以假如想要加载Resource/Image下的物体,直接Resource.Load(Image/,…)就行

2.关于CanvasGroup组件,这个组件的作用是对添加了该组件的物体,当对canvasGroup进行相应的更改的时候,所有的子物体也会进行相应的改变。

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值