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

MethodFluxAttribute

MethodFlux allows you to subscribe to methods as events (such as System.Action or System.Func). Depending on how the method in question is written, it will direct the subscription to the place where it corresponds. In turn, we will need to call it with specific instructions.

Here is a simple example

using UniFlux;
public sealed class TestFlux : MonoFlux 
{
  // "1".Dispatch()
  [MethodFlux("1")] private void OnExample_1()
  {
    Debug.Log("Hello World");
  }

  // "2".Dispatch(42)
  [MethodFlux("2")] private void OnExample_2(int value)
  {
    Debug.Log(value);
  }

  //string response = "3".Dispatch<string>()
  [MethodFlux("3")] private string OnExample_3() 
  {
    return "Hello World";
  }

  //string response = "4".Dispatch<int, string>(42)  
  [MethodFlux("4")] private string OnExample_4(int value)
  {
    return value.ToString();
  }
}

It must be taken into account that only one of the parameters to be sent can be sent. For optimization reasons, it is prohibited to include 2 arguments as parameters. The alternatives to avoid this possible problem are to create Tuples, classes, structs or records.

using UniFlux;
public sealed class TestFlux : MonoFlux 
{
    void Start()
    {
        // string.Dispatch<(int, string, bool)>()
        "Test.Tuples".Dispatch( (69, "96f", true) );
        
        // string.Dispatch<Vector2>()
        "Test.Struct".Dispatch( Vector2.zero );
    }
    
    [MethodFlux("Test.Tuples")] private void OnExample((int, string, bool) value)
    {
        Debug.Log(value);
    }
    [MethodFlux("Test.Struct")] private void OnExample(Vector2 value)
    {
        Debug.Log(value);
    }
}

Last updated 1 year ago

🧩