博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Demon_背包系统(实现装备栏,背包栏,可以切换装备)
阅读量:4619 次
发布时间:2019-06-09

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

using UnityEngine;using System.Collections;public enum BoxType{    Normal,//普通格子    Equip//装备栏格子}    public enum EquipType{    Weapon,//武器    Armor,//护甲    Shoot//鞋子}public class Box : MonoBehaviour {    //格子类型    public BoxType boxType;    //装备类型    public EquipType equipType;    //格子的子对象(一些装备)    public Transform child;    void Start()    {        //给子对象赋值        if (transform.childCount == 1) {            child = transform.GetChild (0);        }    }    ///     /// 接收物品    ///     /// Goods.    public void ReceiveGoods(Goods goods)    {        //普通格子//        if (boxType == BoxType.Normal) {//            BagSingleton.instance.SetParent (goods.transform, transform);//        } else {//            //如果装备类型匹配//            if (goods.goodsType == equipType) {//                BagSingleton.instance.SetParent (goods.transform, transform);//            } else {//                //不匹配返回//                goods.ReturnBack ();//            }//        }        //优化版        if (boxType == BoxType.Equip && goods.goodsType != equipType) {            goods.ReturnBack ();        } else {            BagSingleton.instance.SetParent (goods.transform, transform);            //更新物品的父物体            goods.parent = transform;            //更新格子的子物体            child = goods.transform;        }    }}

using UnityEngine;using System.Collections;using UnityEngine.UI;using UnityEngine.EventSystems;public class Goods : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler {    //物品类型    public EquipType goodsType;    //父对象(格子)    public Transform parent;    private CanvasGroup canvasGroup;    void Start()    {        //给父对象赋值        if (transform.parent) {            parent = transform.parent;        }        //获取        canvasGroup = GetComponent
(); } public void OnBeginDrag (PointerEventData eventData) { //将物品移除格子 transform.SetParent (BagSingleton.instance.bag); //关闭阻挡射线 canvasGroup.blocksRaycasts = false; } public void OnDrag (PointerEventData eventData) { transform.position = Input.mousePosition; } public void OnEndDrag (PointerEventData eventData) { //如果当前物品下方有对象 if (eventData.pointerEnter) { //获取底层对象标签 string tag = eventData.pointerEnter.tag; //如果是格子 if (tag == "Box") { //接收物品 eventData.pointerEnter. GetComponent
().ReceiveGoods (this); } //如果是物品 else if (tag == "Goods") { //下面物品 Goods herGoods = eventData.pointerEnter.GetComponent
(); //下面物品所在格子 Box herBox = herGoods.parent.GetComponent
(); //当前物品 Goods myGoods = this; //当前物品所在格子 Box myBox = parent.GetComponent
(); //交换 BagSingleton.instance.GoodsExchange (myBox, myGoods, herBox, herGoods); } //其他 else { //返回 ReturnBack (); } } else { //返回 ReturnBack (); } //开启阻挡射线 canvasGroup.blocksRaycasts = true; } ///
/// 返回原单位 /// public void ReturnBack() { BagSingleton.instance.SetParent (transform, parent); }}

using UnityEngine;using System.Collections;public class BagSingleton : MonoBehaviour {    //单例    public static BagSingleton instance;    //背包    public Transform bag;    void Start()    {        instance = this;        bag = GameObject.FindWithTag ("Bag").transform;    }    ///     /// 设置格子父物体,并与父物体位置保持同步    ///     /// Parent.    public void SetParent(Transform son ,Transform parent)    {        son.SetParent (parent);        son.localPosition = Vector3.zero;    }    ///     /// 物品交换    ///     /// My box.    /// My goods.    /// Her box.    /// Her goods.    public void GoodsExchange(Box myBox,Goods myGoods,Box herBox,Goods herGoods)    {        //如果双方都在普通格子中,或,双方物品类型一致        if ((myBox.boxType == BoxType.Normal &&            herBox.boxType == BoxType.Normal)            || (myGoods.goodsType == herGoods.goodsType)) {            myBox.ReceiveGoods (herGoods);            herBox.ReceiveGoods (myGoods);        } else {            //当前物品返回原单位            myGoods.ReturnBack ();        }    }}
BagSingleton是Box跟goods两个脚本会引用的单例脚本判断物品切换

转载于:https://www.cnblogs.com/VR-1024/p/6011844.html

你可能感兴趣的文章
linux下core文件调试方法
查看>>
20个创意404错误页面设计的启示
查看>>
基础训练 芯片测试
查看>>
如何用命令将本地项目上传到git
查看>>
JavaScript 实现鼠标拖动元素
查看>>
js 模糊查询 (360接口)
查看>>
python+rabbitMQ实现生产者和消费者模式
查看>>
“模态”对话框和“后退”按钮
查看>>
关于javascript实现的网站页面侧边悬浮框"抖动"问题
查看>>
linux_命令格式和命令提示符
查看>>
Cocos2d-X-3.0之后的版本的环境搭建
查看>>
when case group by 的用法集合
查看>>
认识XmlReader
查看>>
JAVA学习Swing章节标签JLabel中图标的使用
查看>>
sqlserver,oracle,mysql等的driver驱动,url怎么写
查看>>
局部变量和static变量的区别
查看>>
IE下iframe不能正常加载,显示空白
查看>>
mysql服务性能优化—my.cnf配置说明详解
查看>>
洛谷P1908 逆序对
查看>>
noip模拟赛 排列
查看>>