zoukankan      html  css  js  c++  java
  • Demon_背包系统(实现装备栏,背包栏,可以切换装备)

    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);
            }
        }
    
        /// <summary>
        /// 接收物品
        /// </summary>
        /// <param name="goods">Goods.</param>
        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<CanvasGroup> ();
        }
    
        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<Box> ().ReceiveGoods (this);
                }
                //如果是物品
                else if (tag == "Goods") {
                    //下面物品
                    Goods herGoods = eventData.pointerEnter.GetComponent<Goods> ();
                    //下面物品所在格子
                    Box herBox = herGoods.parent.GetComponent<Box>();
                    //当前物品
                    Goods myGoods = this;
                    //当前物品所在格子
                    Box myBox = parent.GetComponent<Box>();
                    //交换
                    BagSingleton.instance.GoodsExchange (myBox, myGoods, herBox, herGoods);
                }
                //其他
                else {
                    //返回
                    ReturnBack ();
                }
            } else {
                //返回
                ReturnBack ();
            }
    
            //开启阻挡射线
            canvasGroup.blocksRaycasts = true;
        }
    
        /// <summary>
        /// 返回原单位
        /// </summary>
        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;
        }
    
        /// <summary>
        /// 设置格子父物体,并与父物体位置保持同步
        /// </summary>
        /// <param name="parent">Parent.</param>
        public void SetParent(Transform son ,Transform parent)
        {
            son.SetParent (parent);
            son.localPosition = Vector3.zero;
        }
    
        /// <summary>
        /// 物品交换
        /// </summary>
        /// <param name="myBox">My box.</param>
        /// <param name="myGoods">My goods.</param>
        /// <param name="herBox">Her box.</param>
        /// <param name="herGoods">Her goods.</param>
        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两个脚本会引用的单例脚本判断物品切换
  • 相关阅读:
    mysql max_allowed_packet过小导致的prepare失败
    linux tcp/ip编程和windows tcp/ip编程差别以及windows socket编程详解
    mysql metadata lock锁
    velocity merge作为工具类从web上下文和jar加载模板的两种常见情形
    mysql 5.7.15发布
    mysql 5.6.33发布
    2016年09月编程语言排行榜
    postgresql 9.6 rc1发布
    www.97top10.com--做最好的技术交流网站
    nginx/ajax跨子域请求的两种现代方法以及403解决
  • 原文地址:https://www.cnblogs.com/VR-1024/p/6011844.html
Copyright © 2011-2022 走看看