Hi,
ich hab noch ein Problem.
Hier erstmal mein Drag&Drop Skript
Code: Alles auswählen
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ItemSlotData : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler
{
public Item item { get; set; }
public int amount { get; set; }
public int SlotID { get; set; }
private GameObject DragImage { get; set; }
void Awake()
{
this.item = null;
this.amount = 0;
this.SlotID = 0;
this.DragImage = null;
}
public void FillData(Item item, int amount, int slotid)
{
this.item = item;
this.amount = amount;
this.SlotID = slotid;
}
public void OnBeginDrag(PointerEventData eventData)
{
Destroy(this.DragImage);
if (this.item != null)
{
Image img = this.transform.Find("Item").GetComponent<Image>();
if (img != null)
{
this.DragImage = Instantiate(this.transform.parent.Find("DragDrop").gameObject);
this.DragImage.transform.SetParent(GameObject.Find("GUI/Canvas").transform);
this.DragImage.SetActive(true);
this.DragImage.GetComponent<Image>().sprite = this.item.Sprite;
this.DragImage.GetComponent<Image>().color = new Color(1, 1, 1, 1);
this.DragImage.transform.position = eventData.position;
Debug.Log(this.DragImage);
try
{
this.DragImage.GetComponent<ItemSlotData>().FillData(this.item, this.amount, this.SlotID);
}
catch { };
}
}
}
public void OnDrag(PointerEventData eventData)
{
if (this.DragImage != null)
{
this.DragImage.transform.position = eventData.position;
}
}
public void OnEndDrag(PointerEventData eventData)
{
Destroy(this.DragImage);
}
public void OnDrop(PointerEventData eventData)
{
Debug.Log("BLA"+this.SlotID);
ItemSlotData droppendItem = eventData.pointerDrag.GetComponent<ItemSlotData>();
}
}
Ich hab also die Drag- und Drop Routinen im selben Skript.
Eigendlich solle in der OnDrop()-Methode doch this.SlotID (der Slot wo hingezogen wird) und droppendItem.SlotID (der Slot von dem aus gezogen wurde) unterschiedlich sein.
Bei mir sind aber immer beide gleich.
Wo liegt mein Denkfehler?
PS: Ich hab grad rausgefunden, dass das DragImage wohl immer mit der Maus mitbewegt wird und deshalb dann immer das Ziel von OnDrop ist. Wie kann ich das verhindern? Kann man die DragImage irgendwie als Ziel von OnDrop verhindern?
Gruß