Skip to content

Instantly share code, notes, and snippets.

@DevEarley
Last active October 10, 2022 20:46
Show Gist options
  • Save DevEarley/84eae6b63027de3fe597000046e9a6af to your computer and use it in GitHub Desktop.
Save DevEarley/84eae6b63027de3fe597000046e9a6af to your computer and use it in GitHub Desktop.
Delegate pattern in Uniy3D

Delegate pattern in Uniy3D

  • A delegate is something that acts on another thing's behalf.

  • So if Thing A needed something, Thing B to Thing Z could deliver that to Thing A. This makes Things B to Z all delegates to Thing A. Thing A invokes a delegate function. Thing A doesn't care who runs the errand. Thing A doesn't care how it happens. As long as it happens.

Delegate Invoker

- A class that contains a Delegate Reference to a Delegate Function and can invoke it any time.

Delegate Reference

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/

- A type in C# that points to any function's address.

Delegate Function

- A function that can be invoked at any time. It's best for this function to be static, but
  it doesn't have to be.

Delegate Class - Contains one or many Delegate Functions. May be any type of class.

- Can be a Controller, Service or a Behaviour.

- Doesn't have to be attached to a GO.

- Define delegates in terms of functionality. 

- Naming format should be `"<Action>Delegate"`

This pattern allows you to assign different behaviors to a class. These behaviors are represented by delegates.

In this example, GameController will call a delegate to render some UI. But each scene needs to render UI a little bit differently. So I have defined a controller for each scene. The delegate's behavior is defined in this UI Controller.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Debug_UIController : MonoBehaviour
{
private GameController GameController;
void Start()
{
GameController = GameObject.Find("GameController").GetComponent<GameController>();
GameController.updateDebugDelegate = UpdateDebugUI;
}
void UpdateDebugUI()
{
//Do something only when in the editor
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour {
public delegate void UpdateUIDelegate();
public UpdateUIDelegate updateUIDelegate;
public delegate void UpdateDebugDelegate();
public UpdateDebugDelegate updateDebugDelegate;
void Update() {
#if DEBUG
updateDebugDelegate();
#endif
updateUIDelegate();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class World1_UIController : MonoBehaviour
{
private GameController GameController;
void Start()
{
GameController = GameObject.Find("GameController").GetComponent<GameController>();
GameController.updateUIDelegate = UpdateUI;
}
void UpdateUI()
{
//Do something
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class World2_UIController : MonoBehaviour
{
private GameController GameController;
void Start()
{
GameController = GameObject.Find("GameController").GetComponent<GameController>();
GameController.updateUIDelegate = UpdateUI;
}
void UpdateUI()
{
//Do something else
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment