⚛️
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

Avoiding Prop Drilling

PreviousMessaging AlternativeNextHow to use a custom Key?

Last updated 1 year ago

Prop Drilling is a problem that occurs when within a hierarchy a specific data must be passed from one point to another

using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
    [SerializeField] private int playerScore = 100;
    [SerializeField] private UIManager uiManager;
    private void Start()
    {     
        uiManager.SetPlayerScore(playerScore);
    }
}
public class UIManager : MonoBehaviour
{
    [SerializeField] private UIPlayerScoreController uiPlayerScore;
    private void SetPlayerScore(int score)
    {     
        uiPlayerScore.SetScore = score;
    }
}
public class UIPlayerScoreController : MonoBehaviour
{
    [SerializeField] private Text text_score;
    private void SetScore(int score)
    {
        text_score.text = score.ToString();
    }
}

The problem with this approach is the implication of having to pass a piece of data through many methods and classes to complete its function.

Now let's do a test handling it with States:

using UnityEngine;
using UnityEngine.UI;
using UniFlux;
public class GameManager : MonoBehaviour
{
    [SerializeField] private int playerScore = 100;
    private void Start()
    {     
        "PlayerScore".Dispatch(PlayerScore);
    }
}
public class UIManager : MonoBehaviour
{
    // Nothing here required.
}
public class UIPlayerScoreController : MonoFlux
{
    [SerializeField] private Text text_score;
    [StateFlux("PlayerScore")] private void SetScore(int state) 
    {
        text_score.text = score.ToString();
    }
}

Note that in this example UIManager is no longer required, and you can have fewer variables and dependencies. In this example we have used StateFlux instead of MethodFlux, this means that if another subscribed to the "PlayerScore" it would automatically take the last state without depending on all the information being sent to it again.

⚠️
(1) Need to send a message to (6)