Search Unity

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:
    6,536
    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:
    6,536
    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:
    6,536
    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
    Tautvydas-Zilys likes this.
  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:
    6,536
    I'll see what I can come up with.
     
    yakumoklesk likes this.