ReactUnity / core

React and HTML framework for Unity UI & UIToolkit
https://reactunity.github.io/
MIT License
733 stars 42 forks source link

Added T4 simple generator as a proof of concept with ReactAction as PR implementation #80

Closed Muchaszewski closed 1 year ago

Muchaszewski commented 1 year ago

The idea is to have a stable generator for all kinds of types to improve the readability of the code. I have used T4 in the past with great success! and the code is usually more readable then string builders.

As a proof of concept and as a content of this PR I have added ReactAction as an Action that can be used as Action in normal C# code, but have an AddListener method that can be used in react,

C# example as a normal delegate

public ReactAction<string> del;
public void Foo()
{
    del += (val) => Debug.Log(val):
    del.Invoke("It worked");
}

but also can be used as a wrapper around Callback class

React

useEffect(() => {
    const unsubscribe = Global.Instance.del.AddListener((val) => {
        console.log("It worked from react");
    };
    return () => unsubscribe();
},[]);
KurtGokhan commented 1 year ago

Hey @Muchaszewski , I started fixing some issues now. If you have pending changes for this PR, please push them. I will soon merge this to main branch and do the necessary changes on it.

Also I added Destroyed property to callbacks. This may be the correct way to detect disposed callbacks.