⚛️
UniFlux
  • Introduction
  • ❓Getting Started
    • What is UniFlux
    • Setup
  • 🧩Components
    • MonoFlux
    • MethodFluxAttribute
    • StateFluxAttribute
    • Extension Key
  • ⚠️Use Cases
    • Service Locator Alternative
    • Singleton Alternative
    • Messaging Alternative
    • Avoiding Prop Drilling
  • 📚Tutorials
    • How to use a custom Key?
    • How To See Subscriptions ?
    • 📽️(Video) How to Use UniFlux on Unity
  • Links and References
    • 🔗References
    • 🌍Unity Asset Store
    • 🎲Made With UniFlux
    • 📦Github
    • 😎About Me
Powered by GitBook
  1. Use Cases

Singleton Alternative

When we work in Unity, a very common case is usually the use of the Singleton pattern like this:

using UnityEngine;
public class UIManager : MonoBehaviour
{
    private static UIManager _instance;   
    private void Awake()
    {
        if (_instance == null)
        {
            _instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else if (_instance != this)
        {
            Destroy(gameObject);
        }
    }

    public void UpdateHealthDisplay()
    {
        // ...
    }
}

public class Player : MonoBehaviour
{
    private int health;
    public void TakeDamage(int damage)
    {
        health -= damage;
        UIManager.Instance.UpdateHealthDisplay(health);
    }
}

The singleton pattern usually comes with problems in how it is managed, it brings with it problems such as a lot of coupling, little flexibility and high long-term dependencies.

With UniFlux we will try to approach the problem in a different way, instead of programming in a declarative way, what we will do will be in a reactive way:

using UnityEngine;
public class UIManager : MonoFlux
{
    [StateFlux("Player.Life")] private void OnChangePlayerLifeState(int state)
    {
        UpdateHealthDisplay(state);
    }
    private void UpdateHealthDisplay(int life)
    {
        // ...
    }
}
public class GameManager : MonoFlux
{
    [StateFlux("Player.Life")] private void OnChangePlayerLifeState(int state)
    {
        CheckIfEndGame(state);
    }
    private void CheckIfEndGame(int life)
    {
        if(life > 0)
        {
            // nothing...
        }
        else
        {
            // Run End of game
        }
    }
}
public class CameraManager : MonoFlux
{
    int last_PlayerLife = default;
    [StateFlux("Player.Life")] private void OnChangePlayerLifeState(int state)
    {
        // if player lose life
        if(last_PlayerLife > state)
        {
            ApplyScrrenShake();
        }
        // update last state
        last_PlayerLife = state;
    }
    private void ApplyScrrenShake()
    {
        // ...
    }
}
public class Player : MonoBehaviour
{
    private int Health
    {
        get => "Player.Life".GetState<int>(out var state) ? state : default;
        set => "Player.Life".DispatchState(value);
    }
    public void TakeDamage(int damage)
    {
        Health -= damage;
    }
}

This way you can unify data in one place but even more elements can subscribe to the current state. This allows you to not depend on the use of Singletons and to maintain low coupling in your projects.

PreviousService Locator AlternativeNextMessaging Alternative

Last updated 1 year ago

⚠️