- Home /
The question is answered, right answer was accepted
4.6 UI Dynamic button event system PointerEnter, PointerExit, PointerUp etc.
I'm trying to make a dynamic inventory system with the new UI. The issue I have with the new UI is the way buttons work and that you have to set them up inside the editor. I have tried several methods but nothing seems to be working properly.
With my inventory system I can just add slots within the editor. Each slot has a script to specify the slot ID and type. I made the entire inventory system with items and a editor to set up items and saving etc. I just need to make the InventoryControl script but I'm having lots of trouble with it. The system uses a lot components and everything works on it's own and everything is really dynamic with only a few settings.
I don't wanna have to set up a EventTrigger and set them up for each slot separate. I found this http://gamedev.stackexchange.com/questions/83027/unity-4-6-gui-dynamically-change-the-functions-called-by-gui-elements but the problem with it is that it doesn't work properly for other events then pointerdown. The object you get with baseEvent.selectedObject is Null for the other events.
So what I basically need is a script I can attach to all slots that will detect mouse events on the 4.6 UI button. Then I can just dispatch a event with the mouse event and the slot and use that in my InventoryControl script. Another good alternative is just 1 script that detects mouse events on all UI buttons but I need to be able to get the gameobject with it and with the code in the link above that doesn't work.
The only option I haven't tested and found online is using a ray to detect it. But that seems inefficient and a terrible way to do this.
This is the broken code from the link above modified to work with my event listener and stuff. The baseEvent.selectedObject is Null for all Pointer events.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class ButtonEvent : MonoBehaviour {
private EventTrigger eventTrigger;
void Start() {
eventTrigger = GetComponent<EventTrigger>();
if (eventTrigger == null) {
return;
}
setupEventListener(EventTriggerType.PointerClick, new UnityEngine.Events.UnityAction<BaseEventData>(ClickEvent));
setupEventListener(EventTriggerType.PointerDown, new UnityEngine.Events.UnityAction<BaseEventData>(PointerDownEvent));
setupEventListener(EventTriggerType.PointerUp, new UnityEngine.Events.UnityAction<BaseEventData>(PointerUpEvent));
setupEventListener(EventTriggerType.PointerEnter, new UnityEngine.Events.UnityAction<BaseEventData>(PointerEnterEvent));
setupEventListener(EventTriggerType.PointerExit, new UnityEngine.Events.UnityAction<BaseEventData>(PointerExitEvent));
}
private void setupEventListener(EventTriggerType eventID, UnityEngine.Events.UnityAction<BaseEventData> listener) {
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = eventID;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(listener);
eventTrigger.delegates.Add(entry);
}
public void ClickEvent(BaseEventData baseEvent) {
EventListener.dispatch<GameObject>("ui_click", baseEvent.selectedObject);
}
public void PointerDownEvent(BaseEventData baseEvent) {
EventListener.dispatch<GameObject>("ui_pointerdown", baseEvent.selectedObject);
}
public void PointerUpEvent(BaseEventData baseEvent) {
EventListener.dispatch<GameObject>("ui_pointerup", baseEvent.selectedObject);
}
public void PointerEnterEvent(BaseEventData baseEvent) {
EventListener.dispatch<GameObject>("ui_pointerenter", baseEvent.selectedObject);
}
public void PointerExitEvent(BaseEventData baseEvent) {
EventListener.dispatch<GameObject>("ui_pointerexit", baseEvent.selectedObject);
}
}
The answer below from Bored$$anonymous$$ormon works perfectly. Just implement the interfaces of the events you need and override the methods.
Answer by Kiwasi · Dec 18, 2014 at 05:45 PM
I just finished a youtube video on this.
The key solution you are after is implementing the event system via script. Add using UnityEngine.EventSystems to the top of your script. Then implement the interfaces from there. This eliminates the need to add listeners altogether. For this to work it should be attatched to the actual UI element detecting the events.
public class ButtonEvent : MonoBehaviour, IClickHandler, IPointerEnterHandler {
Thanks a lot for this. I didn't know about those interfaces and I think I can make something with this. I will try it out tomorrow :)
Thanks a lot, this was perfect and a lot easier than adding the EventTrigger and events dynamically.
Follow this Question
Related Questions
[4.6 - UI] Calling function on button click via script 1 Answer
Unity 4.6 UI button highlight color staying after button clicked 11 Answers
[4.6 - UI] How to call an event with button when the pointer is up the button image. 1 Answer
HELP! Buttons stop working when made a child of a "Canvas Group" empty in 4.6.3f1? 4 Answers
4.6 UI EventSystem Handler Redirection? 0 Answers