c# - 如何使克隆对象不可见 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");
}
}
}
解决方案
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.
推荐阅读
- c# - 如何将多个 tiff 作为 http 响应返回?
- excel - Excel 2016 在框中用重复的问号替换复制的文本
- python - 存储用于自动上传 Youtube 的 Google 授权码
- json - 使用 Swift 从 Firebase 获取 nil 值
- shuffle - 拆分训练集和测试集时是否洗牌
- java - 如何在eclipse中调试用ant编译的java项目?
- http - 远程服务器故障时使用哪个 HTTP 状态代码?500 还是 502?
- python - 字典使用问题
- sql - 在 R 中使用 DBI 库编写 SQL 表
- vb.net - EF 尝试插入未更改的实体