Example #1
0
 // Use this for initialization
 void Start()
 {
     // GetOrientation internally needs access to MagneticField and Accelerometer
     // MagneticField alone is not sufficient - it returns a vector pointing towards the magnetic south pole
     // (right trough the crust of the earth - the magnetic south pole is near the geographic north pole), and you probably do not want this,
     // although you can calculate other things with it if you need it.
     // MagneticField really does what it is saying - return the magnetic field values, which do not correspond to angles.
     Sensor.Activate(Sensor.Type.MagneticField);
     Sensor.Activate(Sensor.Type.Accelerometer);
 }
    IEnumerator StartEverythingSlowly()
    {
        // Some devices seem to crash when activating a lot of sensors immediately.
        // To prevent this for this scene, we start one sensor per frame.

        Sensor.Activate(Sensor.Type.Accelerometer);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.Temperature);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.MagneticField);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.RotationVector);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.LinearAcceleration);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.Gravity);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.Gyroscope);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.Light);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.Orientation);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.Pressure);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.Temperature);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.AmbientTemperature);
        yield return(new WaitForSeconds(0.1f));

        Sensor.Activate(Sensor.Type.RelativeHumidity);
        yield return(new WaitForSeconds(0.1f));

        // wait a moment
        yield return(new WaitForSeconds(0.2f));

        // activate GUI
        _showGui = true;
    }
Example #3
0
    // Tries to activate a certain rotation fallback.
    // If ActivateRotation is called, the best (in most cases) fallback is used.
    // There might be cases/devices where other approaches yield better results.
    //
    // Returns true if fallback is available and activated.
    public static bool TryForceRotationFallback(RotationFallbackType fallbackType)
    {
#pragma warning disable 162
        switch (fallbackType)
        {
        case RotationFallbackType.RotationQuaternion:
            if (Sensor.Get(Sensor.Type.RotationVector).available)
            {
                Sensor.Activate(Sensor.Type.RotationVector);
                _rotationHelper = new GetRotationHelper(RotationQuaternion);
                current         = RotationFallbackType.RotationQuaternion;

                Debug.Log("First sensor choice is available.");
                return(true);
            }
            break;

        case RotationFallbackType.OrientationAndAcceleration:
            if (Sensor.Get(Sensor.Type.Orientation).available)
            {
                Sensor.Activate(Sensor.Type.Orientation);
                _rotationHelper = new GetRotationHelper(OrientationAndAcceleration);
                current         = RotationFallbackType.OrientationAndAcceleration;

                Debug.Log("Second sensor choice is available.");
                return(true);
            }
            break;

        case RotationFallbackType.MagneticField:
            if (Sensor.Get(Sensor.Type.MagneticField).available)
            {
                Sensor.Activate(Sensor.Type.Accelerometer);
                Sensor.Activate(Sensor.Type.MagneticField);
                _rotationHelper = new GetRotationHelper(MagneticField);
                current         = RotationFallbackType.MagneticField;

                Debug.Log("Third sensor choice is available.");
                return(true);
            }
            break;
        }
        return(false);

#pragma warning restore 162
    }
    IEnumerator StartEverythingSlowly()
    {
        // Some devices seem to crash when activating a lot of sensors immediately.
        // To prevent this for this scene, we start one sensor per frame.

        for (int i = 1; i <= Sensor.Count; i++)
        {
            Sensor.Activate((Sensor.Type)i);
            yield return(new WaitForSeconds(0.1f));
        }

        // wait a moment
        yield return(new WaitForSeconds(0.2f));

        Debug.Log("tried to activate all sensors");

        // activate GUI
        showGui = true;
    }
Example #5
0
    // Call this to activate the best available rotation sensor
    public static void ActivateRotation()
    {
        // looks which sensors are available
        // and uses the best one for rotation calculation

        gotFirstValue = false;

        if (Sensor.Get(Sensor.Type.RotationVector).available)
        {
            // on iOS, first choice will always be used (others are made for Android)
            Sensor.Activate(Sensor.Type.RotationVector);
            _rotationHelper = new GetRotationHelper(RotationQuaternion);
            current         = RotationFallbackType.RotationQuaternion;

            Debug.Log("RotationVector is available.");
        }
        else if (Sensor.Get(Sensor.Type.Orientation).available)
        {
            Sensor.Activate(Sensor.Type.Orientation);
            _rotationHelper = new GetRotationHelper(OrientationAndAcceleration);
            current         = RotationFallbackType.OrientationAndAcceleration;

            Debug.Log("Orientation/Acceleration is available.");
        }
        else if (Sensor.Get(Sensor.Type.MagneticField).available)
        {
            Sensor.Activate(Sensor.Type.Accelerometer);
            Sensor.Activate(Sensor.Type.MagneticField);
            _rotationHelper = new GetRotationHelper(MagneticField);
            current         = RotationFallbackType.MagneticField;

            Debug.Log("Accelerometer/MagneticField is available.");
        }
        else
        {
            _rotationHelper = new GetRotationHelper(InputAcceleration);
            Debug.Log("InputAcceleration is available - no compass support.");
        }
    }
