Unity plugin for using Android sensors.
See also: ExampleBehaviour/SensorBehaviour.cs.
Download AndroidSensorPlugin.jar and put it into Unity Assets folder ( Assets/Plugins/Android/AndroidSensorPlugin.jar
).
Add these codes to your Behaviour script, and they initialize the plugin.
using UnityEngine;
...
private AndroidJavaObject plugin;
void Start ()
{
#if UNITY_ANDROID
plugin = new AndroidJavaClass("jp.kshoji.unity.sensor.UnitySensorPlugin").CallStatic<AndroidJavaObject>("getInstance");
#endif
}
void OnApplicationQuit ()
{
#if UNITY_ANDROID
if (plugin != null) {
plugin.Call("terminate");
plugin = null;
}
#endif
}
To use accelerometer
sensor, add these lines on the Start
method.
#if UNITY_ANDROID
if (plugin != null) {
plugin.Call("startSensorListening", "accelerometer");
}
#endif
To get sensor values, add these lines on Update
method.
#if UNITY_ANDROID
if (plugin != null) {
float[] sensorValue = plugin.Call<float[]>("getSensorValues", "accelerometer");
}
#endif
If the app want to read sensor values more frequently, call setSamplingPeriod
plugin method.
The argument is in micro seconds
.
plugin.Call("setSamplingPeriod", 1000); // refreshes sensor 1 milli second each
And then, call InvokeRepeating(string methodName, float time, float repeatRate)
method on Unity Behaviour's Start
method.
This makes enable more frequently method calling than game FPS.
void Start ()
{
... initializations here ...
// wait 1.0 second, and call `CheckSensor` method at 1000Hz (1/0.001 Hz)
InvokeRepeating("CheckSensor", 1.0f, 0.001f);
}
void CheckSensor ()
{
#if UNITY_ANDROID
if (plugin != null) {
float[] sensorValue = plugin.Call<float[]>("getSensorValues", "accelerometer");
}
#endif
}
- accelerometer
- ambienttemperature
- gamerotationvector
- geomagneticrotationvector
- gravity
- gyroscope
- gyroscopeuncalibrated
- heartrate
- light
- linearacceleration
- magneticfield
- magneticfielduncalibrated
- pressure
- proximity
- relativehumidity
- rotationvector
- significantmotion
- stepcounter
- stepdetector