我在创建允许将游戏对象放到屏幕的垃圾区域从而破坏游戏对象的脚本时遇到问题。我在 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);
}
}
}
我是一名优秀的程序员,十分优秀!