Skip to content

Instantly share code, notes, and snippets.

@shiena
Last active March 3, 2021 08:35
Show Gist options
  • Save shiena/d73554393fbdfe90c055905c3b91e2b0 to your computer and use it in GitHub Desktop.
Save shiena/d73554393fbdfe90c055905c3b91e2b0 to your computer and use it in GitHub Desktop.
public class Callback : AndroidJavaProxy, IDisposable
{
private Action action;
private Disposable disposable;
public Callback(Action a) : base("any.java.CallbackInterface")
{
action = a;
}
// implemented method
public void invoke()
{
disposable = Observable.ReturnUnit()
.ObserveOnMainThread()
.Subscribe(_ =>
{
action?.Invoke();
});
}
public void Dispose()
{
disposable?.Dispose();
}
}
public class NativePlugin : MonoBehaviour
{
private CompositeDisposable compositeDisposable = new CompositeDisposable();
public void Call()
{
var callback = new Callback(() => Debug.Log("call"));
javaObj.Call("call" callback);
compositeDisposable.Add(callback);
}
void OnDestroy()
{
compositeDisposable.Clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment