Search Unity

Using .NET [SOLVED]

Discussion in 'Scripting' started by Slaventsiy, Mar 2, 2015.

  1. Slaventsiy

    Slaventsiy

    Joined:
    Feb 27, 2015
    Posts:
    4
    I am trying to use System.Windows.Threading.DispatcherTimer in the project. I put Windowsbase.dll inside Plugins folder, since it's not originally supported by Unity, but now it gives me "NotImplementedException: The requested feature is not implemented".

    The line that causes the error:

    Code (CSharp):
    1. DispatcherTimer dispatcherTimer = new DispatcherTimer();
    Here is the stack:

     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    I would avoid using anything in the System.Windows namespace, it's windows specific and not really supported by Mono. Especially since there's probably other dependencies that the dll might have, and also the dll might be of a newer version of .net that the older version of mono that unity uses doesn't support.

    All for what, to get use of 'DispatcherTimer'?

    This is going to open a whole other can of issues as its:

    1) multi-threading - you have to be very careful about using threading in unity as unity doesn't let you access any of the unity specific stuff from any thread but the main thread.

    2) it is threading based on the Windows Dispatcher queue... why do you need this?

    Use a coroutine or InvokeRepeating if you need something to occur every interval.
     
  3. Slaventsiy

    Slaventsiy

    Joined:
    Feb 27, 2015
    Posts:
    4
    Problem is that Coroutine and InvokeRepeating are not fast enough, I need to get data from accelerometer at the rate of 125000 times per second.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    If you're accessing the accelerometer through the unity api, it just isn't going to work, unity only lets you access any of its api on the main thread. Which means you're tied to its speed.

    If you use some 3rd party libraries to access the accelerometer. Use the System.Threading namespace.

    System.Windows should be avoided. And DispatcherTimer isn't the only threading timer out there. DispatcherTimer is supposed to be used with WPF, which unity does NOT use at all, and should not use. You're not writing a WPF application.

    Just remember, anything that needs to be done to any GameObject or component or what not in the scene NEEDS to be done on the main thread. So you'll need a way to tie back to the main thread to update anything.
     
  5. Slaventsiy

    Slaventsiy

    Joined:
    Feb 27, 2015
    Posts:
    4
    Thank you, making new Thread seems to do the trick.