Example #6
0
 // Use this for initialization
 void Start()
 {
     Sensor.Activate(Sensor.Type.LinearAcceleration);
 }
    // Tries to activate a certain rotation fallback.
    // If ActivateRotation is called, the best (in most cases) fallback is used.
    // There might be cases/devices where other approaches yield better results.
    //
    // Returns true if fallback is available and activated.
    public static bool TryForceRotationFallback(RotationFallbackType fallbackType)
    {
#pragma warning disable 162
        // for platforms other than Android, always use RotationQuaternion
//		if(Application.platform != RuntimePlatform.Android)
//			fallbackType = RotationFallbackType.RotationQuaternion;
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            fallbackType = RotationFallbackType.RotationQuaternion;
        }

        switch (fallbackType)
        {
        case RotationFallbackType.RotationQuaternion:
            if (Sensor.Get(Sensor.Type.GameRotationVector).available)
            {
                Sensor.Activate(Sensor.Type.GameRotationVector);
            }
            else if (Sensor.Get(Sensor.Type.RotationVector).available)
            {
                Sensor.Activate(Sensor.Type.RotationVector);
            }
            if (Sensor.Get(Sensor.Type.GameRotationVector).available || Sensor.Get(Sensor.Type.RotationVector).available)
            {
                rotationHelper = new GetRotationHelper(RotationQuaternion);
                current        = RotationFallbackType.RotationQuaternion;

                Debug.Log("RotationVector is available.");
                return(true);
            }
            break;

        case RotationFallbackType.OrientationAndAcceleration:
            if (Sensor.Get(Sensor.Type.Orientation).available)
            {
                Sensor.Activate(Sensor.Type.Orientation);
                rotationHelper = new GetRotationHelper(OrientationAndAcceleration);
                current        = RotationFallbackType.OrientationAndAcceleration;

                Debug.Log("Orientation/Acceleration is available.");
                return(true);
            }
            break;

        case RotationFallbackType.MagneticField:
            if (Sensor.Get(Sensor.Type.MagneticField).available)
            {
                Sensor.Activate(Sensor.Type.Accelerometer);
                Sensor.Activate(Sensor.Type.MagneticField);
                rotationHelper = new GetRotationHelper(MagneticField);
                current        = RotationFallbackType.MagneticField;

                Debug.Log("Accelerometer/MagneticField is available.");
                return(true);
            }
            break;

        case RotationFallbackType.AccelerationOnly:
        {
            rotationHelper = new GetRotationHelper(InputAcceleration);
            Debug.Log("InputAcceleration is available - no compass support.");
            return(true);
        }
        break;
        }

        return(false);

