Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Access Surface Pro accelerometer data via DLL

Discussion in 'Scripting' started by SimoGecko, May 15, 2017.

  1. SimoGecko

    SimoGecko

    Joined:
    Sep 28, 2014
    Posts:
    13
    Hello guys, I'm trying to find a solution to access the accelerometer input since the standard Input.acceleration doesn't seem to work in the unity editor when on a Surface tablet (and yes I switched my build platform).
    I've read that the easiest way is to create a DLL and read the data directly, but my problem is that in order for a DLL to work in unity, the .NET framework has to be set to 3.5, and all the code I've found (using Windows.Devices.Sensors) seems to be needing version 4 at least.
    I have made apps both in C# and C++ that read the data from the Surface but cannot find a way to import them in unity.

    Here is an example:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using System.Linq;
    5. using Windows.Foundation;
    6. using Windows.Foundation.Collections;
    7.  
    8. // Required to support the core dispatcher and the accelerometer
    9. using Windows.UI.Core;
    10. using Windows.Devices.Sensors;
    11.  
    12.  
    13.  
    14. using System.Runtime.InteropServices;
    15.  
    16.  
    17.  
    18.  
    19.  
    20. namespace ClassLibrary3
    21. {
    22.     public class ClassLibrary3
    23.     {
    24.  
    25.         public static Accelerometer _accelerometer;
    26.         public static AccelerometerReading reading;
    27.  
    28.  
    29.         public static void Init()
    30.         {
    31.             _accelerometer = Accelerometer.GetDefault();
    32.  
    33.             if (_accelerometer != null)
    34.             {
    35.                 // Establish the report interval
    36.                 uint minReportInterval = _accelerometer.MinimumReportInterval;
    37.                 uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
    38.                 _accelerometer.ReportInterval = reportInterval;
    39.  
    40.             }
    41.         }
    42.  
    43.         public static double getAccelerationX()
    44.         {
    45.             return _accelerometer.GetCurrentReading().AccelerationX;
    46.         }
    47.     }
    48.  
    49. }
    This works fine as a standalone, but when I switch Target framework under Project->properties to 3.5, it fail, saying

    1>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3258: The primary reference "Windows" could not be resolved because it has an indirect dependency on the .NET Framework assembly "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which has a higher version "4.0.0.0" than the version "2.0.0.0" in the current target framework.

    and a buch more errors.


    Does anybody know how such a DLL that reads accelerometer input from the Surface Pro and makes it available to unity somehow?
    Thank you guys for your support and help!
     
  2. liebltabeling

    liebltabeling

    Joined:
    Mar 17, 2016
    Posts:
    13
    Hi did you find any solution?
    I am sitting at the same problem know :)

    Thanks, Matthias
     
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    Have you tried closing unity and deleting Library folder under project directory and then opening editor again?