How To: Create Unity3d WinRT Plugin.

Documentation from Unity 4.2 contains too small info about WinRT plugins. Let’s fix this by this tutorial.

1) Create empty unity project:

2)      Create folders in Assets – Plugins and Metro (under Plugins).

3)      We will try to call plugin function from empty GameObject. Create it:

4)      And create C# script:

5)      Then bind it to empty GameObject (by drag&dropping script to Inspector window).

6)      Now check, that everything is ok:

7)      Play scene and notice log message from console:

8)      Now it’s time to create Windows 8 Runtime plugin. Create new Windows Store apps library. I recommend to do it in unity project folder:

9)      Configure paths for output:

10)   And check that everything is ok – compile project and check Assets/Plugins/Metro folder from Unity:

11)   Now add additional project (windows class library) to this solution – Stub dll that runs under unity editor.

12)   Stub contains only design time functions, but can share most of our plugin code. To reference original plugin.cs, create link from "Add existing File Menu" in this project as link. (Tanks Adam Tuliper for tip).

13)   Next step is make some changes of project properties. Make assembly name and default namespace same as WinRTPlugin project and change target framework to version 3.5.

14)   Also point output dir to Assets/Plugins

15)   Delete reference to Microsoft.CSharp assembly:

16)   Close plugin.cs editor tab and doubleclick plugin.cs from Stub project, and surround references from Windows Runtime using NETFX_CORE condition.

17)   Now, build Stub project and check that output dll placed to Assets/Plugins.

18)   It’s time to call our plugin function. Edit your unity script and add plugin instance & function call:

19)   Open plugin.cs from WinRTPlugin project and add some windows 8 functions:

20)   Build you project from Unity:

21)   Open your unity solution from Visual Studio and check that it references WinRTPlugin.dll:

22)   Run your app and check that everything is ok:

 

You can download sample code from here https://sdrv.ms/18LcwIo