首页 > 解决方案 > 当我开始拖动时,我的可拖动对象变得不可见

问题描述

我建立了一个全新的纸牌游戏,每件事都可以正常工作,没有任何错误,但是当我开始将我的对象拖到放置区时,我的卡由于某种原因不可见,我不确定,但我他们超出屏幕范围或类似的事情我找不到解决这个问题的方法,我尝试调试但没有错误,所以我无法找到解决这个问题的方法。这是我的可拖动对象代码

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#unity3ddrag-and-drop

解决方案


推荐阅读