博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NGUI 简单的背包系统
阅读量:7022 次
发布时间:2019-06-28

本文共 3604 字,大约阅读时间需要 12 分钟。

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_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
().spriteName = temp; //将随机获得的图片名字进行预制体的外观赋值 break; //当一次实例化出来一个技能物品后要进行返回 } } } }}

 

 

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

                                                                                                2018-03-31、10:21:03

转载于:https://www.cnblogs.com/zhh19981104/p/8681190.html

你可能感兴趣的文章
Jav解析xml
查看>>
linux学习篇(一)
查看>>
Python网络数据采集PDF
查看>>
topcoder srm 662 div1
查看>>
(备忘)获取调用者类名的一种方法
查看>>
26. Remove Duplicates from Sorted Array(代码思路新奇)
查看>>
思维体操: HDU1287破译密码
查看>>
How To Partition Existing Table Using DBMS_Redefinition
查看>>
微信“跳一跳”高分技巧
查看>>
Codeforces 855C - Helga Hufflepuff's Cup
查看>>
在线预览文件(pdf)
查看>>
Python之路----生成器函数进阶
查看>>
慢查询阻塞了xtrabackup进而阻塞以后的sql导致的系统瘫痪问题
查看>>
L148
查看>>
l366 多元化
查看>>
数据结构学习第二天
查看>>
Velocity.js的使用
查看>>
VS 2008 修改C++文件默认模板
查看>>
Java核心基础学习(一)--- 2019年1月
查看>>
ADO.NET编程之美----数据访问方式(面向连接与面向无连接)
查看>>