⚛️
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
On this page
Edit on GitHub
  1. Components

MonoFlux

MonoFlux is a class that inherits MonoBehaviour but internally manages the subscription through the attributes that we include in the methods

using UniFlux;
// Inherit your class with "MonoFlux" instead of MonoBehaviour.
public sealed class TestFlux : MonoFlux 
{
  // Set a UniFlux's Attribute like "MethodFlux" and also set the Key, "KEY" in this case.
  [MethodFlux("KEY")]
  private void OnExampleMethodIsCalled()  //This method will automatically subscribe!
  {
    Debug.Log("Hello World");
  }
}
// then next you use "KEY".Dispatch(); and OnExampleMethodIsCalled from TestFlux will be called.

MonoFlux Subscribe and unsubscribe methods with attributes in the "OnEnable" or "OnDisable" events.

In Case you want to make subscriptions without attributes you might override "OnFlux" method

using UniFlux;
public sealed class TestFlux : MonoFlux 
{
  protected override void OnFlux(in bool condition) 
  { 
      // "KEY" - OnExampleMethodIsCalled
      "KEY".Store(condition, OnExampleMethodIsCalled)
  }
  private void OnExampleMethodIsCalled()
  {
    Debug.Log("Hello World");
  }
}

In Case you want to keep using MonoBehaviour as inherit class you can handle it using onEnable and OnDisable

using UniFlux;
public sealed class TestFlux : MonoBehaviour
{
  private void OnEnable() 
  { 
      "KEY".Store(true, OnExampleMethodIsCalled)
  }
  private void OnDisable() 
  { 
      "KEY".Store(false, OnExampleMethodIsCalled)
  }
  private void OnExampleMethodIsCalled()
  {
    Debug.Log("Hello World");
  }
}

Last updated 8 months ago

🧩