Android Sensor Tutorial – Shake Sensor

Hi Guys, this is the second tutorial for Android Sensor Plugin for Unity3d and here again we will show you on how easy to use it, so let’s do it.

First always check if you have a Android Manifest File on “Assets/Plugins/Android” Directory without it your app won’t work or it will crashed.

If you have an Android Manifest  File  on the correct directory ( “Assets/Plugins/Android” ) , open it and check if you have this permission

<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />

After that setup we can now start coding

First we need to get the instance of  Shake Plugin

private ShakePlugin shakePlugin = ShakePlugin.GetInstance();

Then Set the Debug to 0 to hide the debug toast messages else pass 1 to show debug toast messages

shakePlugin.SetDebug(0);

And then we need to initialized it.

shakePlugin.Init();

and of course don’t forget to Setup a Listener for Shake Sensor Callback event

shakePlugin.SetCallbackListener(OnShake);

private void OnShake(int count, float speed){
   UpdateShakeCount(count);
   UpdateShakeSpeed(speed);
}

And Then  we start Registering Listener for Shake Sensor

shakePlugin.RegisterSensorListener();

To modify sensitivity just call SetSensitivity method and pass value that you want the range is 0 to 2000

int sensitivity = 500;
shakePlugin.SetSensitivity(sensitivity);

To Modify Update Interval just call SetDelayUpdate and pass value that you want the range is 0 to 1000

int delayUpdate = 100;
shakePlugin.SetDelayUpdate(delayUpdate);

If you are done using Shake sensor just call RemoveSensorListener method

shakePlugin.RemoveSensorListener();

And here’s the  exact code used in our  Shake demo scene

Note: this code needs Text and Slider , just create 4 Text UI and 2 slider inside Uniy3d and drag it on this code Inspector and don’t forget to assign the events for slider.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ShakeDemo : MonoBehaviour {
    
    private ShakePlugin shakePlugin;
    public Text shakeCountText;
    public Text shakeSpeedText;

    public Text sensitivityText;
    public Slider sensitivitySlider;

    public Text delayUpdateText;
    public Slider delayUpdateSlider;

    
    // Use this for initialization
    void Start (){
        shakePlugin = ShakePlugin.GetInstance();
        shakePlugin.SetDebug(0);
        shakePlugin.Init();
        SetSensitivitySlider();
        SetDelayUpdateSlider();

        shakePlugin.SetCallbackListener(OnShake);
        shakePlugin.RegisterSensorListener();
    }

    private void SetSensitivitySlider(){
        int sensitivity = (int)sensitivitySlider.value;
        UpdateSensitivity(sensitivity);
        Debug.Log("CheckSensitivitySlider value: " + sensitivity);
    }

    private void SetDelayUpdateSlider(){
        int delayUpdate = (int)delayUpdateSlider.value;
        UpdateDelayUpdate(delayUpdate);
        Debug.Log("CheckDelayUpdateSlider value: " + delayUpdate);
    }

    public void OnSensitivitySliderChange(){
        SetSensitivitySlider();
    }

    public void OnDelayUpdateSliderChange(){
        SetDelayUpdateSlider();
    }

    private void OnApplicationPause(bool val){
        if(val){
            if(shakePlugin!=null){
                shakePlugin.RemoveSensorListener();
            }
        }else{
            if(shakePlugin!=null){
                shakePlugin.RegisterSensorListener();
            }
        }
    }

    private void OnShake(int count, float speed){
        UpdateShakeCount(count);
        UpdateShakeSpeed(speed);
    }

    private void UpdateSensitivity(int sensitivity){
        if(shakePlugin!=null){
            shakePlugin.SetSensitivity(sensitivity);
            if(sensitivityText!=null){
                sensitivityText.text = string.Format("Sensitivity: {0}",sensitivity);
            }
        }
    }

    private void UpdateDelayUpdate(int delayUpdate){
        if(shakePlugin!=null){
            shakePlugin.SetDelayUpdate(delayUpdate);
            if(delayUpdateText!=null){
                delayUpdateText.text = string.Format("Delay Update: {0}",delayUpdate);
            }
        }
    }

    private void UpdateShakeCount(int count){
        if(shakeCountText!=null){
            shakeCountText.text = string.Format("Shake Count: {0}",count);
        }
    }

    private void UpdateShakeSpeed(float speed){
        if(shakeCountText!=null){
            shakeSpeedText.text = string.Format("Shake Speed: {0}",speed);
        }
    }
}

And That’s it we are done, thank you for reading and happy coding 🙂

For more questions or comments or suggestions just email us at gigadrillgames@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.