CS2335
Master 1.0.0
Master 1.0.0
  • Introduction
  • Introduction
    • Introduction
      • Game Design
    • Unity - Download
    • Visual Studio - IDE
    • Unity Manual, Scripting API
  • Unity Basics
    • Unity Editor Windows
    • Behavior Components
    • 2D Project, Scenes
      • Create 2D Sprite GameObject
      • Create UI-Text GameObject
    • MonoBehavior - Base-Class
    • Create a Custom C# Script
  • Game Coding Structures
    • Games Overview
    • Unity Engine - Event Functions
    • Finite State Machines
    • UML Class Diagram
  • Animation
    • Animator Controller
    • Animation Steps
      • Optional: Dead Animation
    • PlayerController.cs V0
  • Project 1 - Player
    • Player GameObject v1
      • C# Generics, Statics
    • Player GameObject - Jump
    • PlayerController.cs V2-Jump
    • PickUp PreFabs
      • Sorting Layers
    • PlayerController.cs V3-Collide
    • GameData Version1
    • GameData Version2
    • PlayerController V4-Score
  • Project 1 Details
    • Project1 GameObjects
    • PlayerStats Version1
      • UI-Canvas
    • Utility Class
    • Simple Spawner
    • MiniGameManager
      • Logic Diagrams
      • StartButton
      • ResultsPanel
  • Project1 Enhancements
    • PickUp - SelfDestruct
    • Spawn from List of Prefabs
  • Project 2 - StateManager
    • Project 2 - Learning Objectives
      • Inspiration
        • Branching Story Structures
        • Branching Structures
        • Hero's Journey
        • Visual Novel in Unity-Links
    • Project 2 - Starter Assets
    • State Machine Framework
    • StateManager - Singleton Design Pattern
    • Interface IStateBase
    • Create SceneXState.cs
    • OptionPanel Prefab
      • UI Images: Sprite Sheets
      • Button Image-Transitions
    • Project 2 - List of Steps
    • Project 2 - Starter Code
  • Project 2 -Dialogue
    • Hide_Show_Panel Script
    • Edit OptionPanel
    • Simple DialogPrefab
    • Conversation Entry
    • SimpleDialog.cs
    • ScriptableObjects
      • Scriptable Object Factory
    • Conversation Scriptable Objects
    • DialogManager_ConvList
      • DialogManager V2
      • Coroutines: Dynamic Text
      • DialogPrefab wImage
  • Overview: Branching Logic
    • DialogTrigger
    • Dictionary Data-Structure
      • Unity PlayerPrefs Dictionary
    • GameData Version3
    • Dictionary: choiceData
      • SaveChoice, ChoicePanel
        • choiceData - examples
          • Dictionary Value to Disable Options
    • Configure UI-Button Listeners
      • NPC Animation
      • NPC Activation
    • UI-Triggered Animations
    • Simple Inventory
    • EndState Conditions
    • Script ExecutionOrder
    • Custom UnityEvents
    • PlayerStats v2
      • ModifyPlayerData
      • BuyItem
    • Text Input
  • UI Components
    • Finding Game Objects
    • Game Objects: UI vs. 2D Sprite
    • UI Elements
      • Canvas: Screen-Space Render-Mode
      • UI-Buttons To Change Scene
  • Proj4: Inventory System
    • Inventory-System
      • GameData version4
      • Install and Configure
      • Diagrams, Resources
        • Item, Gem, Potion Classes
        • Inventory Class
      • InventoryDisplay, Slot UI
        • InventoryDisplay Class
        • Slot Class
        • Hazard Class
        • Layout Groups
      • Customization Steps
        • Configure Animation
        • AddItem Button
        • Concrete Class: Food
        • MiniGame Mods
          • PlayerController Mods
      • Code: InventorySystem
        • GameData, PickUp Mods
      • Resources: Data Structures
  • Custom Unity Events
    • Event Publishing Patterns
    • Custom Event Messaging
  • Proj4: Mini-Game
    • Simplified Mini-Game
      • PlayerController_v2 Mods
        • PlayerController_v2_final
      • MiniGameManager_v2
    • MiniGame-Overview-Proj4
    • LevelManager
      • LevelManager Logic Diagram
      • LevelManager FSM
      • LoadLevel, StartLevel Logic
      • Code Framework
    • Timer
  • Project 4 - Code Mods
    • Project 4 - Steps
    • Project 4 - Code
      • Code: Final Versions
        • PlayerController Mods
          • PlayerController_v2 Mods
        • GameData - Final
        • LevelManager
        • PlayerStats - Final
        • PickUp, Hazard, ScorePickUp
        • Spawner - Final
        • CameraFollow
        • ScreenFader
        • MiniGameState
        • Example: EndState
      • MiniGameWin Logic
  • Optional, Supplemental Content
    • Optional Content
      • Particle Systems
      • Adding Audio
      • Screen Fading and Reloading
      • ScriptableObjects
      • Disable Debug Logging
      • Events and Actions
      • Saving Data - Serialization
      • Parallax Scrolling
      • Change Sprites
    • XR - Extended Reality
  • Computing Concepts
    • Programming Patterns
      • State - FSM
      • Singleton Pattern
    • C# Language
      • Variables
      • Delegates
      • Dictionary
      • Enum
      • Encapsulation
        • C# Properties
        • Access Modifiers
      • Generics < T >
      • Inheritance
      • Interface
      • List< T >
      • Polymorphism
      • Queue< T >
      • Switch-Case
      • Foreach
      • Static
      • Ternary Operator: ?
      • this
    • Diagrams
      • State Machine Framework
      • UML Class Diagrams
      • Level Manager Logic Diagram
      • Flow-Chart: NumberGame
      • FSM: NumberGame
    • Tools
    • Glossary
    • References and Resources
    • Random Thoughts
