找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 100|回复: 0

拖拽物体结束后鼠标释放时丢弃物品到大地上时的射线检测代码

[复制链接]

36

主题

3

回帖

385

积分

管理员

积分
385
发表于 2026-7-13 00:57:47 | 显示全部楼层 |阅读模式
  1. public void OnEndDrag(PointerEventData eventData)
  2. {
  3.     inventoryUI.dragItem.enabled = false;

  4.     if (eventData.pointerCurrentRaycast.gameObject != null)
  5.     {
  6.         // 原有 UI 交换逻辑
  7.         SlotUI targetSlot = eventData.pointerCurrentRaycast.gameObject.GetComponent<SlotUI>();
  8.         if (targetSlot == null) return;

  9.         int targetIndex = targetSlot.slotIndex;
  10.         if (slotType == SlotType.Bag && targetSlot.slotType == SlotType.Bag)
  11.         {
  12.             InventoryManager.Instance.SwapItem(slotIndex, targetIndex);
  13.         }
  14.         inventoryUI.UpdateSlotHighLight(targetIndex);
  15.     }
  16.     else
  17.     {
  18.         // ----- 丢弃到场景逻辑 -----
  19.         if (!itemDetails.canDropped)
  20.         {
  21.             Debug.Log("该物品无法丢弃");
  22.             return;
  23.         }

  24.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  25.         RaycastHit hit;
  26.         // 只检测 Ground 层(可换成你定义的地面层)
  27.         int layerMask = 1 << LayerMask.NameToLayer("Ground");
  28.         if (Physics.Raycast(ray, out hit, 100f, layerMask))
  29.         {
  30.             // 检查是否为禁止丢弃区域
  31.             if (hit.collider.CompareTag("NoDrop"))
  32.             {
  33.                 Debug.Log("此区域禁止丢弃");
  34.                 return;
  35.             }
  36.             // 生成物品
  37.             Vector3 spawnPos = hit.point + Vector3.up * 0.5f;
  38.             EventHandler.CallInstantiateItemInScene(itemDetails.itemID, spawnPos);
  39.         }
  40.         else
  41.         {
  42.             Debug.Log("没有地面可以放置物品");
  43.         }
  44.     }
  45. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|游戏黄埔游戏开发 ( 豫ICP备2024088136号-1|豫公网安备41040202000275号 )

GMT+8, 2026-9-28 22:26 , Processed in 0.044422 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表