gpt4 book ai didi

c# - 创建带有垃圾桶的拖放系统以移除对象

转载 作者:太空宇宙 更新时间:2023-11-03 23:34:00 25 4
gpt4 key购买 nike

我在创建允许将游戏对象放到屏幕的垃圾区域从而破坏游戏对象的脚本时遇到问题。我在 onDrop 函数中哪里出错了?基本上我想说的是,当一个 GameObject 掉落到垃圾桶上时,它会成为垃圾桶的子项,一旦成为子项,它就会被销毁。这个逻辑合理吗?

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 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.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

你试图破坏掉落区而不是破坏游戏对象。只销毁游戏对象,一切都应该没问题。

关于c# - 创建带有垃圾桶的拖放系统以移除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31209229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com