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 == ) {
child = transform.GetChild ();
}
} /// <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两个脚本会引用的单例脚本判断物品切换