Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Implemante FFTW in Unity

Discussion in 'Scripting' started by Smiley94, Jan 29, 2016.

  1. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    Hello,

    I dont know if it is the right place to post it, sorry If I'm wrong.

    I need to use a C library, FFTW, with C# in Unity.
    I found a wrapper there: https://github.com/tszalay/FFTWSharp
    There is .dll, and some scripts, but I dont know at all how to use them.

    I just know I have to put the .dll in the root, and nothing else. I just need to be able to call a function like DoFFT(float[] data), that's all.

    Can someone explain me how to do this ? :D
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    You place library into project, configure what it supports in its options(systems, bitness, etc) in editor, then download that wrapper's .cs files and call methods from those .cs files.
     
  3. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    That's why I did, but when I build the project, the .dll cannot be loaded, on Pc and Android.

    I get the exception DLLNotFoundException
     
  4. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    I don't really understand which root you mean. It needs to be in assets folder somewhere.

    With only problem that System.Numerics are not in Unity's C# (created a Complex struct in managed.cs in that wrapper manually) it works for me.
    Library settings look like this for me:


    Both 32 bit and 64 bit libraries are in folders Assets/32/ and Assets/64/ in empty project with those two .cs from wrapper in root of assets. 64 bit have x86_x64 set instead, obviously. Calling wrapper methods - no errors.
     
    Last edited: Feb 6, 2016
  5. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23


    I dont really get how to get this ...



    :(
     
  6. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Somehow your Unity doesn't see it as library that can be imported. Does it have .dll (or other library-type) extension? Is dll corrupted or not? What version or Unity are you using and what OS are you using?
     
  7. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    I'm using Unity 4.6 on Windows 10. The library is a .dll, and it should work as it works on editor mode.
     
  8. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Android can't use .dlls as they are built for windows. You need to build library using android SDK.

    I'm not sure about windows case as I need to first look at Unity 4.6 before I can say something, and I don't have it installed currently. It's just possible interface I showed appeared later then 4.6, I don't remember that.
    From what I can say it's possible you build it for x64 bitness while having x32 dll or visa versa, you should check that.
     
    Last edited: Feb 6, 2016
  9. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    I tried with 32 and 64 versions for windows. About Android I'll try to build it, but I never did that.

    Thanks for you're help
     
  10. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    I am kinda lost about using a libraby in Android.

    Can I compile the source code in .so file, and use it in Unity ?
     
  11. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    One more problem I remembered just now: Unity 4 personal didn't support dll loading, do you have pro?

    Yes, that should be the way. It's just compilation must be done using Android SDK from source code (see its manuals on how to do that).
     
  12. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    Yes, I use the pro version.

    Im trying to see how to compile a .so file from a source code, even if I have no idea how to do this. :d
     
  13. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Yeah, that screenshot I posted was from 5+ unity, in 4 there's no such handy dll inspector.
    Can't currently test it in 4 as I don't have pro. There are also some strange things like when I can call method from wrapper it doesn't show license error, but when I copy same lines dllimport lines into MonoBehaviour and calling it from there it shows license errors.

    Also, all dlls must be in Assets/Plugins/<platform> folder as stated here.
    Also found topic about same issue(didn't find anything I didn't mention till now though).
     
  14. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    Well, Im working on this app for Android, my problem at the moment is about compiling fftw to .so file.

    Do you know if there is a tutorial or something ? I spent hours trying to find a solution to my problem ...
     
    Last edited: Feb 6, 2016
  15. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    Sorry for this double post, but I have an important question.

    The first time I compiled my Unity game to Android, I had 2 .dll in my project, (not in Asset, in the first folder).
    This time, my app worked fine. The FFTW .dll was loaded correctly, and my functions were called correctly.
    Since the second compilation, my phone couldnt load the .dll, I had the exception "Unable to find fftw3.dll, DLLNotFoundException". But If .dll doesnt work on Android, why is the app trying to find it ?

    How could it work if .dll cannot be loaded on Android?
     
  16. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Because in DllImport statements in wrapper it's written as DllImport("libfftw3-3.dll"...
     
  17. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    I found a libfftw3f.so and libfftw3.so on Archlinux.com, as I used libfftw3f.dll and libfftw3.dll.
    I added both in Assets/Plugins/Android, and when my script run
    [DllImport("libfftw3f.so", EntryPoint = "fftwf_malloc",ExactSpelling = true,CallingConvention = CallingConvention.Cdecl)]
    then it prints "Unable to find libfftw3f.so" ... and of course "DllNotFoundException" ... :(
     
  18. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    From what I see in manuals there should be no ".dll" or ".so"?
     
  19. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    Even without .so, [DllImport("libfftw3f")] doesnt work too on Android, it prints the same thing, "Unable to find ..." and "DllNotfound Exception"

    Also, I used Winrar to open an .apk from Unity ..; Why is there many .dll in an .apk file, if .dll cannot be used on Android?
     
    Last edited: Feb 7, 2016
  20. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    It's not like .dll can't be used; you can name library .dll instead of .so - linux doesn't prohibit that. It just has different format (OS specific: linux and windows for example have different and android is linux-based) and different dependencies(you don't have kernel32.dll on linux, right?).
     
  21. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    So, if the first time I compiled on Android, with .dll, it worked, it was a bug ?
     
  22. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    I'm not sure as documentation is ambiguous about that. It might be that Unity has its own dll parsing implemented and runs it on android, so that might not be a bug or it might be. If you create a small project with reproduction and post it to bug tracker, you will get answer whether it's a bug or not. Answer from someone in developer team would be much more creditable than me.
    If it worked somehow, I'd suggest trying to restore that point and trying to reproduce it with any means possible.
     
  23. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    That's why I tried for some days, but I still dont know how it worked. Now, I cannot even load .dll from a game built on Pc
     
  24. Smiley94

    Smiley94

    Joined:
    Oct 23, 2014
    Posts:
    23
    I know I am asking many questions, but I really need to fix this problem quickly.

    I found the .so version of the fftw library there: https://www.archlinux.org/packages/extra/x86_64/fftw/

    I added both .so files in Assets/Plugins/Android, compiled, but I still have the exception "Unable to find ... , DllNotfoundException". When I open the .apk file, I see both .so files in /lib, with many others lib from unity, like libunity.so.
    I also tried with this one : https://www.archlinux.org/packages/extra/i686/fftw/, which is the 32 bits version ( I guess ).

    For the PC' problem, I fixed it by putting the .dll in the same folder as the .exe, and it works. But now I need to do it on Android ...

    :(