Unity Using Events and Building Effective UI Systems

September 7, 2021

Unity Events

UI Systems


Title:
C# Events in Unity! – Intermediate Scripting Tutorial

By:
Unity


Youtube Link – Tutorial #1

Description:
Brief introduction to using Events in Unity.


Title:
How To Build An Event System in Unity

By:
Game Dev Guide


Youtube Link – Tutorial #2

Description:
Quick showing of implementing a basic Event system in Unity for gameplay reasons.


Title:
Delegates, Events, Actions and Funcs – The Observer Pattern (Unity & C#)

By:
One Wheel Studio


Youtube Link – Tutorial #3

Description:
Covers Events, as well as Delegates, Actions, and Funcs specifically through the Observer Pattern in Unity.


Title:
Game Architecture Tips – Event Driven UI – Unity

By:
Dapper Dino


Youtube Link – Tutorial #4

Description:
An Event system built in Unity specifically with a focus on UI.


Title:
Building Unity UI that scales for a real game – Prefabs/Scenes?

By:
Jason Weimann


Youtube Link – Tutorial #5

Description:
General coverage of building effective UI systems in Unity.


Overview

I wanted to delve further into using Event systems in Unity specifically when building UI systems, so I collected a few resources I thought would help with researching that. These tutorials cover everything from the basic foundations of Events themselves to fully fledged UI system tutorials near the end implementing those tools and concepts covered in the earlier tutorials.

I think Event heavy systems are very effective ways to build out UI in games, so I want to get a better grasp of setting that up. I especially think the final tutorial by Jason Weimann will help bring those topics together as well as cover other important factors for building larger scale UI systems.

via Blogger http://stevelilleyschool.blogspot.com/2021/09/unity-using-events-and-building.html

Observer Pattern in Unity with C# – by Jason Weimann

April 22, 2021

Observer Pattern

Game Dev Patterns


Title:
Observer Pattern – Game Programming Patterns in Unity & C#

By:
Jason Weimann


Youtube – Tutorial

Description:
Introduction to the observer pattern and implementing it in Unity through C#.


Overview

This tutorial covers the basics of the observer pattern in game development with two ways of implementing it in Unity. The first approach is relatively simple just to establish the concept, whereas the second approach uses events with C# to create a more flexible system.

Observer Pattern Basics

An object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes (usually by calling one of their methods)

Example Uses in Games:

UI elements updating when data behind them changes

Achievement systems

Implementation #1: Inheriting Observer and Subject Classes

Observer Class:

Abstract class your observers will inherit from

OnNotify() method that accepts a value and notification type


Subject Class:

Abstract class your subjects will inherit from

Hold information on their list of observers they report to

RegisterObserver() method to add Observers to list of those reporting to

Notify() passes a value and notification type on to all the observers they report to in their list, in turn calling their OnNotify methods with the passed on data

This example gets the point across, but is not particularly well suited for Unity or C# projects. Requires a base class on all observers and subjects (although could possibly be changed to using an interface system).

Implementation #2: Using Events

Subject objects create a ‘static event Action‘, which they then call when the requirements are met.

Observer objects have their own methods to perform when those same requirements are met, and they are connected by having the observers subscribe their relevant methods to that same ‘static event’.

This reduces the direct coupling between the observers and subjects, as well as any other objects involved. Should profile this as calling events every frame can start to lead to performance loss. As always, also need to be careful to properly unsubscribe methods from events when needed (such as when deactivating or destroying objects).

via Blogger http://stevelilleyschool.blogspot.com/2021/04/observer-pattern-in-unity-with-c-by.html

Using Unity Events and Events and Deciding Between Them

September 7, 2020

Unity

Events and Unity Events

Events or UnityEvents?????????

Youtube – Link

By: Jason Weimann


Overview

Events are something I want to utilize more consistently and more effectively in my code setups, so this seemed like a good supporting block to look into to understand their usage better. It also covers the usage and differences between Unity Events and the standard C# Events, so this gives me more options to explore and utilize as well more so than some other sources.

Unity Event Systems: Introductory Research

December 17, 2019

Unity Event Systems

Basics, Setup, and Tutorials

ADDENDUM: An EVEN MORE AWESOME Event System, by viewers!

Link – Tutorial 1

By: quill18creates


C# Events

Link – Tutorial 2

By: Jonathan Weinberger {ON Unity Learn Premium}


Tutorial 1

They created a public abstract class, Event, that all types of events will just inherit from. This helps keep all your events nice and similar, as well as making sure they all have key methods such as those necessary for registering and unregistering listeners. Since each event type is its own version of this base event, they can each hold a different collection of listeners, which makes sense since you only want certain behaviors to happen when certain events happen (such as a unit dying).

Inheritance and Static Fields in C#

This was an interesting interaction that I did not know it worked this way. When a base class in C# contains a static field, and then you create a new class that inherits from this class, it actually creates its own separate and unique version of that static field (as opposed to their just being one overall static field contained in the base class for all classes that inherit it). This has pros and cons. The pros are that it is helpful when you want the derived classes to be able to hold data only amongst themselves. The cons are obviously that you will have to do something a bit extra if you want something to contain all the information from all derived classes from that original base class.

Tutorial 2

This is just a very basic intro to events in C# I found on Unity Learn premium. This was beneficial to just cover some basic rules of events in general again.

General Events Notes

  • allow us to create broadcast system so other objects can subscribe/unsubscribe
  • need delegate type
  • will cause an error if it’s null when called
  • helps classes be self contained (since they do not rely on communication directly with another object)
  • every instance of an object that wants to be subscribed to the event must make sure that it itself subscribes
  • have inherent security (as opposed to delegates)
  • General Rule: always have an unsubscribe for every subscribe

Final Notes

Unity Learn actually has a ton of information, tutorials, and practice for learning about events, so I will most likely look to follow some of them in the future. They range from a few more really basic ones such as the one I just checked out, to more challenging ones, and even advanced ones that are much more project based as well. I really think this type of system will be useful for my current personal project, so I want to understand it well in an effort to properly utilize it.