Мой перетаскиваемый объект, когда я начинаю перетаскивать, становится невидимым - PullRequest
0 голосов
/ 09 октября 2019

Я создаю совершенно новую карточную игру, все работает без ошибок, но когда я начинаю перетаскивать свой объект, в этом случае карта попадает в зону сброса, моя карта почему-то не видна, я неконечно, но я думаю, что они выходят за пределы экрана или что-то в этом роде, я не могу найти способ исправить это, я пытаюсь отладить, но нет ошибки, поэтому я не могу найти способ решить эту проблему. Это мой код для перетаскиваемого объекта

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);
}


}
...