c# - 当我开始拖动时,我的可拖动对象变得不可见
问题描述
我建立了一个全新的纸牌游戏,每件事都可以正常工作,没有任何错误,但是当我开始将我的对象拖到放置区时,我的卡由于某种原因不可见,我不确定,但我他们超出屏幕范围或类似的事情我找不到解决这个问题的方法,我尝试调试但没有错误,所以我无法找到解决这个问题的方法。这是我的可拖动对象代码
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
public Transform parentToReturnTo = null;
public Transform placeholderParent = null;
GameObject placeholder = null;
public void OnBeginDrag(PointerEventData eventData) {
Debug.Log ("OnBeginDrag");
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() );
parentToReturnTo = this.transform.parent;
placeholderParent = parentToReturnTo;
this.transform.SetParent( this.transform.parent.parent );
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++) {
if(this.transform.position.x < placeholderParent.GetChild(i).position.x) {
newSiblingIndex = i;
if(placeholder.transform.GetSiblingIndex() < newSiblingIndex)
newSiblingIndex--;
break;
}
}
placeholder.transform.SetSiblingIndex(newSiblingIndex);
}
public void OnEndDrag(PointerEventData eventData) {
Debug.Log ("OnEndDrag");
this.transform.SetParent( parentToReturnTo );
this.transform.SetSiblingIndex( placeholder.transform.GetSiblingIndex() );
GetComponent<CanvasGroup>().blocksRaycasts = true;
Destroy(placeholder);
}
}
解决方案
推荐阅读
- c# - StreamWriter 不会将任何文本写入文件
- java - 如何拥有一个包含 JFrame 的所有 UI 元素的类?
- c++ - 如何在堆栈上为指向对象的指针分配内存?
- ios - 父列表视图和子列表视图之间的 SwiftUI 导航会导致设备上的额外刷新,但不会在模拟器中
- python - 在一个语句中计算多个逻辑运算符“或”、“和”
- html - margin-bottom 适用于 Chrome,但不适用于 Firefox
- mule - 如何检查mule的请求配置中传递了什么标头
- javascript - TypeError:无法读取未定义的属性“signInWithPhoneNumber”
- c - 以用户输入valgrind问题结束的c线程程序
- php - 从 PHP 中的 mySQL 存储过程获取返回值