So, i’m rewatching this lecture several times and trying to really get into the code, because I want to have a proper understanding of what’s happening and why everything is set up the way that it it. So, I have a few questions.
For this method, in DragItem
private IDragDestination<T> GetContainer(PointerEventData eventData)
{
if (eventData.pointerEnter)
{
var container = eventData.pointerEnter.GetComponentInParent<IDragDestination<T>>();
return container;
}
return null;
}
Does this not just get the parent container of “this”? In other words, since we have this inventory object attached to the cursor during the unity DragEvent, does “pointer enter” not refer to said game object? If this refers to whatever UI element that the mouse is hovering over, how do we know that it’s going to be getting a UI slot, and not, say, just the inventory canvas and therefore cause a glitch? The only qualifier prior to launching this method is
if (!EventSystem.current.IsPointerOverGameObject())
{
container = parentCanvas.GetComponent<IDragDestination<T>>();
}
//note, parentCanvas = GetComponentInParent<Canvas>(); so it's just the default UI canvas
so does that not just point to the general canvas for the inventory UI? All it’s looking for is ANY UI element, correct? I’m really confused about how this works. I see that each Inventory Slot has a “Canvas Group” component but a canvas group is not a canvas.
I have a lot of other small questions too, I hope that we go over all of this in more detail. But that’s I guess the main one for now, as the whole thing (in my mind’s eye) doesn’t seem to make sense for how it works, and i really want to know.
Sorry, one more question lol. So for this method in DragItem:
private void DropItemIntoContainer(IDragDestination<T> destination) { if (object.ReferenceEquals(destination, source)) return; var destinationContainer = destination as IDragContainer<T>; var sourceContainer = source as IDragContainer<T>; // Swap won't be possible if (destinationContainer == null || sourceContainer == null || destinationContainer.GetItem() == null || object.ReferenceEquals(destinationContainer.GetItem(), sourceContainer.GetItem())) { AttemptSimpleTransfer(destination); return; } AttemptSwap(destinationContainer, sourceContainer); }
Why are we attempting to place an item into the new container if is potentially fails the check of “Destination == null”? doesn’t that, like… not work?