【Unity3D】使用IPointerDownHandler和IPointerUpHandler简易实现背包小案例

BagPanel管理着全部 格子,格子管理具体物品,SlotItem是具体物品。

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 背包容器
/// </summary>
public class BagPanel : MonoBehaviour, BagInterface
{
    public GameObject slotItemPrefab;
    //背包全部格子
    private Slot[] slotArr;

    private void Start()
    {
        slotArr = transform.GetComponentsInChildren<Slot>();
    }

    private void Update()
    {
        //右击添加物体
        if (Input.GetMouseButtonDown(1))
        {
            AddBagItem();
        }
    }

    /// <summary>
    /// 添加物品
    /// </summary>
    public void AddBagItem()
    {
        Slot slot = GetEmptySlot();
        if (slot != null)
        {
            GameObject slotItemGo = GameObject.Instantiate(slotItemPrefab);
            slotItemGo.transform.SetParent(slot.transform);
            slotItemGo.transform.localPosition = Vector3.zero;
        }
    }
    
    /// <summary>
    /// 获取空格子
    /// </summary>
    private Slot GetEmptySlot()
    {
        for (int i = 0; i < slotArr.Length; i++)
        {
            if (slotArr[i].transform.childCount == 0)
            {
                return slotArr[i];
            }
        }
        return null;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface BagInterface{

    /// <summary>
    /// 添加物品
    /// </summary>
    void AddBagItem();    
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 背包格子
/// </summary>
public class Slot : MonoBehaviour {

    //可以做一些格子才能做到的事情,因为它管理着具体的物品,可以添加一些使用道具的方法
    //选中后使用道具,选中后销毁道具,选中后升级道具等操作。
    //道具数量可直接放在SlotItem,也可以放在Slot,区别不大,只是放在Slot在移动Item时,还要处理Slot的数量,而放在SlotItem的话 就直接在SlotItem一起处理完毕。
    //反正,目前的小案例是不需要使用到这个Slot去完成BagPanel管理者与SlotItem进行交互的
    
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

/// <summary>
/// 背包物品
/// </summary>
public class SlotItem : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    private bool IsHover = false;
    private Image image;     
    private void Start()
    {
        image = GetComponent<Image>();
    }
    private void Update()
    {
        if (IsHover)
        {
            this.transform.position = Input.mousePosition;
        }
    }
    //鼠标按下
    public void OnPointerDown(PointerEventData eventData)
    {
        IsHover = true;
        image.raycastTarget = false;//当鼠标拿起物品后,物品不能接收鼠标射线,为的就是能让下面的方法拿到放下的格子
    }

    /// <summary>
    /// 鼠标抬起
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerUp(PointerEventData eventData)
    {
        //拿到物品到放下的格子物体(注意:因为我们在拿起的时候把鼠标上物品的raycastTarget=false,所以现在pointerCurrentRaycast射线是碰不到鼠标上的物品的,而是它下面的物体
        GameObject upGo = eventData.pointerCurrentRaycast.gameObject;
        Debug.Log(upGo.name);
        //若放下的区域已经有物品存在,此时upGo是要放下的格子的道具物体
        if (upGo.GetComponent<SlotItem>() != null)
        {
            //交换
            GameObject upGoChild = upGo;
            Transform mouseGoParent = this.transform.parent;
            this.transform.SetParent(upGoChild.transform.parent);
            this.transform.localPosition = Vector3.zero;//这句代码就是居中显示.
            upGoChild.transform.SetParent(mouseGoParent);
            upGoChild.transform.localPosition = Vector3.zero;            
        }
        else
        {            
            this.transform.SetParent(upGo.transform);//此时upGo是空格子Slot
            this.transform.localPosition = Vector3.zero;
        }
        IsHover = false;
        image.raycastTarget = true;//别忘记恢复物品能检测射线,不然你再按就拿不起来了。。
    }
}

重要点PointerEventData的pointerCurrentRaycast可理解为一条射向鼠标点的射线,为了防止鼠标上已经拿着的物品 阻挡着射线,而设置了物品身上的Image组件的raycastTarget,若你的物品是很复杂的,建议在物品身上加CanvasGroup,再把Blocks Raycasts取消勾选即可,如下:

好了,最基本的背包功能差不多就这样,其他的也就是差不多的。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值