首页 > 解决方案 > 如何使克隆对象不可见 Unity

问题描述

1.当我开始拖动对象它的克隆本身时我遇到了一个问题,我需要在面板区域(我的对象在那里重生)而不是在 dropzone 我试图找到解决方案我可以让克隆不可见吗?如果我可以怎么办?这是代码:

enter code here

公共类 DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public enum Slot { ileri, sağa, sola, fonksiyon };

public Slot typeofMove;
public Transform ParentreturnTo = null;
public Transform PlaceholderParent = null;
GameObject placeholder = null;
public GameObject ileriprefab;
public GameObject x;
public GameObject panel;
public void OnBeginDrag(PointerEventData eventData)
{



    placeholder = new GameObject();

    placeholder.transform.SetParent(this.transform.parent);
    LayoutElement le = placeholder.AddComponent<LayoutElement>();
    le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
    le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
    le.flexibleWidth = 0;
    le.flexibleHeight = 0;

    placeholder.transform.SetSiblingIndex(this.transform.GetSiblingIndex());
    //sibling index kartı aldıgımız yeri döndürür.

    Debug.Log("OnBeginDrag");
    ParentreturnTo = this.transform.parent;
    PlaceholderParent = ParentreturnTo;
    this.transform.SetParent(this.transform.parent.parent);


    if (this.transform.parent.position == GameObject.FindGameObjectWithTag("carddroparea").transform.position)
    {
        x = Instantiate(moveforwardprefab, moveforvardprefab.transform.position, Quaternion.identity);



        x.transform.SetParent(PlaceholderParent.transform);

        //im trying to saying here if the object "this" in drop zone dont instantiate it or make it invisible ??????? but its not working 
    }


    GetComponent<CanvasGroup>().blocksRaycasts = false;












}

public void OnDrag(PointerEventData eventData)
{

    Debug.Log("OnDrag");
    this.transform.position = eventData.position;
    if (placeholder.transform.parent != PlaceholderParent)
        placeholder.transform.SetParent(PlaceholderParent);

    int newSiblingIndex = PlaceholderParent.childCount;
    for (int i = 0; i < PlaceholderParent.childCount; i++)
    {
        //**parentreturnto. getchild(i)**//

        if (this.transform.position.x < PlaceholderParent.GetChild(i).position.x)
        {
            newSiblingIndex = i;
            //     placeholder.transform.SetSiblingIndex(i);
            if (PlaceholderParent.transform.GetSiblingIndex() < newSiblingIndex)
                newSiblingIndex--;
            break;


        }


    }
    placeholder.transform.SetSiblingIndex(newSiblingIndex);

}

public void OnEndDrag(PointerEventData eventData)
{



    Debug.Log("OnEndDrag");
    this.transform.SetParent(ParentreturnTo);

    this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());
    //Kartın alındıgı yere konulması için gerekli

    GetComponent<CanvasGroup>().blocksRaycasts = true;
    Destroy(placeholder);
    if (this.transform.parent.position == GameObject.FindGameObjectWithTag("panel").transform.position)
    {

        Destroy(x);

        Debug.Log("destroyed");

    }

    if (x.transform.parent.position == GameObject.FindGameObjectWithTag("carddroparea").transform.position)
    {

        Destroy(x);

        Debug.Log("destroyed");

    }






}

}

标签: c#objectunity3dcloneinvisible

解决方案


If you just want to make a GameObject invisible there are many ways to do it. You can, for example:

yourGameObject.SetActive(false);

You can also deactivate your gameobject Renderer

yourGameObject.GetComponent<Renderer>().enabled = false;

how to adapt it to your code is up to you.


推荐阅读