Custom UnityEvents

Button.onClick Event: In our game application, we have several situations where it will be beneficial to define events for an object. We have already worked with the UI-Button object's Click() method which raises the OnClick event. In these cases, we defined an Event-handler method that we wanted to be executed when the onClick event occurred.

Event: AddListener( ) In order to associate our Event-handler method with the Button's onClick event, we had to use the AddListener( ) method. AddListener specifies a delegate as the input parameter, where a delegate defines the format of a function/method that matches a specific function/ method signature.

GameData Custom Events:

Since our GameData object instance is persisted across all scenes, then we need to be careful not to create code dependencies between GameData and objects that are only in one specific scene. GameData will be an event publisher, objects within a scene can subscribe to GameData events, to recieve notification (and event data) when events have occurred.

Custom UnityEvents: onPlayerDataUpdate, onGameStateUpdate

For the events above, can you identify a likely publisher object. Can you identify one or more likely subscribers for each message?

Define the Events

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;  //MUST INCLUDE THIS

//11/9/2020 Version 4 - custom

//Singleton - one and only 1 ever in existance
//global variable to allow easy access
public class GameData : MonoBehaviour
{
    //DEFINE, INITIALIZE CUSTOM EVENTS
    public UnityEvent onPlayerDataUpdate = new UnityEvent();
    public UnityEvent onGameStateUpdate = new UnityEvent();

//rest of class data

Invoke the Events

//updated TakeDamage: 
    public void TakeDamage(int value)
    {
        health -= value;
        if (health < 0) health = 0;  //makes sure health !< 0
        Debug.Log("Health is updated " + health);
        InvokePlayerDataUpdate();   //INVOKE, call method below
    }

    //Execute custom event if there are listeners
    //Create a method to Invoke the Event
    public void InvokePlayerDataUpdate()
    {
        if (onPlayerDataUpdate != null)
        {
            onPlayerDataUpdate.Invoke();
        }
    }

AddListeners to Events - PlayerStats.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //Directive add for working with UI elements

public class PlayerStats : MonoBehaviour
{

    [SerializeField]
    Text healthText, scoreText;

    CanvasGroup cg;

    // Start is called before the first frame update
    private void Start()
    {
        UpdateDisplay();
        //Now PlayerStats gets updated every time PlayerData is modified
        GameData.instanceRef.onPlayerDataUpdate.AddListener(UpdateDisplay); 
    }

 
    public void UpdateDisplay()
    {
        scoreText.text = "Score: " + GameData.instanceRef.Score;
        healthText.text = "Health: " + GameData.instanceRef.Health;
    }

    
} //end class PlayerStats

Last updated