Search Unity

UWP app with UWP plug-in that uses async functions won't compile

Discussion in 'Windows' started by andyrobert, Feb 25, 2019.

  1. andyrobert

    andyrobert

    Joined:
    Mar 14, 2018
    Posts:
    6
    I'm using Unity 2017.4.19f1 I have a UWP app that utilizes a UWP plug-in (along with a placeholder). The Visual Studio solution creates successfully. However, when attempting to compile the solution I receive the following error,

    Error CS7069 Reference to type 'Task<>' claims it is defined in 'System.Runtime', but it could not be found

    I wrote a simplified app and plug-in to attempt to demonstrate the issue. The UWP plug-in code looks like this,

    Code (CSharp):
    1. namespace UWPLibTest
    2. {
    3.     public class Foo
    4.     {
    5.         public async Task<string> SayHello(string name)
    6.         {
    7.             await Task.Delay(500);
    8.             return $"Hello {name}!";
    9.         }
    10.     }
    11. }
    And the Unity code utilizing the plug-in looks like this,

    Code (CSharp):
    1. public class Tester : MonoBehaviour {
    2.    async void Start () {
    3.      var f = new Foo();
    4.      var r = await f.SayHello("Bob");
    5.       Debug.LogError(">>> " + r);
    6.     }
    7. }
    The error occurs at the var r = await f.SayHello("Bob") line. If I remove all that code and add an await Task.Delay(500) then the code compiles without issue.

    In Unity, my plug-in settings look like the following,



    Any ideas as to why I'm getting that error?
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    You need to wrap the usage of it in defines. Like this:

    Code (csharp):
    1. public class Tester : MonoBehaviour
    2. {
    3. #if UNITY_WSA && !UNITY_EDITOR
    4.     async
    5. #endif
    6.     void Start ()
    7.     {
    8. #if UNITY_WSA && !UNITY_EDITOR
    9.         var f = new Foo();
    10.         var r = await f.SayHello("Bob");
    11.         Debug.LogError(">>> " + r);
    12. #endif
    13.     }
    14. }
     
  3. andyrobert

    andyrobert

    Joined:
    Mar 14, 2018
    Posts:
    6
    Sorry if I was in unclear. The error occurs in the generated UWP project. I don't receive the error in the Unity editor because of the placeholder DLL.

    Also, to reiterate an await call to a standard library async function compiles without error. The error occurs when making the call to a plug-in.
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Which generated UWP project is that? How did you compile your plugin?
     
  5. andyrobert

    andyrobert

    Joined:
    Mar 14, 2018
    Posts:
    6
    The plug-in is compiled as part of a separate solution. It's a UWP library with a .net 4.7 place holder.

    The generated UWP project to which I was referring is the Visual Studio project that's generated when you set the platform to Windows Store in Unity and do a build.
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    What happens when you press CTRL + SHIFT + B? Does it not restore NuGet packages? Those errors should go away once NuGet packages are restored.
     
  7. andyrobert

    andyrobert

    Joined:
    Mar 14, 2018
    Posts:
    6
    Yes, it does the nuget restore as part of the build. I can make async calls to the standard library. It's when I try to make an async call to my plug-in that it falls. I have a larger plug-in it fails on and also a very simple plug-in and very simple Unity project (see the code in the original post) that fails also.
     
  8. andyrobert

    andyrobert

    Joined:
    Mar 14, 2018
    Posts:
    6
    The issue turned out to be that in my plug-in project I was targeting a version of UWP (10.0.17763) that was higher than (I'm guessing) the version of UWP that the version of Unity I'm using supports (2017.4.19f1). Targeting 10.0.14393 seems to fix the build error.

    Is there anywhere that lists the max UWP version usable for a given version of Unity?
     
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Unity supports all SDKs that are installed on your machine. The setting for the SDK is in the build window.
     
  10. andyrobert

    andyrobert

    Joined:
    Mar 14, 2018
    Posts:
    6
    At least part of the problem appears to be that regardless of the version of UWP that you choose in Unity, when the Visual Studio solution is generated it uses aan ancient version of the Microsoft.NETCore.UniversalWindowsPlatform nuget package which seems to give you all kind of errors if you then try to call in to a plug-in using a later version of that nuget package. Yeah, you can then manually bump up the version of the nuget package, but that's pretty annoying for a generated solution that doesn't get checked in to source control.
     
    Last edited: Feb 27, 2019
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    You can check in that solution to your source control if you want to modify it. We specifically don't even overwrite some of the files so you could make your own changes to it.