Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  3. Unity 2017.2 is now released.
    Dismiss Notice
  4. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  5. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  7. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Android life cycle accelerometer and gyroscope

Discussion in 'Android' started by jdwieber, Apr 29, 2012.

  1. jdwieber


    Mar 20, 2012

    I'm working on a mobile game that uses the accelerometer and gyroscope. When deployed to an android device, the game works well in the beginning. However, if the device sleeps, when I wake it up and try to continue playing the gyroscope, and accelerometer both stop working. Is there a way to hook into the android life cycle functions without writing a custom activity? The script that I'm using to update the gravity direction is below. It's very simple and does the job, until the phone sleeps. Also, can I provide a custom manifest without overriding the default unity activity (for using-permissions)?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    4. public class GravityDirectionUpdater : MonoBehaviour
    5. {
    6.     private static Quaternion m_quat;
    7.     public float m_gravity = -29.8f;
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.         //Doesn't always work unless I put this here.
    13.         Input.gyro.enabled = true;
    15.         //TODO: reorient camera in scene to avoid transforming frame in scripts
    16.         m_quat = Quaternion.AngleAxis ( -90, Vector3.up ) *
    17.                  Quaternion.AngleAxis ( -90, Vector3.forward ) *
    18.                  Quaternion.AngleAxis ( -90, Vector3.left );
    19.     }
    21.     // Update is called once per frame
    22.     void Update ()
    23.     {
    24.         //I always see this, even after waking up
    25.         Debug.Log ( "Updat gyro.enabled: " + Input.gyro.enabled.ToString () );
    27.         //This makes no difference here
    28.         Input.gyro.enabled = true;
    30.         Physics.gravity = m_quat * ( Input.gyro.gravity.normalized * m_gravity );
    32.     }
    33. }
  2. szi_John


    Feb 10, 2012
    Have you tried turning off sleep?

    Code (csharp):
    May not suit your exact app but...
  3. jdwieber


    Mar 20, 2012
    Hi John,
    Thanks for the suggestion. It does prevent the problem and the annoyance of the screen dimming during play. The only thing I did different is that I used the unity constant.

    Code (csharp):
    1.  Screen.sleepTimeout = SleepTimeout.NeverSleep;