#pragma warning restore 162
    }
    void OnGUI()
    {
        if (!_showGui)
        {
            return;
        }

        GUI.skin = guiSkin;
        // show all sensors and values in a big, fat table
        // Remember : You can only see on your device what sensors are supported, not in the editor.
        GUI.color = _guiColor;

        GUILayout.BeginArea(new Rect(5, 5, Screen.width - 10, Screen.height - 10));
        _scrollPosition = GUILayout.BeginScrollView(_scrollPosition);
        // GUILayout.BeginArea(new Rect(0, 150, 1000,800));


        GUILayout.BeginHorizontal();
        {
            C = 0;
            GUILayout.Label("Sensor", GUILayout.Width(W[C++]));
            // GUILayout.Label("#", GUILayout.Width(20));
            GUILayout.Label("Exists", GUILayout.Width(W[C++]));
            GUILayout.Label("Active", GUILayout.Width(W[C++]));
            GUILayout.Label("Name", GUILayout.Width(W[C++]));
            GUILayout.Label("Power", GUILayout.Width(W[C++]));
            GUILayout.Label("Resolution", GUILayout.Width(W[C++]));
            GUILayout.Label("MaxRange", GUILayout.Width(W[C++]));
            // GUILayout.Label("MinDelay", GUILayout.Width(60));
            GUILayout.Label("Values", GUILayout.Width(W[C++]));
            // if (GUILayout.Button(s.active?"Deactivate":"Activate", GUILayout.Width(90)))
            // {
            //		if (s.active)
            //		{
            //			Sensor.Deactivate(i);
            //		}
            //		else
            //		{
            //			Sensor.Activate(i);
            //		}
            //	}
        }
        GUILayout.EndHorizontal();

        GUILayout.Label("");

        for (var i = 1; i <= Sensor.Count + 1; i++)
        {
            C = 0;
            var s = Sensor.Get((Sensor.Type)i);
            if (s == null)
            {
                continue;
            }
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label("" + s.description, GUILayout.Width(W[C++]));
                GUILayout.Label("" + (s.available?"Yes":"No"), GUILayout.Width(W[C++]));
                GUILayout.Label(s.active ? "X" : "O", GUILayout.Width(W[C++]));
                GUILayout.Label("" + s.name, GUILayout.Width(W[C++]));
                GUILayout.Label("" + s.power, GUILayout.Width(W[C++]));
                GUILayout.Label("" + s.resolution.ToString("F2"), GUILayout.Width(W[C++]));
                GUILayout.Label("" + s.maximumRange, GUILayout.Width(W[C++]));
                // GUILayout.Label("" + s.minDelay, GUILayout.Width(60));
                GUILayout.Label("" + s.values, GUILayout.Width(W[C++]));

                if (s.available)
                {
                    if (GUILayout.Button(s.active?"Deactivate":"Activate", GUILayout.Width(110)))
                    {
                        if (s.active)
                        {
                            Sensor.Deactivate((Sensor.Type)i);
                        }
                        else
                        {
                            Sensor.Activate((Sensor.Type)i);
                        }
                    }
                }
                else
                {
                    GUILayout.Label("Not available", GUILayout.Width(110));
                }
            }
            GUILayout.EndHorizontal();
        }
        GUILayout.Label("");

        GUILayout.BeginHorizontal();
        {
            GUILayout.Label("Best rotation value", GUILayout.Width(W[0]));
            GUILayout.Label("(provided by SensorHelper.rotation)", GUILayout.Width(510));
            GUILayout.Label("" + SensorHelper.rotation, GUILayout.Width(120));
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        {
            GUILayout.Label("getOrientation", GUILayout.Width(W[0]));
            GUILayout.Label("Needs MagneticField and Accelerometer to be enabled. Gets fused from the two.", GUILayout.Width(510));
            GUILayout.Label("" + Sensor.GetOrientation(), GUILayout.Width(120));
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        {
            GUILayout.Label("Rotation Quaternion", GUILayout.Width(W[0]));
            GUILayout.Label("Calculated from rotation vector. Best accuracy.", GUILayout.Width(510));
            GUILayout.Label("" + Sensor.rotationQuaternion, GUILayout.Width(120));
            try
            {
                GUILayout.Label("" + Sensor.rotationQuaternion.eulerAngles, GUILayout.Width(120));
            }
            catch
            {
            }
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        {
            GUILayout.Label("getAltitude", GUILayout.Width(W[0]));
            GUILayout.Label("Calculated from pressure.", GUILayout.Width(510));
            GUILayout.Label("" + Sensor.GetAltitude(), GUILayout.Width(120));
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        {
            GUILayout.Label("SurfaceRotation", GUILayout.Width(120));
            GUILayout.Label("Device surface rotation.", GUILayout.Width(510));
            GUILayout.Label("" + Sensor.surfaceRotation, GUILayout.Width(120));
        }
        GUILayout.EndHorizontal();

        if (Input.gyro.enabled)
        {
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label("Gyro", GUILayout.Width(W[0]));
                GUILayout.Label("Attitude", GUILayout.Width(120));
                GUILayout.Label("" + Input.gyro.attitude, GUILayout.Width(120));
                GUILayout.Label("" + Input.gyro.attitude.eulerAngles, GUILayout.Width(120));
            }
            GUILayout.EndHorizontal();
        }

        if (Input.compass.enabled)
        {
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label("Compass", GUILayout.Width(W[0]));
                GUILayout.Label("Raw Vector", GUILayout.Width(120));
                GUILayout.Label("" + Input.compass.rawVector, GUILayout.Width(120));
            }
            GUILayout.EndHorizontal();
        }
        if (Application.isEditor)
        {
            GUILayout.Label("WARNING: Sensors can only be accessed on an Android device, not in the editor.");
        }

        GUILayout.EndScrollView();
        GUILayout.EndArea();
    }
Example #9
0
 // Use this for initialization
 void Start()
 {
     // activate Light sensor
     Sensor.Activate(Sensor.Type.Light);
 }
Example #10
0
 // Use this for initialization
 void Start()
 {
     Sensor.Activate(Sensor.Type.LinearAcceleration);
     r = GetComponent <Rigidbody>();
 }