- Home /
Other
Call Pointer Enter through GameObject
I am trying to make a drag and drop system for my inventory. But. The slots are underneath the items. And the Pointer Enter trigger is on the slots. So. Pointer Enter isn't being called. How do i fix this? Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class slotDragDetection : MonoBehaviour {
public int Slot;
bool pointerEntered = false;
bool pointerClicked = false;
void Awake() {
EventTrigger trigger = gameObject.AddComponent<EventTrigger> ();
EventTrigger.Entry entry = new EventTrigger.Entry ();
EventTrigger.Entry entry2 = new EventTrigger.Entry ();
EventTrigger.Entry entry3 = new EventTrigger.Entry ();
EventTrigger.Entry entry4 = new EventTrigger.Entry ();
//Entry one
entry.eventID = EventTriggerType.PointerEnter;
entry.callback.AddListener( ( data ) => { onPointerEnter( (PointerEventData)data ); } );
trigger.triggers.Add( entry );
//Entry two
entry2.eventID = EventTriggerType.PointerExit;
entry2.callback.AddListener( ( data ) => { onPointerExit( (PointerEventData)data ); } );
trigger.triggers.Add (entry2);
//Entry three
entry3.eventID = EventTriggerType.PointerDown;
entry3.callback.AddListener( ( data ) => { onPointerDown( (PointerEventData)data ); } );
trigger.triggers.Add (entry3);
//Entry four
entry4.eventID = EventTriggerType.PointerUp;
entry3.callback.AddListener( ( data ) => { onPointerUp( (PointerEventData)data ); } );
trigger.triggers.Add (entry4);
}
public void onPointerEnter(PointerEventData data) {
pointerEntered = true;
Debug.Log ("Entered");
}
public void onPointerExit(PointerEventData data) {
pointerEntered = false;
Debug.Log ("Exited");
}
public void onPointerDown(PointerEventData data) {
pointerClicked = true;
}
public void onPointerUp(PointerEventData data) {
pointerClicked = false;
}
}
Edit: Here's my 2nd script for dragging the items: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;
public class dragItems : MonoBehaviour {
Transform defaultSlot;
float offsetX;
float offsetY;
void Awake() {
EventTrigger trigger = gameObject.AddComponent<EventTrigger> ();
EventTrigger.Entry entry = new EventTrigger.Entry ();
EventTrigger.Entry entry2 = new EventTrigger.Entry ();
EventTrigger.Entry entry3 = new EventTrigger.Entry ();
//Entry one
entry.eventID = EventTriggerType.BeginDrag;
entry.callback.AddListener( ( data ) => { onBeginDrag( (PointerEventData)data ); } );
trigger.triggers.Add( entry );
//Entry two
entry2.eventID = EventTriggerType.Drag;
entry2.callback.AddListener( ( data ) => { onDrag( (PointerEventData)data ); } );
trigger.triggers.Add (entry2);
//Entry three
entry3.eventID = EventTriggerType.EndDrag;
entry3.callback.AddListener( ( data ) => { onEndDrag( (PointerEventData)data ); } );
trigger.triggers.Add (entry3);
}
public void onBeginDrag( PointerEventData data) {
offsetX = transform.position.x - Input.mousePosition.x;
offsetY = transform.position.y - Input.mousePosition.y;
if (transform.parent.parent.parent != null && transform.parent.parent.parent.CompareTag ("inventory")) {
defaultSlot = transform.parent.transform;
transform.SetParent (transform.parent.parent.parent);
}
}
public void onDrag(PointerEventData data) {
transform.position = new Vector3 (offsetX + Input.mousePosition.x, offsetY + Input.mousePosition.y);
}
public void onEndDrag(PointerEventData data) {
transform.position = defaultSlot.transform.position;
transform.SetParent (defaultSlot);
}
}
Answer by Feyyyyy · Mar 20, 2017 at 07:21 PM
Hi,
If I understad you corectly, there should be an interactable option on your items. Uncheck them. So your slots will start detecting mouse events instead of items.
Edit According to your comment, in your item class you ca do something like that;
public class ItemUI {
public slotDragDetection currentSlot;
public void onPointerEnter(PointerEventData data) {
currentSlot.onPointerEnter(data);
}
public void onPointerExit(PointerEventData data) {
currentSlot.onPointerExit(data);
}
public void onPointerDown(PointerEventData data) {
currentSlot.onPointerDown(data);
}
public void onPointerUp(PointerEventData data) {
currentSlot.onPointerUp(data);
}
}
Now your slots aren't exactly receiving the events but this can work for your situation. I can't be sure that it will work for you, since I do not know your needs exactly.
But. I need to drag the items aswell as make the slots detect the cursor