Game Events

Code examples to send and listen for game events.

Send Game Event – string

Send a message with the passed game event string. This example shows how to send the ‘MyGameEvent’ game event string.

gameEvent – The game event string sent with this message

using Doozy.Engine;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    private void Start()
    {
        GameEventMessage.SendEvent("MyGameEvent");
    }
}

Send Game Event – GameObject

Send a message with the passed GameObject reference. This message is used only when the source game object reference that sent this message is needed.

source – The source game object reference that sent this message

using Doozy.Engine;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    private void Start()
    {
        GameEventMessage.SendEvent(gameObject);
    }
}

Send Game Event – string, GameObject

Send a message with a given game event string and a GameObject reference.

gameEvent – The game event string sent with this message
source – The source game object reference that sent this message

using Doozy.Engine;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    private void Start()
    {
        GameEventMessage.SendEvent("MyGameEvent", gameObject);
    }
}

Send Game Event – SystemGameEvent

Send a message with the passed SystemGameEvent. SystemGameEvents are DoozyUI special game events, also known as system events, that trigger predefined actions.

using Doozy.Engine;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    private void Start()
    {
        //Triggers the 'Back' button system function
        GameEventMessage.SendEvent(SystemGameEvent.Back);  
        
        //Exits play mode (if in editor) or quits the application if in build mode
        GameEventMessage.SendEvent(SystemGameEvent.ApplicationQuit); 
    }
}

Send Game Event – SystemGameEvent, GameObject

Send a message with the passed SystemGameEvent and a GameObject reference. SystemGameEvents are DoozyUI special game events, also known as system events, that trigger predefined actions.

using Doozy.Engine;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    private void Start()
    {
        //Triggers the 'Back' button system function
        GameEventMessage.SendEvent(SystemGameEvent.Back, gameObject);  
        
        //Exits play mode (if in editor) or quits the application if in build mode
        GameEventMessage.SendEvent(SystemGameEvent.ApplicationQuit, gameObject); 
    }
}

Send Game Events – List, GameObject

Send a list of messages, each message with the given event string and GameObject reference.

using System.Collections.Generic;
using Doozy.Engine;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    public List GameEvents = new List
                                     {
                                         "MyEvent 1",
                                         "MyEvent 2",
                                         "MyEvent 3"
                                     };

    private void Start()
    {
        GameEventMessage.SendEvents(GameEvents, gameObject);
    }
}

Listen for Game Events

The following example shows how to register and unregister a game events listener.

using Doozy.Engine;
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    private void OnEnable()
    {
        //Start listening for game events
        Message.AddListener<GameEventMessage>(OnMessage);
    }

    private void OnDisable()
    {
        //Stop listening for game events
        Message.RemoveListener<GameEventMessage>(OnMessage);
    }

    private void OnMessage(GameEventMessage message)
    {
        if (message == null) return;

        Debug.Log("Received the '" + message.EventName + "' game event.");

        if (message.Source == null) return;

        Debug.Log("'" + message.EventName + "' game event was sent by the [" + message.Source.name + "] GameObject.");
    }
}