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

How to get Unity to recognize "winmd" files?

Discussion in 'Windows' started by Maisey, Apr 23, 2015.

  1. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    When using a normal DLL file, Unity will recognize this and the methods will be available before compilation. But when placing a winmd file inside the appropriate folder Asset > Plugin > Metro(/WSA) it won't work as with a DLL.

    1. Unity won't recognize that it has been "imported" and thus you can't write "using MyDerpLibrary".
    2. When trying to create a build it still won't understand that there's a winmd file that contains the necessary definitions.

    How would I proceed with this?

    I'm using Unity 4.5.5.
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    Hi,

    there is a simple workaround to this problem: create an empty DLL targeting .NET 3.5 with the same public classes/methods but without any implementations, and put that to Assets/Plugins. When compiling scripts, Unity will use DLL as reference target, but when you build your project, Unity will copy the winmd file to final project location and your scripts will actually land in winmd when they call its methods.

    It works a little bit different in Unity 5.
     
  3. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    Okay thank you! This should really be mentioned somewhere, found one other post vaguely mentioning this, so wasn't sure it actually was still the correct way of doing things.
     
  4. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    I kinda got it to work. But once the project is it Visual Studio and I try to build it I get build error saying that my methods exist both in ../Unprocessed/MyLib.dll and in ../Unprocessed/MyLib.winmd. I solved it by manually removing the MyLib.dll in the Unprocessed folder, but is there a "cleaner" way of doing this?

    @Tautvydas Zilys

    Thank you!
     
  5. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,672
    In Unity 5, you can explicitly check which plugin is Editor only and which one is for WSA. In 4.5.5, because the filenames of these plugins don't match, you'll have to probably move/delete file before building to WSA and then restore it, you could do that from the script though...
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    Did you compile the "WinMD" file yourself? Is it C# or C++? I assumed it was part of a C++ plugin.
     
  7. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    I didn't compile it myself, but it's a "custom" lib. It's C#.
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    Hmm, that's unexpected. I don't think Unity 4.6 is able to automatically handle it - it assumes there's another .dll with the same name next to the .winmd. Since your .winmd is made from C# code, it will not be there. What happens instead is that it picks up the fake DLL that was meant for Unity editor, and copies it next to the .winmd file.

    I suppose you don't have source code for it? Of course, the easiest solution would be to rebuild it as .dll rather than a .winmd.

    Another solution you could do, is create a new empty Visual Studio C++ DLL project, build it, and copy the resulting DLL next to winmd in Assets/Plugins/Metro. Then, Unity will copy the native plugin to the final build directory, and there would be no conflicts as native DLLs don't have classes. The dll you compiled wouldn't be used in any way - it would be there to just trick Unity.

    Let me know how it goes if you try making it work.
     
  9. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    Thank you for this @Tautvydas Zilys !

    I will try one of these solutions. I actually do have the source code, so I will try to build is as an DLL instead. If possible to make a DLL why would one make an winmd file to begin with?

    EDIT:

    I've now tried your solutions without success.
    1. Tried building the source into a (Portable) DLL (a normal DLL won't work because of Universal App nature). Failed with some error (when trying to build in Visual Studio) which didn't really indicate anything to what might be wrong.
    2. Tried to build an empty C++ DLL. Not sure which kind you were referring to here since there's a bunch of different DLLs, but I kinda tried them all. I'm also not sure what you meant by this solution. Was I supposed to keep the C# DLL with the method declarations so that Unity still understands that the methods still exists? The closest I came to a build with this solution was that VS complained about "x86" and "MTIL", but it seemed way off.
     
    Last edited: Apr 24, 2015
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    What's the error? I'd try building it as "Windows Store" C# Class Library, instead of portable.

    Any of them should work. My idea was this:

    Assets/Plugins/MyPlugin.dll // The fake C# DLL that you made for editor
    Assets/Plugins/Metro/MyPlugin.dll // Empty C++ DLL
    Assets/Plugins/Metro/MyPlugin.winmd // Real winmd file
     
  11. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    Sorry for not getting back to you @Tautvydas Zilys ! I don't seem to get notifications from this thread any more? :/

    There is no "Windows Store" C# Class Library. The only options are: Class Library (Portable for Universal Apps) and Windows Runtime Component (Portable for Universal Apps). I took the former, since the other one is winmd-files.

    I will try this.
     
  12. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,642
    At least in VS2013 there is. New Project -> Visual C# -> Store Apps -> Windows Apps

    there is "Class Library (Windows)" in the list.
     
  13. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    @Aurimas Cernius

    You are correct, there actually was an "Class Library (Windows)". I clicked the "Store Apps" menu thinking it would list them all, but it didn't. Thank you and sorry for my blindness.

    (Haven't had time to try any of these solutions correctly yet, will get back when I've managed to do it)
     
  14. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    @Aurimas Cernius

    I've now tried this:
    It will give the following error when trying to build from Unity:

    Error building Player: Exception: Assembly compatibility check error:
    Failed to get assembly information for one of two plugins: D:\GitHub\my-game\Unity\Temp\StagingArea\Data\Managed\Plugins\Metro\PushSDK.dll

    Is there anything I'm missing? I tried building:
    1. C++ > Store Apps > Windows Apps > DLL (Windows)
    2. C++ > Store Apps > Universal Apps > DLL (Universal Apps)

    Thank you!
     
  15. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,642
    That's not the right setup, you can not mix native/managed plugins.
    I'd recomment you do the following setup:

    Assets/Plugins/MyManagedPlugin.dll // The fake C# dll for editor
    Assets/Plugins/Metro/MyManagedPlugin.dll // Real C# plugin for WSA, that calls MyPlugin.winmd internally
    Assets/Plugins/Metro/MyPlugin.winmd // The real winmd, completely hidden from scripts by MyManagedPlugin
     
  16. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    I'm confused now.

    @Tautvydas Zilys told be to do the setup I mentioned. That I should place an empty C++ DLL in the folder so that it doesn't conflict once it's inside the Visual Studio Project. But now you are telling I should create a real C# DLL file which actually does stuff?

    This seems like a very big hassle. Sure that Tautvydas solution won't work some how, @Aurimas Cernius ?
     
  17. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,642
    You can try Tautvydas solution too, but having managed dll for editor is wrong, when platform dll is native.
    You can try to remove editor plugin completely and instead wrap calls to plugin in scripts with define. It can be !UNITY_EDITOR or NETFX_CORE.
     
  18. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    That should work just fine. Just tried locally on my machine. I've uploaded my experiment here:

    http://1drv.ms/1zo2Ofq

    I used Unity 4.6.4f1.

    I think you misread it - both of the plugins are C# :).
     
  19. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    Thank you for confirming @Tautvydas Zilys . What kind of C++ DLL did you build? Because as I said in previous post I got an error when trying it.

    (I will try your test-project in my Unity and see if it succeeds)
     
  20. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    I created an empty windows store 8.1 DLL. I included source code for the plugins.
     
  21. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    It seems your solution doesn't work in Unity 4.5.5 @Tautvydas Zilys . I get the error below, when trying to build from Unity.

    If I rename the "FakeDLLForEditor"-plugin it will compile, but then I'm left with the same problems as from the start - conflicting definitions for methods.

    @Tomas1856 mentioned this:
    How would I do this via a script? (Have no other option now)
     
    Last edited: May 4, 2015
  22. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    Indeed, just tried it locally and it doesn't work on Unity 4.5.5 due to some strange bug.

    So, your options at the moment are:

    1. Update to Unity 4.6.4 (I verified that my solution works there);
    2. Rebuild WINMD file as DLL instead.
     
  23. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    I can't update to Unity 4.6 because we are relying on a customized NGUI-plugin (which made big changes in 4.6). How would I make Tomas1856 solution (see below)?
    Any ideas @Tautvydas Zilys ?
     
    Last edited: May 4, 2015
  24. vaishalichaudhary0209

    vaishalichaudhary0209

    Joined:
    Oct 16, 2019
    Posts:
    2
    Do we still have this issue. For Editor can we load winmd file?
    For WSA, I am able to load winmd but not for Editor
     
  25. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    Editor cannot load or run .winmd files. You can only use them for UWP builds.