- Home /
How to implement listeners in unity3d android plugin?
I am trying to build a unity 3d android plugin. Although there are a few tutorials on accomplishing this task (including the unity documentation). I could not find/ didn't understand how to implement java event handlers into unity as I am quite new to unity/ android development.
For instance: Obvious things like an AndroidJavaClass Object in Unity can only be used to call static functions took me a while to figure out.
I haven't been able to solve the following issue.
Android Library
This is an indoor-positioning system (Indoor Atlas), determines the users' position without the use of GPS
import com.indooratlas.android.sdk.IALocation;
import com.indooratlas.android.sdk.IALocationListener;
import com.indooratlas.android.sdk.IALocationManager;
import com.indooratlas.android.sdk.IALocationRequest;
import com.indooratlas.android.sdk.IARegion;
public class UnityLocation
implements IALocationListener, IARegion.Listener{
// Location Manager requests location updates
private IALocationManager mIALocationManager;
private Context currentContext;
// These two function will be called from unity to start location services
public void setContext(Context context)
{
currentContext = context;
}
public void startLocationServices()
{
Log.d("TESTING", "Start Location services function called");
mIALocationManager = IALocationManager.create(currentContext);
final IARegion region = IARegion.floorPlan("1e7d3247-9aab-4de9-903e-aa0fcccd3b45");
mIALocationManager.setLocation(IALocation.from(region));
IALocationRequest request = IALocationRequest.create();
mIALocationManager.requestLocationUpdates(request,this);
}
// This is the function where the coordinates are retrieved listener
@Override
public void onLocationChanged(IALocation iaLocation) {
// Log the received coordinates to the console.
Log.d("LOCATION UPDATE", "LATITUDE: " + iaLocation.getLatitude());
Log.d("LOCATION UPDATE", "LONGITUDE: " + iaLocation.getLongitude());
}
}
If I call the two functions setContext() and startLocationServices() from any activity in android studio, the coordinates are printed to the console and are updated as I move around the room.
But if I try to call the same two function from a unity script, even though the object is initialised and I can call functions, nothing is printed to the console.
Unity Script
jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
if(jc == null)
Debug.Log("unable to get unity activity class");
jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
if (jo == null)
Debug.Log ("unable to get current activity");
else {
Debug.Log ("Current activity read");
}
//display.text.text = "UNITY ACTIVITY";
//location = new double[2] { 0, 0 };
plugin = new AndroidJavaObject ("edu.deakin.sp9.indooratlaslibrary.UnityLocation");
if (plugin == null)
Debug.Log ("unable to get plugin class");
else
Debug.Log ("Plugin class retrieved");
//display.text.text = "Code reached plugin class";
plugin.Call ("setContext", jo);
plugin.Call("startLocationServices");
My guess is that the listener in this case "onLocationChanged" needs to be manually implemented in unity, but I have no idea how this can be done, reading the unity documentation of AndroidJavaProxy was no help. Any help would be greatly appreciated.
Your answer

Follow this Question
Related Questions
Player Prefs Dosen't work on android 0 Answers
Unity Editor Freezes- Help! Urgent! 1 Answer
Working with 2 scripts 2 Answers
How to get jvm in untiy script or Android NDK Plugin 0 Answers
[Unity 5 / Android] How to set Non-fullscreen by Default? 1 Answer