A simple message based eventsystem for unity
Just attach the SimpleEvents.cs
script to a GameObject in your scene. Done!
The SimpleEvents.cs
script supports all data types by using an array of objects. This means that you can pass any type of data with an event.
Call Simple Event
using UnityEngine;
using SimpleEvents;
public class ExampleScript : MonoBehaviour
{
void SomeMethod()
{
SimpleEvents.Event("sampleName");
}
}
Listening for Simple Events
using UnityEngine;
using SimpleEvents;
public class ExampleScript : MonoBehaviour
{
private void OnEnable()
{
SimpleEvents.onEvent += SimpleEventReceived;
}
private void OnDisable()
{
SimpleEvents.onEvent -= SimpleEventReceived;
}
void SimpleEventReceived(string eventName, object[] values)
{
if (eventName == "sampleName")
{
//do Something...;
}
}
}
Call Simple Event with integer value
using UnityEngine;
using SimpleEvents;
public class ExampleScript : MonoBehaviour
{
void SomeMethod()
{
SimpleEvents.SimpleEvent("sampleName", 123, 4.5f);
}
}
Listening for Simple Events
using UnityEngine;
using SimpleEvents;
public class ExampleScript : MonoBehaviour
{
private void OnEnable()
{
SimpleEvents.onEvent += SimpleEventReceived;
}
private void OnDisable()
{
SimpleEvents.onEvent -= SimpleEventReceived;
}
void SimpleEventReceived(string eventName, object[] values)
{
if (eventName == "sampleName" && values.Length == 2 && values[0] is int intValue && values[1] is float floatValue)
{
//do Something with intValue and floatValue...;
}
}
}
If something does not work, make sure you have added the SimpleEvents script to an active GameObject in your scene.