如何使用C#在Unity中将对象解组到其父对象?

如何使用C#在Unity中将对象解组到其父对象?,c#,unity3d,drag-and-drop,puzzle,C#,Unity3d,Drag And Drop,Puzzle,当我拖动“block1”组时,我将其作为一个整体拖动。但我想做的是,当我把它放到“槽”中时,我想将它解组,得到所有的“子(立方体)”并将其重新设置为“槽”的父级。将每个立方体连接到每个插槽。我该怎么做?设置transform.parent=null只需从“块”面板中删除“块1” 以下是脚本: 使用UnityEngine; 使用系统集合; 使用UnityEngine.EventSystems; 公共类DragHandler:MonoBehavior、IBeginDragHandler、IDra

当我拖动“block1”组时,我将其作为一个整体拖动。但我想做的是,当我把它放到“槽”中时,我想将它解组,得到所有的“子(立方体)”并将其重新设置为“槽”的父级。将每个立方体连接到每个插槽。我该怎么做?设置transform.parent=null只需从“块”面板中删除“块1”

以下是脚本:

使用UnityEngine;
使用系统集合;
使用UnityEngine.EventSystems;
公共类DragHandler:MonoBehavior、IBeginDragHandler、IDragHandler、IENDragHandler{
公共静态游戏对象块;
矢量3起始位置;
转换起始租金;
公共静态游戏对象立方体;
#区域IBeginDragHandler实现
公共void OnBeginDrag(PointerEventData事件数据)
{
棋子=游戏对象;
startPosition=transform.position;
startParent=transform.parent;
GetComponent().blocksRaycasts=false;
}
#端区
#区域IDragHandler实现
public void OnDrag(PointerEventData事件数据)
{
transform.position=eventData.position;
}
#端区
#区域IEndDragHandler实现
公共无效OnEndRag(PointerEventData事件数据)
{
工件=零;
GetComponent().blocksRaycasts=false;
if(transform.parent==startParent){
transform.position=起始位置;
GetComponent().blocksRaycasts=true;
}
}
#端区

}
我不确定您在这里要问什么,但要设置一个对象,有几种方法

public class ParentExample : MonoBehaviour
{
    GameObject Child;   
    GameObject ObjectToBeParent;

    void Start()
    {
        //This sets the parent without the worldPos "staying"
        Child.transform.SetParent(ObjectToBeParent.transform, false);
        //This sets the parent
        Child.transform.parent = ObjectToBeParent.transform;
    }
}

我不太确定你在问什么,但要设置一个对象,有几种方法

public class ParentExample : MonoBehaviour
{
    GameObject Child;   
    GameObject ObjectToBeParent;

    void Start()
    {
        //This sets the parent without the worldPos "staying"
        Child.transform.SetParent(ObjectToBeParent.transform, false);
        //This sets the parent
        Child.transform.parent = ObjectToBeParent.transform;
    }
}