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 c# - Unity new UI - Dynamically change the functions called by GUI elements - Game Development Stack Exchange 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);
}
}