创建一个带有垃圾桶的拖放系统来移除对象

本文关键字:系统 拖放 对象 垃圾桶 一个 创建 | 更新日期: 2023-09-27 18:01:00

我在创建一个脚本时遇到了问题,该脚本允许将游戏对象放到屏幕的垃圾区,从而破坏游戏对象。onDrop函数哪里出错了?基本上,我想说的是,当一个游戏对象被扔到垃圾桶上时,它就变成了垃圾桶的孩子,一旦它变成了孩子,它就会被摧毁。这个逻辑有道理吗?

using System.Collections;
using UnityEngine.EventSystems;
namespace MyNamespace
{
    public class DropZone : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler 
    {
        public void OnPointerEnter(PointerEventData eventData)
        {
            if(eventData.pointerDrag == null)
                return;
            DragHandling dragHandling = eventData.pointerDrag.GetComponent<DragHandling>();
            if(dragHandling != null)
            {
                dragHandling.placeholderParent = this.transform;        // change parent based on drop zone
            } 
        }
    public void OnPointerExit(PointerEventData eventData)
    {
        if(eventData.pointerDrag == null)
            return;
        DragHandling dragHandling = eventData.pointerDrag.GetComponent<DragHandling>();
        if(dragHandling != null && dragHandling.placeholderParent == this.transform)
        {
            dragHandling.placeholderParent = dragHandling.parentToReturnTo;     // change parent based on drop zone
        } 
    }
    public void OnDrop(PointerEventData eventData)
    {
        Debug.Log(eventData.pointerDrag.name + " was dropped on " + gameObject.name);
        DragHandling dragHandling = eventData.pointerDrag.GetComponent<DragHandling>();
        if(dragHandling != null)
        {
            dragHandling.parentToReturnTo = this.transform;     // change parent based on drop zone
        }  
        if (this.transform.parent.gameObject == dragHandling.trashCan) 
        {
            Destroy(this);
        }
    }
}

这是我用来获取变量的脚本:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class DragHandling : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler
{
    public Transform placeholderParent = null;
    public Transform parentToReturnTo = null;
    GameObject placeholder = null;
    public GameObject trashCan;
    public Transform trashCanTrans;
    public GameObject partsPanel;
    public Transform partsPanelTrans;
    public GameObject buildBoard;
    public GameObject dragLayer;
    void Awake ()
    {
        dragLayer = GameObject.FindGameObjectWithTag("DragLayer");
        buildBoard = GameObject.FindGameObjectWithTag("Board");
        partsPanel = GameObject.FindGameObjectWithTag("Parts");
        trashCan = GameObject.FindGameObjectWithTag("Trash");
        partsPanelTrans = partsPanel.transform;
        // trashCanTrans = trashCan.transform;
    }
    #region IPointerClickHandler implementation
    public void OnPointerClick (PointerEventData eventData)
    {
        if(this.transform.parent.gameObject == buildBoard)
        {
            this.transform.SetAsLastSibling();
        }
    }
    #endregion
    #region IBeginDragHandler implementation
    public void OnBeginDrag (PointerEventData eventData)
    {
        // this.transform.SetAsLastSibling();
        // create placeholder gap and hold correct position in layout
        placeholder = new GameObject();
        placeholder.transform.SetParent(this.transform.parent);
        placeholder.transform.SetSiblingIndex(this.transform.GetSiblingIndex());
        LayoutElement le = placeholder.AddComponent<LayoutElement>();               // add layout element
        le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;      
        le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
        le.flexibleWidth = 0;
        le.flexibleHeight = 0;
        parentToReturnTo = this.transform.parent;                   // store current parent location
        placeholderParent = parentToReturnTo;                       // set placeholder gameobject transform
        GetComponent<CanvasGroup>().blocksRaycasts = false;         // turn off image raycasting when dragging image in order to see what's behind the image            
    }
    #endregion
    #region IDragHandler implementation
    public void OnDrag (PointerEventData eventData)
    {
        this.transform.position = eventData.position;               // set object coordinates to mouse coordinates
        this.transform.SetParent(dragLayer.transform);              // pop object to draglayer to move between dropzones
    }
    #endregion
    #region IEndDragHandler implementation
    public void OnEndDrag (PointerEventData eventData)
    {
        this.transform.SetParent(parentToReturnTo);                                 // Snaps object back to orginal parent if dropped outside of a dropzone
        this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());    // Returns card back to placeholder location
        GetComponent<CanvasGroup>().blocksRaycasts = true;                          // turn on Raycast blocking
        Destroy(placeholder);                                                       // kill the placeholder if object hits a drop zone or returns to parts panel
    }
    #endregion
}

好吧,所以我做了一些更改,现在当我把游戏对象拖到垃圾桶上时,它会被破坏,但垃圾桶也会被破坏。我一直在努力让它发挥作用,但仍然没有成功。这是我现在拥有的,但它仍然不能正常工作。

public void OnDrop(PointerEventData事件数据({Debug.Log(eventData.pointerDrag.name+"已在"+gameObject.name上删除(;

    DragHandling dragHandling = eventData.pointerDrag.GetComponent<DragHandling>();
    if(dragHandling != null)
    {
        dragHandling.parentToReturnTo = this.transform;     // change parent based on drop zone
        if (this.transform.gameObject == dragHandling.trashCan) 
        {
            Debug.Log("You've dragged something into the trash!");
            Destroy(this.transform.gameObject);
        }
    }  
}

创建一个带有垃圾桶的拖放系统来移除对象

老实说,我不认为你应该把"this"放在destroy函数中,看到有人直接问这个问题:http://answers.unity3d.com/questions/19223/is-destroygameobject-the-same-as-destroythis.html

你试图破坏掉落区域,而不是破坏gameObject。只销毁gameObject,一切都会好起来。

如果垃圾桶与游戏对象一起自毁,请不要使该对象成为垃圾桶的子对象。这里可能发生的情况是destroy((函数也想破坏对象的父对象,以保持数据的一致性。