In meshsaver.cs, asynchronous functions are utilized. However, when I try to implement them in my own application, I get a 'Feature asynchronous functions cannot be used because it is not part of the C# 4.0 language specification' error. How do I implement async functions in Unity?
Unity, by default targets .net 3.5. In your player settings you can update it to .net 4.6.
I have had it set that way since I began my project and it's thrown the error regardless :(
You might want inquire over in the Unity forums to see if anyone might know.
@gjrgj
It is not implemented in current published version of unity (5.6 1f1). And it could be implemented in the following versions.
Currently you have to use callbacks to expose your interfaces, and use coroutines to wait for value change.
@waynebaby I have also found that this is mostly the solution you will have to go with. You may find it easier to wrap your code with promises (uPromse) such that handling your async code may be easier.
I did some more research over the weekend and I think I understand how to do it - will using preprocessor directives to ignore certain blocks of code in Unity still compile them within Visual Studio? For UWP apps, I'm under the impression that the VS compiler can selectively deal with code that isn't built with the Unity compiler. Is that true?
While this is very doable, do even UWP apps support async keywords? And even if it turns out they do, is this really optimal? I am working on a project which is starting to take a quite large code base. The more you differentiate the version for HoloLens vs the Unity editor (which you typically develop with), the more problems you will have with maintenance and further development. Also, you may in the future want to implement spectator view, etc. which depends on a working mono solution. Just my subjective take on this.
I see, thanks for the feedback. @christjt they do I believe, as one of the scripts in Microsoft's Holotoolkit utilizes async (meshsaver.cs).
Just to affirm my understanding of the Unity -> VS dev process: if I wrap code with a #if (WINDOWS_UWP) directive, then it will be ignored when compiling the project build within Unity. However, when building the final version in VS to target the Windows store, the code in this block will be compiled and used in the final version of the app. So even though it is ignored by Unity, it will still work and be present in the end?
Haven't read up that much about the different compiler directives, but I usually use #if NETFX_CORE for whatever is to only be run on HoloLens and #if UNITY_EDITOR for only unity stuff. (#if !UNITY_EDITOR can be used for anything BUT the unity editor if you wanna be clever)
I figured it out - I just wasn't understanding the function of the .sln that is generated. Unity generates whatever files VS needs from it, then final building of C# 5/6 and UWP stuff is compiled within VS. So stuff like async functions can be used with the WINDOWS_UWP directive and will be ignored by Unity. I guess the rule of thumb is: for UWP, only worry about compilation within VS.
Going to close this. It's probably been archived by google by now.
Still getting this. 2017.4.6f1 with latest holotoolkit as well as on 2018.1.8 and 2018.2
Most helpful comment
While this is very doable, do even UWP apps support async keywords? And even if it turns out they do, is this really optimal? I am working on a project which is starting to take a quite large code base. The more you differentiate the version for HoloLens vs the Unity editor (which you typically develop with), the more problems you will have with maintenance and further development. Also, you may in the future want to implement spectator view, etc. which depends on a working mono solution. Just my subjective take on this.