Android Sensors for Unity
Want to get the light sensor output or other cool stuff of your Android smartphone? This package provide you an easy access to the Android Sensor Manager. You can get a full list of possible sensors on this page. Each output get stored in a smart variable.
Inspired by the talk of Ryan Hipple’s (twitter | github) amazing talk from Unite Austin 2017, you can find part of that in the unity blog post, I wrote a simple event system based on Scriptable Objects. These system allows you to drag & drop variables and events around.
- Grab unityAndroidSensors.unitypackage from the Releases page for everything you need!
- OR, use the git repository.
- Tested with Unity 2018.3 (New Prefab Workflow) or above.
What you get
Firstly, You have access to every sensor value of the Android Sensor Manager. Secondly, You get a simple extentable event system which allows you to create SmartEvents and SmartVars in the project view.
Events and Variables
To create a new SmartEvent or a SmartVar you can use the Create->SensorPlugin->SmartData context menu in the project view.
Events are accessible via the ListenSmartEvent Component
Here you can listen to SmartEvent’s. If a event gets fired all callbacks get invoked.
These are particularly useful for game-level events, such as starting, pausing the game, a player dying etc.
Variables are just container of data, with the advantage that you can drag & drop them around like the events.
Float- and IntVars can be compared by the Float- or IntVarComparator. You can choose to compare with a constant or another smart variable.
Unity Sensor Plugin
The heart of the plugin is the UnitySensorPlugin class. You need the class just once in your scene to get the connection to your Android phone. All sensor readers communicate with the UnitySensorPlugin to get the sensor data as a float array.
The Sensor Reader provides you with some option to choose the right sensor, the output type and the update interval (per frame). The output is stored in a SmartVar. This can be of type float, int or Vector3.
If you choose int or float you have the option to select the specific axis of the output value, otherwise you get all axis as a Vector3Var.
If you want to modifiy the output of a sensor before it is written into a SmartVar you can create a modifier which can manipulate the raw float array from the sensor.
An example of a modifier is in the Modifier folder. It is applied to the pre-configured acceleration sensor.