我在创建一个脚本时遇到了麻烦,该脚本允许将游戏对象拖放到屏幕的垃圾区域,从而破坏了游戏对象。我在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
}
好的,我做了一些更改,现在,当我将游戏对象拖到垃圾箱时,它会被销毁,但垃圾箱也是如此。我一直在努力使其运作,但仍然没有运气。这是我现在所拥有的,但仍无法正常工作。
公共无效OnDrop(PointerEventData eventData){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,一切都应该正常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句