如何统一将一个对象的父级更改为另一个插槽父级?

发布于 2025-01-17 01:13:18 字数 2545 浏览 4 评论 0原文

我正在制作一款剪刀石头布游戏,但不是大家都知道的经典游戏,你有 12 张牌,每种类型 3 张蓝色牌(3 张纸牌,3 张石头牌,3 张剪刀牌),每种类型 2 张紫色牌和一张最后一张同时包含布和剪刀的卡片,要开始游戏,您必须将这些卡片排列在插槽中。这是游戏中的图片 经过深思熟虑,我让它与拖放系统一起工作,但我遇到了一些问题:

  • 第一个问题是,每次我尝试将任何卡放入插槽中时,它都会返回到原来的位置。
  • 第二个问题是我希望插槽成为插入其中的卡的父级,但当我开始拖动它时它只是离开它的父级然后当我释放它时它返回到它的原始父级(我什至尝试删除脚本中的一些行测试第二个问题是否是由第一个问题引起的,但即使卡在插槽中实现,这些代码也不是没有用的,如果卡没有在插槽中实现,它们会将卡返回到原始位置。一个槽)。 这里是卡片代码:
using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class DragDrop : MonoBehaviour, IPointerDownHandler,IBeginDragHandler,IEndDragHandler,IDragHandler
 {
     private RectTransform rectTransform;
     private CanvasGroup canvasGroup;
     public Text textField;
     [SerializeField] Canvas canvas;
     private Transform originalPos;
 
 
     private void Awake()
     {   
         rectTransform = GetComponent<RectTransform>();
         canvasGroup = GetComponent<CanvasGroup>();
     }
 
 
     public void OnBeginDrag(PointerEventData eventData)
     {
         canvasGroup.alpha = .5f;
         canvasGroup.blocksRaycasts = false;
         originalPos = transform.parent;
         transform.SetParent(transform.parent.parent);
     }
 
     public void OnDrag(PointerEventData eventData)
     {
         rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
     }
 
     public void OnEndDrag(PointerEventData eventData)
     {
         canvasGroup.alpha = 1f;
         canvasGroup.blocksRaycasts = true;
         transform.SetParent(originalPos);
         transform.localPosition = Vector3.zero;
         if (transform.localPosition == Vector3.zero)
         {
             textField.enabled = true;
         }
         
 
     }
 
     public void OnPointerDown(PointerEventData eventData)
     {
         
         
         textField.enabled = false;
         
         
     }
 }

这里是老虎机代码:

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class slots : MonoBehaviour, IDropHandler
 
 {
     public GameObject slot;
     public void OnDrop(PointerEventData eventData)
     {
         if(eventData.pointerDrag != null) 
         {
             eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
         }
     }
 }

我真的很感谢你能帮助我的任何事情,因为我已经被困在这个问题上两天了。

i am making a rock paper scissors game but not the classic one that everyone knows, you have 12 cards, 3 blue cards of each type(3 paper cards,3 rock cards,3 scissors cards), 2 purple cards of each type and one last card that contains paper and scissors at the same time and for the game to start you have to arange those cards in slots.here an image from the game
after a lot of thinking i made it work with drag&drop system but i faced some problems:

  • first problem that everytime i try to drop any card into the slot it returns to it's original position .
  • second problem that i want the slot to become the parent of the card inserted in it but it just leave it's parent when i start dragging it then when i release it it returns to it's original parent(i even tried to delete some lines in my script to test if the second problem is caused by the first one or not,but it's the same even thought that the card get implemented in the slot and those codes aren't for nothing they return the card to it's original position if it's not implemented in a slot ).
    here the cards code:
using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class DragDrop : MonoBehaviour, IPointerDownHandler,IBeginDragHandler,IEndDragHandler,IDragHandler
 {
     private RectTransform rectTransform;
     private CanvasGroup canvasGroup;
     public Text textField;
     [SerializeField] Canvas canvas;
     private Transform originalPos;
 
 
     private void Awake()
     {   
         rectTransform = GetComponent<RectTransform>();
         canvasGroup = GetComponent<CanvasGroup>();
     }
 
 
     public void OnBeginDrag(PointerEventData eventData)
     {
         canvasGroup.alpha = .5f;
         canvasGroup.blocksRaycasts = false;
         originalPos = transform.parent;
         transform.SetParent(transform.parent.parent);
     }
 
     public void OnDrag(PointerEventData eventData)
     {
         rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
     }
 
     public void OnEndDrag(PointerEventData eventData)
     {
         canvasGroup.alpha = 1f;
         canvasGroup.blocksRaycasts = true;
         transform.SetParent(originalPos);
         transform.localPosition = Vector3.zero;
         if (transform.localPosition == Vector3.zero)
         {
             textField.enabled = true;
         }
         
 
     }
 
     public void OnPointerDown(PointerEventData eventData)
     {
         
         
         textField.enabled = false;
         
         
     }
 }

and here the slots code:

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class slots : MonoBehaviour, IDropHandler
 
 {
     public GameObject slot;
     public void OnDrop(PointerEventData eventData)
     {
         if(eventData.pointerDrag != null) 
         {
             eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
         }
     }
 }

i really appreciate anything you can help me with cause i've been stuck in this for 2 days now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2025-01-24 01:13:18

在我看来,好像您正在 OnEndDrag 方法中将 Transform.parent 设置回原来的Pos​​。您可能应该执行 MouseToWorldPoint 并找到您将鼠标悬停在其上的对象,然后将父级设置为该 gameObjects 转换。

It looks to me as if you're setting the Transform.parent back to the originalPos there in your OnEndDrag method. You should probably do a MouseToWorldPoint and find the object you're hovering over, then set the parent to that gameObjects transform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文