So far I am able to retrieve simple values from static functions in Unity from my Android Plugin, my goal is to have this Plugin act as a phone sensor listener, and query the sensor values from inside Unity scripts. Would this be achievable? If so, are there are online resources I could follow as a tutorial? Many thanks in advance!
Asked
Active
Viewed 398 times
1 Answers
3
Yes. This is possible. Write the onSensorChanged
code in java, build it as a plugin then when onSensorChanged
is called in Java, use UnityPlayer.UnitySendMessage
to call your C# function on the Unity side.
Something like this:
public final void onSensorChanged(SensorEvent event)
{
UnityPlayer.UnitySendMessage("TheNameOfYourGameObject", "NameOfCallbackFunction", Arrays.toString(event.values));
}
The post here describes how to setup UnityPlayer.UnitySendMessage
in your Java plugin.
Note that you don't have to do this. There is a free Unity plugin you can use which already implemented all the sensor API with onSensorChanged
. You can check it out here and it is very easy to use.

Programmer
- 121,791
- 22
- 236
- 328
-
This is more than what I could have asked for, thank you kind sir! – Shivalkyr Mar 14 '18 at 00:50