Reputation: 897
I want to use the light sensor on Android with Unity. Unity can't do this and there is no asset/plugin on the store who could do that. And google isn't so frendly with this question...
Upvotes: 3
Views: 4519
Reputation: 897
This part explain how to use my code.
Put the .jar
file into Assets/Plugins/Android
folder
Then simply add the script LightSensorPluginScript.cs
on the GameObject you wanted.
Then if you want to get the sensor value:
TextMesh tm;
LightSensorPluginScript test;
void Start() {
tm = transform.GetComponent<TextMesh>();
test = GetComponent<LightSensorPluginScript> ();
}
void Update() {
tm.text = test.getLux().ToString();
}
All the files can be found in this zip archive
This part explain how to create the plugin.
First of all you have to create an Android library. If you are using Android Studio, they are converted into .aar
files. Extract them like a zip file and you will find a classes.jar
file which is the correct .jar
you want. You can rename it as you want, Unity didn't care.
Android Java code
public class LightSensorLib{
private SensorManager mSensorManager;
private Sensor mSensorRot;
private float lux = -1000;
public void init(Context context) {
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
mSensorRot = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
SensorEventListener mySensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float sensorData[];
if(event.sensor.getType()== Sensor.TYPE_LIGHT) {
sensorData = event.values.clone();
lux = sensorData[0];
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
mSensorManager.registerListener(mySensorEventListener, mSensorRot, 500);
}
public float getLux () {
return lux;
}
}
Then I have a C# script who does the bridge between Android Java and Unity
C# Bridge code
public class LightSensorPluginScript : MonoBehaviour {
private AndroidJavaObject activityContext = null;
private AndroidJavaObject jo = null;
AndroidJavaClass activityClass = null;
void Start () {
#if UNITY_ANDROID
activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
jo = new AndroidJavaObject("com.etiennefrank.lightsensorlib.LightSensorLib");
jo.Call("init", activityContext);
#endif
}
public float getLux() {
#if UNITY_ANDROID
return jo.Call<float>("getLux");
#endif
}
}
Now you can do what is explained in the Simple version
part of this post to use the sensor.
If you want any precision I would be glad to answer and update the post.
For the IOs version I'm sorry but I have no mac to develop this... So if you want to lend me one I would be glad to do it.
I could add it as a plugin on the asset store but it feels a bit cumbersome. So I prefer to post it on StackOverflow where the post editor is really neat.
At last I hope it helps some developers.
Upvotes: 10
Reputation: 1
using UnityEngine;
using System;
using System.Collections;
public class LightSensorPluginScript : MonoBehaviour {
private AndroidJavaObject activityContext = null;
private AndroidJavaObject jo = null;
AndroidJavaClass activityClass = null;
public TextMesh tm;
LightSensorPluginScript test;
void Start () {
#if UNITY_ANDROID
activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
tm = transform.GetComponent<TextMesh>();
test = GetComponent<LightSensorPluginScript>();
jo = new AndroidJavaObject("com.etiennefrank.lightsensorlib.LightSensorLib");
jo.Call("init", activityContext);
#endif
}
public float getLux() {
#if UNITY_ANDROID
return jo.Call<float>("getLux");
#endif
}
void Update()
{
tm.text = test.getLux().ToString();
}
}
Upvotes: -2