Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Build for UWP C# with IL2CPP

Discussion in 'Windows' started by madhur, Apr 17, 2019.

  1. madhur

    madhur

    Joined:
    May 16, 2012
    Posts:
    86
    Hi,

    I have a Unity project which we built for UWP using .Net as scripting backend. When I try to run it in Unity 2019, noticed that .Net is not there anymore and we have to build it using IL2CPP.
    When I try to build it with IL2CPP, it is giving an error '
    error CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?)'

    This is the code part related to that
    Code (CSharp):
    1.  
    2. using Windows.Storage;
    3. ....
    4. StorageFolder storageFolder = ApplicationData.Current.LocalFolder;

    So with the .net option being removed, is it possible to build a Unity project which has c# scripts as UWP to be used in a C# UWP project.
    If possible, can you please tell me how to solve reference issues like this?

    Thanks
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,906
    Where are you putting this code? Is it in your Unity Project? You need to wrap usages of Windows namespace in #if ENABLE_WINMD_SUPPORT/#endif.
     
  3. madhur

    madhur

    Joined:
    May 16, 2012
    Posts:
    86
    Thanks @Tautvydas-Zilys , it was in the Unity project and after adding the if condition got it working.

    Now I need to integrate that UWP project built with IL2CPP with my C# UWP project.
    I have been following this thread https://forum.unity.com/threads/solved-xamlunityconnection-in-il2cpp-xaml-c-il2cpp.495345/
    But still not clear what I have to do.
    I have also looked at this project https://github.com/TautvydasZilys/unity-uwp-il2cpp-with-csharp-project-example and there I am seeing a c# project file and a c++ project file. I am not sure how I can combine both these projects.
    Also when I tried to run the UWP app created from this example built using Unity 2019 I am getting these errors,

    Error Payload file 'E:\Samples\unity-uwp-il2cpp\bin\build\bin\x86\Debug\SymbolMap' does not exist.
    Error Payload file 'E:\Samples\unity-uwp-il2cpp\bin\build\bin\x86\Debug\GameAssembly.dll' does not

    Can you please explain how you created this example project. I need to know how to integrate an existing C# UWP project with a Unity IL2CPP built UWP project. Any help will be really appreciated.

    Will that work in Unity 2019 and Visual studio 2019?
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,906
    The C++ project file in that github repo isn't actually used. It's just produced by Unity every time you build so I didn't bother deleting it. It's all in the C# project.

    SymbolMap file is no longer used in Unity 2019.1. You should modify the C# project file to not include it. However, GameAssembly.dll should be built from Il2CppOutputProject - does the build even get triggered? Perhaps it fails for some reason?

    I created the project by just making a new UWP project from Visual Studio, and then I added references to Unity pieces manually.
     
  5. madhur

    madhur

    Joined:
    May 16, 2012
    Posts:
    86
    @Tautvydas-Zilys Thanks for your help. I managed to integrate a C# UWP app with a UWP project built with IL2CPP and then I added the Windows runtime component as you have explained to do the communication and got that also working. But one thing I noticed is when I run the app in Visual studio, it prints only the logs I have added in Visual studio project, but the ones I have added in Unity Script.cs file (IL2CPPBridge class) doesn't get printed. But the logic I have added inside the MyFunction3 and MyFunction4 get executed, only issue is logs not printing.
    But when I added the same code your 'unity-uwp-il2cpp-with-csharp-project-example' then it prints all the logs from Unity as well. I have used the same settings when building both projects.

    Also the AddActivatedEventArgs doesn't seem to print those logs in my project.
    If you have any idea what I have missed please let me know.

    Thank you.
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,906
    The output from Unity side will only show up in VS output window if you use mixed mode debugger. That setting can be found in project properties, debug tab.
     
  7. madhur

    madhur

    Joined:
    May 16, 2012
    Posts:
    86
  8. madhur

    madhur

    Joined:
    May 16, 2012
    Posts:
    86
    For anyone trying to integrate a C# UWP app with a Unity UWP app as a C# project and do the communication there, these are the steps I followed with the help of Tautvydas-Zilys in https://forum.unity.com/threads/solved-xamlunityconnection-in-il2cpp-xaml-c-il2cpp.495345/
    • Create a blank Universal App in C#. Name TestApp1

    • Create a New Unity project. Name TestApp1
      • Open the sample scene

      • Go to build settings add open scene

      • and switch to Universal Windows Platform

      • Select Build type as XAML project
      • Select Copy references option (then it’ll copy the unity player references to the sln)

      • Then Build
    • Update the .sln file of the C# UWP project by looking at the .sln file of the Unity built project. E.g add these parts
    Code (CSharp):
    1. ProjectSection(ProjectDependencies) = postProject
    2.  
    3.         {EA17D85E-DA6F-49EA-A879-8F48265B121D} = {EA17D85E-DA6F-49EA-A879-8F48265B121D}
    4.  
    5.     EndProjectSection
    6.  
    7. EndProject
    8.  
    9. Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Unity Data", "TestApp1\Unity Data.vcxitems", "{C102B663-472B-4E4D-B82E-F8B2FAD767AA}"
    10.  
    11. EndProject
    12.  
    13. Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Il2CppOutputProject", "Il2CppOutputProject\Il2CppOutputProject.vcxproj", "{EA17D85E-DA6F-49EA-A879-8F48265B121D}"
    14.  
    15. EndProject

    • Copy the Il2CppOutputProject folder and the UnityCommon file from the Unity built project to the C# UWP app folder and also copy the Data and Managed folders in TestApp1 folder from the Unity built project to C# project - TestApp1 folder . Also copy the Resource, StoreManifest, Unity Data, Unity Data.vcxitems, UnityGenerated files also there.

    • Add these lines to TestApp1 C# project file
    <Import Project="..\UnityCommon.props" />

    <ItemGroup>
    <Reference Include="UnityPlayer">
    <SpecificVersion>False</SpecificVersion> <HintPath>$(UnityWSAPlayerDir)Players\UAP\il2cpp\$(PlatformTarget)\$(Configuration)\UnityPlayer.winmd</HintPath>
    </Reference>
    </ItemGroup>
    <ItemGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64' OR '$(Configuration)|$(Platform)'=='Release|x64'">
    <Content Include="$(UnityWSAPlayerDir)Players\UAP\il2cpp\$(PlatformTarget)\$(Configuration)\WinPixEventRuntime_UAP.dll" />
    </ItemGroup>

    <ItemGroup>
    <Content Include="$(SolutionDir)build\bin\$(Platform)\$(Configuration)\GameAssembly.dll" />
    <Content Include="$(SolutionDir)build\bin\$(Platform)\$(Configuration)\GameAssembly.pdb" />
    </ItemGroup>


    <Content Include="Resource.res">
    </Content>
    <Content Include="StoreManifest.xml">
    </Content>
    <Content Include="Data\**">
    <ExcludeFromResourceIndex>true</ExcludeFromResourceIndex>
    </Content>

    • Now open the .sln in the C# UWP project.

    • Make sure the TestApp1 (Universal Windows) app is set as the Start up project. If the other project is set as Start up (Which is shown in bold letters) so change it by right clicking the project and setting as Start up project in Solution explorer

    • In VS select Debug - x64
    Then to add the C# to Unity communication part
    • Add the content of the App.xaml.cs and MainPage.xaml and cs file from the given example to the existing files (Make sure to change the namespace in all the places)

    • Comment the OnAppActivated method and the line calling it, in the App.xaml.cs in that example

    • In the same solution, go to new project -> C++ -> Windows Universal -> Windows Run time component. Select the path as \TestApp1 folder and select Add to solution and name it as IL2CPPToDotNetBridge

    • Copy the content to the Class1.h file and comment the Class1 implementation code in the Class1.cpp file

    • From the TestApp1 Project add a Reference to the IL2CPPToDotNetBridge component (Add Reference -> Projects -> you will this project)

    • In the MainPage.xaml.cs add DotNetBridge class sample code given and call it from MainPage()

    • Clean and build the solution.

    • Copy the IL2CPPToDotNetBridge.dll and IL2CPPToDotNetBridge.winmd files that get generated in x64\Debug\IL2CPPToDotNetBridge and paste in the Unity project’s Assets folder

    • Create Script.cs file in Unity projects Assets/Scripts folder and add the given content there.

    • Then add that script to the Main camera

    • Build it and copy the same files/folders as earlier

    • Go to VS ->TestApp1 -> Properties -> Debug -> Debugger Type -> Mixed

    • Clean and build and run the solution.

    • You should see the Unity logs and the C# UWP app logs
     
    Last edited: Aug 30, 2019
  9. yakumoklesk

    yakumoklesk

    Joined:
    Dec 12, 2013
    Posts:
    4
    That is really cumbersome. I wonder if there could be a way of automating all of this, like for example giving some extra information to Unity when building projects.
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,906
    I'll see what I can come up with.
     
    yakumoklesk likes this.
  11. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,229
    and? because this is a nightmare :/
    When it wasn't il2cpp it was so much easier, now it's a pain in the butt to try to communicate accross
     
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,906
    You can do this in OnPostprocessBuild callback. Or, you can do this manually once and then check-in the project into your source control.

    We don't have anything in the works for this scenario, unfortunately.

    I understand it's cumbersome to do this but to be fair, you'd have to do exact same things for all other platforms Unity supports and while it was more convenient on .NET scripting backend, it was a mere side effect of the fact that Unity used the same .NET runtime as the "rest of the app". It wasn't an explicit design goal.

    If you explain what you're trying to do, perhaps I can come up with a less painful way to do it.
     
  13. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,229
    I'm trying to use the Microsoft Advertising sdk for xaml. I don't think I can call that from inside unity (or is there a way to include the package to the solution?). So I have to be able to call methods from unity into the visual studio xaml project so display/retrieve ad status.
     
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,906
    You can call into it from Unity. I threw together a project that does just that. Just don't forget to add a reference to the ads SDK in generated Visual Studio project (otherwise you'll get class not registered exception).

    I used this page as guidance in order to be able to do it: https://marketplace.visualstudio.com/items?itemName=AdMediator.MicrosoftAdvertisingSDK
     

    Attached Files:

  15. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,229
    nice, i didn't know you could add the package into the unity project.
     
  16. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,906
    You can add anything that's reference-able from a VS project.
     
  17. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,229
    well, when i try to build i get this error:
    error CS0234: The type or namespace name 'Advertising' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

    I open the Setup.cs file and try to add a reference, but visual studio does not allow me to add the Advertising SDK (or any reference in fact). How did you manage it to include it?
     
  18. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,229
    Ok, I figured it out. I add to attach the Microsoft.Store.winmd file you created. Now the question is... how did you create that file?
     
  19. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,906
    I took it from the nuget package and placed it in the Unity project.
     
  20. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,229
    I'm trying to do:
    using Microsoft.Services.Store.Engagement;

    any idea how to be able to generate the winmd for that? It fails to compile saying it doesn't find that namespace (when I try to build for UWP)
     
  21. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,906
  22. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,229
    so basically the way it works is to download the nuget, hope there is a .winmd file and put it in?
     
  23. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    7,906
    There will be either .winmd and .dll files in that NuGET package. Visual Studio needs to reference those files to compile C# code in a UWP application - it's not magic. It just extracts the NuGET package automatically for you, which Unity does not do.
     
  24. MuhammadHaseeb56

    MuhammadHaseeb56

    Joined:
    Mar 12, 2019
    Posts:
    11
    i cant understand it please me to hw i can apply ads in il2cpp build?
     
  25. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    115
    @Tautvydas-Zilys Hello, I am attempting to embed a Unity view inside UWP Xaml C# project and I believe I am almost there following your guide and I thank everyone for helping me get to this point. I followed the instructions, and I am getting the Unity logs as well as the C# UWP app logs, and the Unity view comes up successfully, but I am also getting many of these logs every second:

    Exception thrown at 0x00007FF95F9AA859 in [Project Name].exe: Microsoft C++ exception: _com_error at memory location 0x000000A83AB011A8.

    Have any of you encountered this before or know what could be causing this if I did a step wrong? Thanks again.
     
    Last edited: Apr 11, 2020
  26. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    115
    Maybe this can help show what's happening:

    (note I commented out the bridge bootstrapper lines trying to narrow the problem down so those logs are not here)

    Update: Okay, I believe this is a Unity problem, not a problem with this method as I still get these errors if I simply build and run the Unity built UWP solution from the c++ project. Still not sure what to do from here though.

    Update 2: Okay, I found this, and "it's not a bug": https://issuetracker.unity3d.com/issues/lwrp-project-spawns-multiple-exception-in-uwp-build



    'Music Visualizer Test.exe' (Win32): Loaded 'C:\Windows\System32\biwinrt.dll'.
    onecoreuap\xbox\devices\api\winrt\pnpapiwrapper.cpp(385)\Windows.Gaming.Input.dll!00007FF955496CA4: (caller: 00007FF95549A640) ReturnHr(1) tid(8a2c) 8685C003 onecoreuap\drivers\mobilepc\sensors\convergence\common\pnpmanager\pnpmanager.cpp(591)\Windows.Devices.Sensors.dll!00007FF8BB7BDFD5: (caller: 00007FF8BB7AA56F) Exception(1) tid(32f8) 80070490 Element not found.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: wil::ResultException at memory location 0x0000006BED47CD80.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
    The thread 0x29b8 has exited with code 0 (0x0).
    onecoreuap\drivers\mobilepc\sensors\convergence\api\winrt\public\internal\simpleorientation.cpp(127)\Windows.Devices.Sensors.dll!00007FF8BB7B5B76: (caller: 00007FF8BB7ABB9B) Exception(2) tid(32f8) 80070490 Element not found.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: wil::ResultException at memory location 0x0000006BED47CF50.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
    onecoreuap\drivers\mobilepc\sensors\convergence\api\winrt\public\internal\sensorserver.cpp(68)\Windows.Devices.Sensors.dll!00007FF8BB7A744B: (caller: 00007FF8BB7A3763) ReturnHr(1) tid(32f8) 80070490 Element not found.
    onecoreuap\drivers\mobilepc\sensors\convergence\api\winrt\public\lib\simpleorientationsensor.cpp(160)\Windows.Devices.Sensors.dll!00007FF8BB79182C: (caller: 00007FF8BB78EBC3) ReturnHr(2) tid(32f8) 80070490 Element not found.
    onecoreuap\windows\wgi\winrt\display\displaycommon.cpp(411)\Windows.Graphics.dll!00007FF952D3E65E: (caller: 00007FF952D3DBF2) ReturnHr(3) tid(32f8) 80070490 Element not found.
    onecoreuap\windows\wgi\winrt\display\displaycommon.cpp(411)\Windows.Graphics.dll!00007FF952D3E65E: (caller: 00007FF952D3D7EC) ReturnHr(4) tid(32f8) 80070490 Element not found.
    onecoreuap\windows\wgi\winrt\display\displaycommon.cpp(411)\Windows.Graphics.dll!00007FF952D3E65E: (caller: 00007FF952D3DBF2) ReturnHr(5) tid(32f8) 80070490 Element not found.
    [0.142961 / 4.518562] - AppCallbacks::SetupOrientationSensorEvents
    'Music Visualizer Test.exe' (Win32): Loaded 'C:\Windows\System32\windows.applicationmodel.datatransfer.dll'.
    'Music Visualizer Test.exe' (Win32): Loaded 'C:\Windows\System32\coml2.dll'.
    'Music Visualizer Test.exe' (Win32): Loaded 'C:\Windows\System32\edputil.dll'.
    [0.185990 / 4.547715] - AppCallbacks::Load
    [2.272083 / 4.547944] - Starting first scene loading
    The following GlobalManagers were stripped from the build (Either because they're not used or not supported on this platform):
    ClusterInputManager
    UnityConnectSettings
    Begin showing splash screen.
    [1.267255 / 5.815199] - OnWindowActivated event - Deactivated.
    The thread 0xad24 has exited with code 0 (0x0).
    The thread 0x9c9c has exited with code 0 (0x0).
    [0.306519 / 6.121719] - OnVisibilityChanged event - Hidden.
    [0.430904 / 6.552623] - Finishing first scene loading
    UnloadTime: 2.426200 ms
    [0.033215 / 6.585838] - First level loaded
    [0.000299 / 6.586137] - PerformUpdateAndRender started
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: _com_error at memory location 0x0000006BF14E0E58.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: _com_error at memory location 0x0000006BF14E0E58.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: _com_error at memory location 0x0000006BF14E0E58.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: _com_error at memory location 0x0000006BF14E0E58.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: _com_error at memory location 0x0000006BF14E0E58.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: _com_error at memory location 0x0000006BF14E0E58.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: _com_error at memory location 0x0000006BF14E0E58.
    End showing splash screen.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: _com_error at memory location 0x0000006BF14E0E58.
    Exception thrown at 0x00007FF95F9AA859 in Music Visualizer Test.exe: Microsoft C++ exception: _com_error at memory location 0x0000006BF14E0E58.
     
    Last edited: Apr 12, 2020
unityunity