1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

C# 6.0

Discussion in 'Scripting' started by supremegrandruler, Mar 27, 2015.

  1. timintal

    timintal

    Joined:
    Jul 23, 2012
    Posts:
    8
    I have an issue with unity 5.6. As soon as I import C# 6.0 support into my project all log errors has no line number where exactly in script this error occured.
    Before importing: [​IMG]

    After importing: [​IMG]

    Is there a way to fix this as it really slows development process?
     
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,260
    Usually async/await code looks simpler than one using coroutines. I would rewrite it this way:
    Code (CSharp):
    1. public async void OnButtonClicked() // async methods work fine as direct event handlers
    2. {
    3.     something.StartTheThing();
    4.     while (something.IsDone == false)
    5.     {
    6.         await 0;
    7.     }
    8.     DoStuffThatNeedsTheMainThread(); // will run in the main thread
    9. }
    However, if StartTheThing() returns UnityEngine.AsyncOperation, it can be even simplier:
    Code (CSharp):
    1. public async void OnButtonClicked()
    2. {
    3.     await something.StartTheThing();
    4.     DoStuffThatNeedsTheMainThread(); // will run in the main thread
    5. }
    If it returns something else, other than void, it's possible to write an awaiter for that return type so that the previous example could also work. It's also possible to write an awaiter for the type of that 'something' itself, if it would make sense. So we could write
    Code (CSharp):
    1. public async void OnButtonClicked()
    2. {
    3.     something.StartTheThing();
    4.     await something;
    5.     DoStuffThatNeedsTheMainThread(); // will run in the main thread
    6. }
    Such an awaiter wouldn't be much different from the AsyncOperationAwaiter that you can find inside AsyncTools.cs.
     
    Last edited: Apr 21, 2017
    makeshiftwings likes this.
  3. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,260
    There must be something wrong with debug symbols. Are you on Mac or Windows?
     
  4. timintal

    timintal

    Joined:
    Jul 23, 2012
    Posts:
    8
    I'm on Mac, and my colleague on Windows doesn't have this problem. May be something wrong with Mono.
     
  5. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,260
    You must be using Roslyn compiler. Roslyn can't generate debug symbols in 'pdb' format on Mac, and the 'portable pdb', that it can generate, is not supported yet by Mono. So the solution is to switch to Mono C# 6 compiler via deleting the Roslyn folder.
     
  6. buistvo

    buistvo

    Joined:
    Apr 21, 2017
    Posts:
    1
    Great work with that pack!
    I have a problem: VS2017 compiles my code but unity not. Is says:

    Assets\CSharp vNext Support\AsyncTools\AsyncTools.cs(13,29): error CS0433: The type 'Lazy<T>' exists in both 'System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35' and 'nunit.framework, Version=3.5.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb'

    anyone knows how to fix this?
     
    Last edited: Apr 21, 2017
  7. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    263
  8. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    484
    Wait, I thought C# 6.0 support wouldn't be included until Unity 2017
     
  9. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,067
    @alexzzzz I've been using this for a while but decided to check out the new Unity beta with .NET 4.6 support, but they only support C# 6. Do you know if it's possible to use this with the new Unity runtime so that I can use both C# 7 and .NET 4 libraries like some sort of madman?
     
    Qbit86 likes this.
  10. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,260
    I'm currently kind of out of business for a while and won't be able to test 2017.1 beta for at least a week, maybe two.

    Theoretical we should be able to use Roslyn to compile C# 7.0 for the new runtime, we just need to switch the compiler options to target CLR 4 instead of CLR 2. TPL and AsyncBridge libraries won't be needed anymore. The only thing the new Unity runtime lacks for full C# 7 support is ValueTuple type, but now we can take and use its official version from NuGet.
     
    mdrotar likes this.
  11. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,067
    Sweet. For now I've undid my tuples and out var's and went back to boring old C# 6 so I can be more "official" in my testing of the 2017.1 beta. It's exciting to see that after years of grueling effort, the Unity team is finally almost near the point of matching what you threw together one day just to see if it was possible. ;)
     
    Qbit86 likes this.
  12. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,260
    I'm wondering, does it make sense to write C# 7.x code in Unity 2017 beta still targeting the old Mono 2.x? Shouldn't living on the edge forbid such compromises?
     
  13. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,260
    C# 7.0 support for Unity 2017.1 / Mono 4.6

    [link removed]

    I removed Task Parallel Library and Async Bridge, and also removed my custom UnitySyncronizationContext as well as UnityTaskScheduler.

    Since Unity has only one SynchronizationContext for the main thread, stuff like this no longer works:
    Code (CSharp):
    1. await AsyncTools.ToUpdate();
    2. await AsyncTools.ToLateUpdate();
    3. await AsyncTools.ToFixedUpdate();
    Now there are only two options left:
    Code (CSharp):
    1. await AsyncTools.ToThreadPool();
    2. await AsyncTools.ToMainThread();
    I had to change the implementation of custom awaiters for UnityEngine.AsyncOperation and for floats/integers, they also work now:
    Code (CSharp):
    1. await .5f; // wait 0.5 seconds
    2. await 2; // wait 2 seconds
    3. await 0; // wait for the next frame
    4.  
    5. var request = UnityWebRequest.Get("http://placeimg.com/512/512");
    6. await request.Send(); // awaits the completion
     
    Last edited: Jun 7, 2017
  14. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,260
    I believe I included demo scripts and maybe even scenes in the previous package ― it wasn't intentional.

    https://bitbucket.org/alexzzzz/unity-c-5.0-and-6.0-integration/downloads/CSharp70Support 3.1.0 (for Net 4.6).zip

    I've finally more or less cleaned up the mess after migrating to the new runtime. All the obsolete stuff is removed:
    1. AsyncBridge
    2. Task Parallel Library
    3. TupleBridge (replaced with the official NuGet package)
    4. Roslyn C# 6.0 support (feels obsolete now when 7.0 is available)
    5. Standalone Mono C# 6.0 support (since it's already integrated in Unity 5.5+)

    Also checked all the language feature tests in the demo project in the repository and added a couple of missing tests to cover the final C# 7.0 feature changes. Everything seem to work fine except
    1. Resharper claims that interpolated strings can't be converted to FormattableString type, however the compiler doesn't agree.
    2. I haven't tried ValueTasks yet.
     
    Last edited: Jun 8, 2017
  15. ccklokwerks

    ccklokwerks

    Joined:
    Oct 21, 2014
    Posts:
    43
    Well, I'm feeling rather stupid, so maybe someone can point out what I have done wrong.

    I'm on Mac with 2017.1.0b8, I installed the Mono framework, copied the folder parallel to Assets and imported the package.

    I wrote a test MonoBehavior which tries to do this in start, where obj is a SerializeField which is not set.

    void Start()
    {
    Transform t = this.obj?.transform;
    Debug.Log(t); ​
    }

    In the Unity editor, I don't get compile errors, but when I run, I get an unassigned reference exception. If this is working correctly, shouldn't t be assigned null?

    Also, and here you can laugh at me, but MonoDevelop-Unity is saying it doesn't support 4.6 nor langversion 7. I'm guessing I can't use MonoDevelop with this?
     
  16. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    347
    Did you change the target .Net framework to 4.5 (or 4.6, I forget) in your project settings?
     
    ccklokwerks likes this.
  17. ccklokwerks

    ccklokwerks

    Joined:
    Oct 21, 2014
    Posts:
    43
    Yes, I did do that... I'm going to go do that in a separate test project because (presumably) MonoDevelop should not be complaining about 4.6. edit: Same behavior, so I screwed up my 2017 beta install or something... edit2: I needed to separately get the prototype MonoDevelop!
     
    Last edited: Jun 9, 2017
  18. Artery

    Artery

    Joined:
    May 30, 2017
    Posts:
    1
    @alexzzzz Awesome job! Thank you very much for your work, that's exactly what I needed!
     
  19. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    13
    @alexzzzz
    • I removed the old "Assets\CSharp vNext Support" Folder in unity.
    • Then downloaded "CSharp70Support 3.1.0 (for Net 4.6).zip" from your bitbucket download page.
    • I tried to install .net 4.6 (NDP462-KB3151802-Web) but it told me I already have a newer version.
    • I did use the "ngen install.cmd"
    • I have set player settings -> .NET 2.0
    • I have used "Reimport All" and also restarted unity (5.6.0p3)
    • I am on Windows 10 x64
    Now I get a lot of compilation errors inside of the CSharp vNext Project:
    • Assets/CSharp vNext Support/AsyncTools/AsyncTools.cs(35,15): error CS1644: Feature `interpolated strings' cannot be used because it is not part of the C# 4.0 language specification
    • Assets/CSharp vNext Support/AsyncTools/AsyncTools.cs(46,36): error CS1644: Feature `expression bodied members' cannot be used because it is not part of the C# 4.0 language specification
    • Assets/Loader/OPCParser.cs(126,93): error CS1644: Feature `null propagating operator' cannot be used because it is not part of the C# 4.0 language specification
    And those 3 errors several times. Where did I do wrong?

    Best Wishes

    1. Edit: CSharp70Support 2.0.5.zip seems to work.
    2. Edit2: I am not able to click on error message anymore to open them directly in Rider. (Ok this looks like to be more of a problem with Rider.... Works after a restart.)

    1. Edit3: I get another error in Rider: "Ambigous Reference System.Threading.Task, System.Threading.Task" and it asks me if I want to use Task from System.Threading or from mscorlib.
    2. Fixed by manually deleting System.Threading from the References View in Rider.
    3. Edit4: After adding the import again it obviously adds the reference again and it is ambigous again. Any ideas?
    Edit 5: When building from command line I get

    ArgumentException: The Assembly System.Runtime is referenced by System.ValueTuple ('Assets/CSharp vNext Support/System.ValueTuple.dll'). But the dll is not allowed to be included or could not be found.
    at UnityEditor.AssemblyHelper.AddReferencedAssembliesRecurse (System.String assemblyPath, System.Collections.Generic.List`1 alreadyFoundAssemblies, System.String[] allAssemblyPaths, System.String[] foldersToSearch, System.Collections.Generic.Dictionary`2 cache, BuildTarget target) [0x00245] in C:\buildslave\unity\build\Editor\Mono\AssemblyHelper.cs:142
    at UnityEditor.AssemblyHelper.FindAssembliesReferencedBy (System.String[] paths, System.String[] foldersToSearch, BuildTarget target) [0x0001e] in C:\buildslave\unity\build\Editor\Mono\AssemblyHelper.cs:179

    Sounds bad. Is this harmless - if not, what do I have to do to fix it? When I try to run the generated player.exe it tells me there is no player_Data folder next to it (but there is) and in other Threads I have found that a missing dll can be the reason so this two problems look connected to me.

    Edit6: And the following warnings:
    Assets\CSharp vNext Support\TupleBridge\ValueTuple.cs(289,22): warning CS0436: The type 'ValueTuple<T1>' in '<...>\Assets/CSharp vNext Support/TupleBridge/ValueTuple.cs' conflicts with the imported type 'ValueTuple<T1>' in 'System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Using the type defined in '<...>\Assets/CSharp vNext Support/TupleBridge/ValueTuple.cs'.
     
    Last edited: Jun 30, 2017
  20. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,260
    https://bitbucket.org/alexzzzz/unit...loads/CSharp70Support 3.2.3 (for Net 4.6).zip

    - Unity 2017.1 and 2017.2 beta support
    - Updated C# compiler to version 2.3.1.61919
    - C# compiler version is written to the log file
    - Added C# 7.1 feature tests
    - Replaced System.ValueTuple.dll with a custom version that doesn't reference anything except System.dll and System.Core.dll

    Tested in standalone builds and in the editor on Windows (2017.1 and 2017.2), and on MacOS in (2017.1)
     
    Last edited: Jul 22, 2017 at 3:25 PM
  21. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,260
    CSharp70Support x.x.x (for Net 4.6).zip is for Unity 2017.x with built-in Net 4.6 support.