Unity插件NGUI实现背包系统


                  背包系统的制作

1 创建一个Sprite作为背景图片

2 创建一个Sprite作为格子并保存为Prefab

3 在格子下创建Sprite作为物品

4 在物品下创建Label用来显示物品个数

5 把物品做成Prefab

 

7 给物品添加Box Collider

8 创建脚本继承UIDragDropItem指定给物品

using UnityEngine;

using System.Collections;

public class KnapsackItem : UIDragDropItem {

    protected override void OnDragDropRelease(GameObject surface)

    {

        base.OnDragDropRelease(surface);

        Debug.Log(surface.transform.childCount);//显示格子下面子物体有多少个

        //if (surface.transform.childCount > 0)//当物品大于零时

        if(surface.tag=="Ceil")//当格子上有物体时,surface将会改变,故用此来判断是否已经有物品

        {//没有物品时

            this.transform.parent = surface.transform;//将物品移到格子下边

            this.transform.localPosition = Vector3.zero;//设置局部坐标为0,此时可以居中

        }

        else if(surface.tag=="KnapsackItem")

        {//有物品时,进行交换

            Transform parent = surface.transform.parent;//过度,将格子上的当前物品位置保存

            surface.transform.parent = this.transform.parent;//将已经在格子上的物品移动到要交换的物品的格子

            surface.transform.localPosition = Vector3.zero;//设置局部坐标为0\

            this.transform.parent = parent;//把要交换的格子移过去

            this.transform.localPosition = Vector3.zero;

        }

    }

}

9 创建脚本指定给格子

代码设计:

using UnityEngine;

using System.Collections;

public class KnapSack : MonoBehaviour {

   public GameObject[] Ceils=new GameObject[16];//用来存放物品的格子数组

 }     

10 给格子进行编号并将格子复制给Ceils数组

11 设置Tag并指定给相应对象

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值