C# 使用垃圾桶创建拖放系统以移除对象
我在创建一个脚本时遇到了问题,该脚本允许将游戏对象放到屏幕的垃圾区域,从而破坏游戏对象。我在onDrop函数中哪里出错了?基本上我想说的是,当一个游戏物体被扔到垃圾桶上时,它就变成了垃圾桶的孩子,一旦它是孩子,它就会被摧毁。这种逻辑有意义吗C# 使用垃圾桶创建拖放系统以移除对象,c#,unity3d,drag-and-drop,C#,Unity3d,Drag And Drop,我在创建一个脚本时遇到了问题,该脚本允许将游戏对象放到屏幕的垃圾区域,从而破坏游戏对象。我在onDrop函数中哪里出错了?基本上我想说的是,当一个游戏物体被扔到垃圾桶上时,它就变成了垃圾桶的孩子,一旦它是孩子,它就会被摧毁。这种逻辑有意义吗 using System.Collections; using UnityEngine.EventSystems; namespace MyNamespace { public class DropZone : MonoBehaviour, IDr
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);
}
}
}
使用系统集合;
使用UnityEngine.EventSystems;
名称空间MyNamespace
{
公共类DropZone:MonoBehavior、IDropHandler、IpInterenterHandler、IpInterexiThandler
{
公共void OnPointerEnter(pointereventdataeventdata)
{
if(eventData.pointerDrag==null)
返回;
DragHandling DragHandling=eventData.pointerDrag.GetComponent();
if(拖动!=null)
{
dragHandling.placeholderParent=this.transform;//根据放置区域更改父项
}
}
公共void OnPointerExit(pointereventdataeventdata)
{
if(eventData.pointerDrag==null)
返回;
DragHandling DragHandling=eventData.pointerDrag.GetComponent();
if(dragHandling!=null&&dragHandling.placeholderParent==this.transform)
{
dragHandling.placeholderParent=dragHandling.parentToReturnTo;//根据放置区域更改父项
}
}
公共无效OnDrop(PointerEventData事件数据)
{
Debug.Log(eventData.pointerDrag.name+”被放在“+gameObject.name”上);
DragHandling DragHandling=eventData.pointerDrag.GetComponent();
if(拖动!=null)
{
dragHandling.parentToReturnTo=this.transform;//根据放置区域更改父对象
}
if(this.transform.parent.gameObject==dragHandling.trashCan)
{
摧毁(这个);
}
}
}
这是我用来获取变量的脚本:
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
}
使用UnityEngine;
使用UnityEngine.UI;
使用系统集合;
使用UnityEngine.EventSystems;
使用System.Collections.Generic;
公共类拖动:MonoBehavior、IBeginDragHandler、IDragHandler、IENDragHandler、IPointerClickHandler
{
公共转换占位符父项=null;
public Transform parentoreturnto=null;
游戏对象占位符=null;
公共游戏机垃圾桶;
公共垃圾转运站;
公共游戏对象partsPanel;
公共交通部分;
公共游戏对象构建板;
公共游戏对象dragLayer;
无效唤醒()
{
dragLayer=GameObject.FindGameObjectWithTag(“dragLayer”);
buildBoard=GameObject.FindGameObjectWithTag(“Board”);
partsPanel=GameObject.FindGameObjectWithTag(“Parts”);
垃圾桶=GameObject.FindGameObjectWithTag(“垃圾桶”);
partsPanelTrans=partsPanel.transform;
//trashCanTrans=trashCan.transform;
}
#区域IPoClickHandler实现
公共void OnPointerClick(PointerEventData事件数据)
{
if(this.transform.parent.gameObject==buildBoard)
{
this.transform.SetAsLastSibling();
}
}
#端区
#区域IBeginDragHandler实现
公共void OnBeginDrag(PointerEventData事件数据)
{
//this.transform.SetAsLastSibling();
//创建占位符间隙并在布局中保持正确位置
占位符=新游戏对象();
占位符.transform.SetParent(this.transform.parent);
占位符.transform.SetSiblingIndex(this.transform.GetSiblingIndex());
LayoutElement le=placeholder.AddComponent();//添加布局元素
le.preferredWidth=此.GetComponent().preferredWidth;
le.preferredHeight=此.GetComponent().preferredHeight;
le.flexibleWidth=0;
le.flexibleHeight=0;
parentToReturnTo=this.transform.parent;//存储当前父位置
placeholder Parent=parentToReturnTo;//设置占位符游戏对象转换
GetComponent().blocksRaycasts=false;//拖动图像时关闭图像光线投射,以便查看图像后面的内容
}
#端区
#区域IDragHandler实现
public void OnDrag(PointerEventData事件数据)
{
this.transform.position=eventData.position;//将对象坐标设置为鼠标坐标
this.transform.SetParent(dragLayer.transform);//将对象弹出到dragLayer以在DropZone之间移动
}
#端区
#区域IEndDragHandler实现
公共无效OnEndRag(PointerEventData事件数据)
{
this.transform.SetParent(parentToReturnTo);//如果将对象放在dropzone之外,则将对象捕捉回原始父对象
this.transform.SetSiblingIndex(占位符.transform.GetSiblingIndex());//将卡返回占位符位置
GetComponent().blocksRaycasts=true;//启用光线投射阻止
销毁(占位符);//如果对象碰到放置区域或返回到零件面板,则删除占位符
}
#端区
}
好的,我做了一些修改,现在当我把游戏物体拖到垃圾桶上时,它会被破坏,但垃圾桶也会被破坏。我一直在努力让它工作,但仍然没有运气。这是我现在的东西,但它仍然不能正常工作
公共无效OnDrop(PointerEventData事件数据)
{
Debug.Log(eventData.pointerDrag.name+”被放在“+gameObject.name”上)
DragHandling DragHandling=eventData.pointerDrag.GetComponent();
if(拖动!=null)
{
dragHandling.parentToReturnTo=this.transform;//更改基于父对象的对象
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);
}
}
}