Powered by GitBook
On this page
  • Example: UI-Buttons - onClick.AddListener
  • Observer Pattern
  • Subject - Publisher:
  • Observer: Subscriber, Listener
  • C# Delegate, Event:
  • Using UnityEvent: Unity Manual
  • MVC Pattern
  • Publish / Subscribe System
  • Loose Coupling

Was this helpful?

  1. Custom Unity Events

Event Publishing Patterns

PreviousResources: Data StructuresNextCustom Event Messaging

Last updated 5 years ago

Was this helpful?

There are several similar approaches to designing a system where we can have our custom objects communicate with each other when events occur. Many Unity UI GameObject Components provide this type of event messaging functionality, but we want to understand these programming patterns so we can create this functionality with our own custom objects.

Example: UI-Buttons - onClick.AddListener

In Unity, we have worked with UI Buttons, where we create a reference to a UI-Button component in our custom C# class script, then we add our custom class method as a listener to the button component's onClick event. In the code below, we have defined a function: void GameStartActions() that we want to have executed when the button click() event occurs. It is required that our function match the delegate-type: a function with a void return-type and no input parameters, in order to add our GameStartActions as a listener to the button's onClick event. This is the pattern of behavior that we want to create within our code. When the button object is clicked, we want our custom object to be notified, we do so by creating a function that is passed as a delegate to the button's onClick event.

private Button gameStartButton;

void Start(){
gameStartButton = GameObject.Find ("StartGame").GetComponent<Button> ();
gameStartButton.onClick.AddListener (GameStartActions);
    }

public void GameStartActions(){
    // do game start things
}

Observer Pattern

The is a behavioral programming pattern that is useful for game-design situations, as discussed in: In a situation where have an event that occurs within one object component, the Subject, and we have other components, Observers, that want to be notified when this event has occurred. Often the Observer Pattern is implemented using Interfaces, however, since Unity uses C#, then we can use C# delegates and events to create a our Observer Pattern implementation.

Subject - Publisher:

The subject is the object instance that will publish notification that an event has occurred, he will maintain a list of other objects that have requested to be notified of the event. This can represent a "one - to - many" relationship, where there is one subject that notifies many observer objects. The publisher object must specify an event, which must follow a prescribed syntax called a delegate, which can hold a list of methods to be executed at some future point in time, when the event occurs.

Observer: Subscriber, Listener

An observer is an object instance that has actions to perform once an event has occurred on the subject object, it has subscribed, or registered, to be notified when the event occurs so it can perform it's actions. The observer object needs to subscribe as a listener, which means that it needs to specify a method that can be executed by the publisher when the event occurs.

C# Delegate, Event:

The C# language provides 2 related concepts that can be used to configure event-driven communication between publisher - subscriber objects.

UnityEvents are a way of allowing user driven callback to be persisted from edit time to run time without the need for additional programming and script configuration.

UnityEvents are useful for a number of things:

  • Content driven callbacks

  • Decoupling systems

  • Persistent callbacks

  • Preconfigured call events

UnityEvents can be added to any MonoBehaviour and are executed from code like a standard .net delegate. When a UnityEvent is added to a MonoBehaviour it appears in the Inspector and persistent callbacks can be added.

GameData: UnityEvent: OnPlayerDataUpdate

The image below shows that the Unity Editor Inspector Panel displays UnityEvent, like the custom GameData UnityEvent: OnPlayerDataUpdate. This allows methods to be added as Listeners from other GameObjects in a Scene. There is no limit to the number of methods that can be added as listeners to a UnityEvent. We will add listeners explicitly within our code, however, it is instructive to understand that this configuration can be done in the inspector panel. This shows the at custom UnityEvents are essentially no different that a Button's OnClick Event.

MVC Pattern

The observer pattern is similar to the Model-View-Controller pattern, where the model is an abstract representation of the data for the system. The view represents the interface-view that is rendered and displayed for the user. The controller is a component that manages input and communicates with the data-model based on user-event interaction. The view component should be automatically notified when there are changes in the data-model, so it can update the user's view of the system.

Publish / Subscribe System

We can consider these design patterns as simplified versions of a publish / subscribe system. Typically in a Publish / Subscribe system is implemented using an architecture with some type of message-manager class which functions to manage published events and distributes notifications to the subscribing components.

Loose Coupling

These patterns provide a way to reduce dependencies between objects within a system. The publisher / subject does not need implementation details about which objects have subscribed for notification of events. This means that design changes to the system will be less likely to cause errors. Interfaces provide one means of implementing loose coupling between components because it insures that functionality is implemented across all classes that implement an interface, so no details of a classes implementation are required when executing interface methods. Delegates also provide a method of implementing loose coupling because delegates allow passing functions between objects as a means of communicating.

A delegate provides specification of a custom 'Type'. It specifies the syntax for a special Type of method that can be used for event-driven communication. A delegate signature specifies the return-type, name, and parameter-list that can be used to refer to instances of methods that conform to the Delegate's specification. Delegates can specify the Type of a Method that can be specified as an input paramter to a method.

An event is a specialized, restricted instance of a delegate. It must conform to the syntax specified in the associated delegate. An event can only be invoked within the class where it is defined. When an event is invoked, it will execute all methods that have been added as listeners to the event.

Using UnityEvent:

Observer Pattern
Game Programming Patterns.
Delegate:
Event:
Unity Manual