- Home /
Adding custom BaseEventData to UnityEngine.EventSystems NOT Unity.Events
Hi all,
I am currently trying to use the UnityEngine.EventSystems (developed for the UI but which also has a 3D Physics Raycaster) in a game. The reason I want to use it is to make use of all the already available (an handy) editor features of this system over the UnityEngine.Events.
However, the system has been developed for the UI and I would need to be able to add events to the system which trigger on certain game actions.
For example, in the EventTrigger component I would like to see in the dropdown menu a "OnUKeyPressed" events in addition to all the others (eg: PointerClick).
There is a wealth of pretty good tutorials out there for the UnityEngine.Events but not that much for the UnityEngine.EventSystems.
Any help would be much appreciated !
so you want a custom event like button has "OnClick" you want your own am i right ?
Yes, for example I would like an "OnU$$anonymous$$eyPressed" event to register when the U key is pressed when the mouse is over an object which would trigger the Use() method on the object.
Answer by ExtinctSpecie · Apr 21, 2017 at 11:05 PM
as i understood from your comment you need something that can register events and use them
ill show you 2 examples try to play with them since i dont have the time
//first example
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class MyClickTrigger : MonoBehaviour , IPointerClickHandler
{
#region IPointerClickHandler implementation
public void OnPointerClick (PointerEventData eventData)
{
MyOwnEventTriggered ();
}
#endregion
//my event
[Serializable]
public class MyOwnEvent : UnityEvent { }
[SerializeField]
private MyOwnEvent myOwnEvent = new MyOwnEvent();
public MyOwnEvent onMyOwnEvent { get { return myOwnEvent; } set { myOwnEvent = value; } }
public void MyOwnEventTriggered()
{
onMyOwnEvent.Invoke();
}
}
///add a collider to the object as well so the OnPointerClick can work
//second example
create a script like this one
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI.Extensions;
using UnityEngine.Events;
public class MyEventManager : MonoBehaviour {
private Dictionary<string,UnityEvent> eventDictionary;
private static MyEventManager eventManager;
public static MyEventManager instance
{
get
{
if (!eventManager)
{
eventManager = FindObjectOfType (typeof (MyEventManager)) as MyEventManager;
if (!eventManager)
{
Debug.LogError ("There needs to be one active EventManger script on a GameObject in your scene.");
}
else
{
eventManager.Init ();
}
}
return eventManager;
}
}
void Init ()
{
if (eventDictionary == null)
{
eventDictionary = new Dictionary<string, UnityEvent>();
}
}
public static void StartListening (string eventName, UnityAction listener)
{
UnityEvent thisEvent = null;
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
{
thisEvent.AddListener (listener);
}
else
{
thisEvent = new UnityEvent ();
thisEvent.AddListener (listener);
instance.eventDictionary.Add (eventName, thisEvent);
}
}
public static void StopListening (string eventName, UnityAction listener)
{
if (eventManager == null) return;
UnityEvent thisEvent = null;
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
{
thisEvent.RemoveListener (listener);
}
}
public static void TriggerEvent (string eventName)
{
UnityEvent thisEvent = null;
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
{
thisEvent.Invoke ();
}
}
}
and then you register an event this way
public class LastPageTriggers : MonoBehaviour {
void OnEnable()
{
MyEventManager.StartListening ("lastPage",LastPage);
}
void OnDisable()
{
MyEventManager.StopListening ("lastPage", LastPage);
}
void LastPage()
{
Debug.Log ("Last Page function");
}
public static void TriggerLastPage()
{
MyEventManager.TriggerEvent ("lastPage");
}
}
//do not forget to stop listening on disable
//if you want to invoke the method LastPage from anywhere you just call //the static method of TriggerLastPage
//so somewhere else you would have
void Update () {
if (Input.GetKeyDown ("u"))
{
MyEventManager.TriggerEvent ("lastPage");
//or LastPageTriggers.TriggerLastPage();
}
}
Cheers thanks !
I like it but what I am really trying to achieve is the following:
As you can see I want to be able to literally add a customise event to the Unity EventSystem and be able to access it along with all the others in the drop down menu.
$$anonymous$$aybe this is not possible or too complicated, but I would thing that it is easier that making a event system from scratch.
I suppose I will have to trigger my event independently from where the events from the EventSystem are triggered (I assume using the BaseInput$$anonymous$$odule) but I would still be able to find a way to feed in a new event into the system.
i dont know if thats possible i've only seen that in swift language where you can create extensions what you can do tho is create a script called OnU$$anonymous$$eyPressed with an update function that checks for "u" if pressed and then invoke the method