Unity 实现背包功能

来源:互联网 发布:淘宝店铺过期不存在 编辑:程序博客网 时间:2024/05/21 10:51

NGUI下创建几个Sprite图片,作为盛放物品的格子,并命名为Sprite。在其中的一个格子里创建四个Sprite,并命名Wupin_Baoxiang,,,作为物品,,,记得在每个框和物体都加上Box Collicer,,并且为边框加上Iskuang的标签,物品身上加上标签IsWuTi,,加上如下代码即可实现背包简易功能(两个物品互换位置,拖拽到别的框里,拖拽到非框区域回到原来的位置)


using UnityEngine;using System.Collections;public class BeiBao : UIDragDropItem{    protected override void OnDragDropRelease(GameObject surface)    {        base.OnDragDropRelease(surface);        //获取当前物体的父对象位置        Transform myparent = this.transform.parent;                if (surface.tag == "IsKuang")  //如果是框就存进去        {            //改变父对象并且本地坐标重置为零            this.transform.parent = surface.transform;            this.transform.localPosition = Vector3.zero;                  }        else if(surface.tag == "IsWuTi")   //如果是物体就交换位置        {                //声明一个变量,把B的父级存起来                Transform parent = surface.transform.parent;                //B的父级换成了A的父级 并且相对位置重置为零                surface.transform.parent = myparent;                         surface.transform.localPosition = Vector3.zero;                   //与上同理 A - B                this.transform.parent = parent;                  this.transform.localPosition = Vector3.zero;        }        else        {                //其他情况 就返回原来的地方                this.transform.parent = myparent;                this.transform.localPosition = Vector3.zero;            //Debug.Log(surface.name);                  }    } }


原创粉丝点击