简体   繁体   English

如何在Unity中使用Android光传感器

[英]How to use the Android light sensor in Unity

I want to use the light sensor on Android with Unity. 我想在带有Unity的Android上使用光传感器。 Unity can't do this and there is no asset/plugin on the store who could do that. Unity无法做到这一点,并且商店中没有资产/插件可以做到这一点。 And google isn't so frendly with this question... 谷歌对这个问题不是那么友好...

Simple version 简单版

This part explain how to use my code. 本部分说明如何使用我的代码。

  1. Put the .jar file into Assets/Plugins/Android folder .jar文件放入Assets/Plugins/Android文件夹
  2. Then simply add the script LightSensorPluginScript.cs on the GameObject you wanted. 然后只需在所需的LightSensorPluginScript.cs上添加脚本LightSensorPluginScript.cs
  3. 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(); } 
  4. All the files can be found in this zip archive 所有文件都可以在此zip存档中找到

Detailed version 详细版本

This part explain how to create the plugin. 本部分说明如何创建插件。

First of all you have to create an Android library. 首先,您必须创建一个Android库。 If you are using Android Studio, they are converted into .aar files. 如果您使用的是Android Studio,它们将转换为.aar文件。 Extract them like a zip file and you will find a classes.jar file which is the correct .jar you want. 像提取一个zip文件一样提取它们,您将找到一个classes.jar文件,它是您想要的正确的.jar文件。 You can rename it as you want, Unity didn't care. 您可以根据需要重命名它,Unity不在乎。

Android Java code Android Java代码

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#脚本,他负责在Android Java和Unity之间架起桥梁

C# Bridge code C#桥代码

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. 现在,您可以按照本文的“ Simple version部分中的说明进行操作,以使用传感器。

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. 对于IOs版本,我很抱歉,但是我没有mac来开发它。因此,如果您想借给我一个,我会很乐意这样做。

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. 因此,我更喜欢将其发布在StackOverflow上,其中的帖子编辑器非常整洁。

At last I hope it helps some developers. 最后,我希望它对一些开发人员有所帮助。

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();
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM