NGUI 简单的背包系统

1、首先在场景中创建格子,用来存放物体的

 

2、为每一个格子设置标签为Item,建议只做一个格子,然后创建预制体就可以了,然后为每一个格子附加Box Collider组件,要用于检测

3、接下来就是创建要实例出来的功能物体了,建议只创建一个,然后做成预制体就可以了,后面通过通过修改贴图就行了,减少步骤

4、为功能物品附加UIDragDropItem组件,这里对其OnDragDropRelease方法进行重写,有一定的好处:

5、接下来为物品添加一下脚本:

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

public class item_pag : UIDragDropItem
{


    public UISprite temp_01;
    public UILabel label_01;            //显示数量的


    private int number = 1;
    public void AddCount(int i=1)
    {
        //默认一次加一
        number += i;
        label_01.text = number + "";
        
    }


    protected override void OnDragDropRelease(GameObject surface)
    {
        base.OnDragDropRelease(surface);
        
        if(surface.tag=="Cell")
        {
            //当移到空的格子上的时候则直接移上去即可
            this.transform.parent = surface.transform;          //将物品附加到格子的下面
            this.transform.localPosition = Vector3.zero;            //使其的移动位置在其中心,不会需要精确的移动
        }
        else if(surface.tag=="Item")
        {
            //当格子上已经有物品了,则只能交换两个物品的位置信息了

            Transform parent = surface.transform.parent;                          //将原来格子上的物品信息存储下来                                                                  
            surface.transform.parent = this.transform.parent;                     //将要被迫移的格子放到现在的位置
            surface.transform.localPosition = Vector3.zero;

            this.transform.parent = parent;                                          //将要移动的物体移动到玩家要拖动到的位置上去
            this.transform.localPosition = Vector3.zero;
        }
    }
}

6、为用来放物品的格子添加一下脚本:

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

public class Bag : MonoBehaviour
{


    public GameObject[] cells;           //存放格子的数组
    public string[] itemName;           //这是捡到的物体名字
    public GameObject itemPrefabs;      //物体的预制体,可以通过名字对齐修改显示的外观
   // private 

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.X))
        {            
            PickUp();       //当按下x键的时候随机获得一个技能wupin
        }
    }
    private void PickUp()
    {

        bool IsItem = false;            //用于判断是够有重复的物品的标志位
        int index = Random.Range(0, itemName.Length);      
        string temp = itemName[index];                       //获得随机的物体名字
        for (int i = 0; i < cells.Length; i++)
        {
            if(cells[i].transform.childCount>0)
            {
                //当该格子上有子物体的时候开始检测时候会出现重复的
                item_pag itens = cells[i].GetComponentInChildren<item_pag>();     
                    
                //获取该格子上的子物体,也就是物品,对重复的操作处理放在item_bag脚本中处理
                if(itens.temp_01.spriteName==temp)
                {
                   
                    itens.AddCount();            //如果重名的话,给下标栏数量+1就行了
                    IsItem = true;
                    break;              
                }
            }
        }
        if(IsItem==false)
        {
            for (int i = 0; i < cells.Length; i++)
            {
                if (cells[i].transform.childCount <= 0)
                {
                    //当格子上没有物体的时候则为其空格子添加物体
                    GameObject gg = NGUITools.AddChild(cells[i], itemPrefabs);      //将物品添加到格子下面作为子物体
                    gg.transform.localPosition = Vector3.zero;
                    gg.GetComponent<UISprite>().spriteName = temp;          //将随机获得的图片名字进行预制体的外观赋值
                    break;          //当一次实例化出来一个技能物品后要进行返回
                }
            }
        }       
    }
}

7、大概的思路就是这样了,其中有一些步骤没有详细的说明出来,留点琢磨的空间吧!

                                                                                                2018-03-31、10:21:03

原文地址:https://www.cnblogs.com/zhh19981104/p/8681190.html