How to pass an interface to Java from Unity code?
Posted
by
nickbadal
on Game Development
See other posts from Game Development
or by nickbadal
Published on 2012-10-03T00:17:52Z
Indexed on
2012/10/03
3:51 UTC
Read the original article
Hit count: 210
First, let me say that this is my first experience with Unity, so the answer may be right under my nose. I've also posted this question on Unity's answers site, but plugin questions don't seem to be as frequently answered there.
I'm trying to create a plugin that allows me to access an SDK from my game. I can call SDK methods just fine using AndroidJavaObject
and I can pass data to them with no issue. But there are some SDK methods that require an interface to be passed.
For example, my Java function:
public void attemptLogin(String username, String password, LoginListener listener);
Where listener
; is a callback interface. I would normally run this code from Java as such:
attemptLogin("username", "password", new LoginListener() {
@Override
public void onSuccess() {
//Yay! do some stuff in the game
}
@Override
public void onFailure(int error) {
//Uh oh, find out what happened based on error
}
});
Is there a way to pass a C# interface through JNI/Unity to my attemptLogin
function? Or is there a way to create a mimic-ing interface in C# that I can call from inside the Java code (and pass in any kind of parameter)?
Thanks in advance! :)
© Game Development or respective owner