1

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!

Programmer
  • 121,791
  • 22
  • 236
  • 328
Shivalkyr
  • 49
  • 5

1 Answers1

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