Search Unity

C# 6.0

Discussion in 'Scripting' started by Deleted User, Mar 27, 2015.

  1. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,400
    1. Ok.
    2. Ok.
    3. Ok.
    4. It probably will. The code that was previously executed in the main thread will now run in background threads.

    Check this folder, you'll need all three .cs files. I've added UnityTaskScheduler class and AsyncTools.MainThreadScheduler static property. There's also a demo here.

    5 - 6. Can't say anything specific about async/await and coroutines except that generally all the awaiters should continue to work.

    They are completely independent.

    It will be deprecated. There is experimental Incremental C# Compiler package that adds Roslyn and C# 7.x support officially. As soon as it's released there will be no reason to use third-party plugins. I've switched to using this package myself.

    PS
    I guess I could continue to make fixes for 2017.4 LTS is case something break unexpectedly which I hope will not happen.
     
    Last edited: May 16, 2018
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,400
    Copy Microsoft.CSharp.dll into your project.
     
    malyzeli likes this.
  3. malyzeli

    malyzeli

    Joined:
    May 1, 2014
    Posts:
    11
    Yep, problem solved. Can you please explain why it worked before, even though DLL was not copied there? Is this DLL used only in IDE context? Would be glad if I understand what is going on there.

    I also noticed you saying above that this tool will be deprecated because of official incremental compiler. If we manage to upgrade our project to Unity 2018 then we can switch to using this incremental compiler as well. I would like to know if there is something specific to consider before doing this change (talking about switching the compiler, not the version of Unity).

    Thanks for your time!
     
  4. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,231
    Note for user of this Roslyn hacks, I have just transferred to Unity Incremental Compiler (https://forum.unity.com/threads/unity-incremental-c-compiler.523993/) that also uses Roslyn with C# 7.2, all of my code that was using the hack with C#7.2 feature still compiles without any problems. (+huge speed boost) One problem in that thread seems to be UNet, but luckily I am not using UNet right now.
     
    malyzeli likes this.
  5. MapMan

    MapMan

    Joined:
    Oct 27, 2013
    Posts:
    38
    @alexzzzz Thank you for your reply and implementing UnityTaskScheduler.

    Unfortunately, my game hangs on the very first
    await AsyncTools.ToMainThread();
    call. The editor is responsive, it's just that the await never returns and code after the call never executes. By using
    AsyncTools.WhereAmI
    method, I can confirm that the execution is on a background thread. Any ideas what could this be caused by? I tried switching to the incremental roslyn compiler and the same issue persists.
     
  6. MapMan

    MapMan

    Joined:
    Oct 27, 2013
    Posts:
    38
    I figured it out.

    In my game, in the first scene, I have a GO with an initialization script. That script is set to have highest script execution order (executes before any other script). In the Awake method, I create a new Task with initialization logic. Because the first call to
    await AsyncTools.ToMainThread();
    happens so fast, the
    AsyncTools.Initialize
    method hasn't been invoked yet and thus it's impossible to use the awaiter.

    To solve this, I had to force AsyncTools initialization before any of my game logic runs. The best way to do it, is to simply pass
    RuntimeInitializeLoadType.BeforeSceneLoad
    parameter to
    RuntimeInitializeOnLoadMethodAttribute
    for
    AsyncTools.Initialize
    . This will cause the Initialize method to be invoked before the first scene is loaded and thus, regardless of script execution order, before any Awake methods are invoked.
     
    alexzzzz likes this.
  7. someguywithamouse

    someguywithamouse

    Joined:
    Mar 25, 2018
    Posts:
    9
    @alexzzzz I know this is mayyybbeeeeeee a bit off-topic from where we're at right now, but, uh......

    Do you guys know where to find the version that uses C# 5, instead of version 6 or 7? I'm trying to get PrimaryConstructor to work, because I just spent almost an hour trying to figure out the FileIOPermission Constructor (I'm new to C#, deal with it. Only reason I made it here so fast is because of my past experience with JS), and I'm not scrapping a major part of my script just because Unity's nagging about how Primary Constructors aren't supported.
     
  8. lifeformed

    lifeformed

    Joined:
    Aug 1, 2012
    Posts:
    51
    @alexzzzz Nice work on your C#7 package.
    I have a question:
    When I am using the Visual Studio debugger, every variable used in a local function is unavailable. It says they're not in scope. For example:
    Code (CSharp):
    1.     void Start ()
    2.     {
    3.         int x = 0;
    4.         int y = 0;
    5.         void Test()
    6.         {
    7.             x++;
    8.         }
    9.         Debug.Log("Breakpoint here");
    10.     }
    I can't inspect the variable x, but I can inspect y. This doesn't happen in a normal, non-Unity C# project. I'm using Unity 2018.2.7f1 and Visual Studio Community 2017. Have you seen this issue before?

    Thanks.
     
  9. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    1,763
    this has been deprecated for 5 months.
     
  10. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    879
    Also, 2018.3 has C#7.2 support and 2018.2 is C#7, so not sure why they want to use this.
     
  11. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    1,763
    well I wanted it for 5.6.6 but it wasn't there...
     
  12. lifeformed

    lifeformed

    Joined:
    Aug 1, 2012
    Posts:
    51
    Oh oops. Are you sure? I am using 2018.2 and it's not letting me use C#7 features. I can't find any documentation about it anywhere.

    EDIT: Oh looks like I have to use the incremental compiler? Let me give it a shot.

    EDIT2: The incremental compiler works, but I'm still getting the same problem with the debugger. I'll try asking on the incremental compiler thread.
     
    Last edited: Sep 13, 2018
  13. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    879
    You shouldn't need to use the incremental compiler. You just need to set the highest .net in your Unity project's Player settings. And perhaps delete the csproj files in your project root to let them all regenerate.
     
    twobob likes this.
  14. lifeformed

    lifeformed

    Joined:
    Aug 1, 2012
    Posts:
    51
    That doesn't seem to work for me. Unity still gives me errors if I use C# 7 features, like local functions, even if I change the version in the project settings in Visual Studio. I've tried it on a brand new project and it didn't work. Does it work for you?
     
  15. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    1,763
    Okay. So paste a tiny bit of code that doesn't work you.
     
  16. lifeformed

    lifeformed

    Joined:
    Aug 1, 2012
    Posts:
    51
    The code in my original post is enough to cause the problem. Just make a new project and put this in a new script on a GameObject:

    Code (CSharp):
    1.     void Start ()
    2.     {
    3.         int x = 0;
    4.         int y = 0;
    5.         void Test()
    6.         {
    7.             x++;
    8.         }
    9.         Debug.Log("Breakpoint here");
    10.     }
    At the breakpoint, x cannot be examined, but y can.
     
  17. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    1,763
    Is it a scope issue? Or am I being stupid

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SimpleTest : MonoBehaviour
    6. {
    7.  
    8.     int x,y = 0;
    9.  
    10.     void Start()
    11.     {
    12.  
    13.     void Test()
    14.     {
    15.         x++;
    16.     }
    17.         Debug.Log(x);
    18.         Test();
    19.         Debug.Log(x);
    20.  
    21.         Debug.Log("Breakpoint here");
    22.     }
    23. }
    upload_2018-9-15_15-17-55.png

    upload_2018-9-15_15-35-44.png

    upload_2018-9-15_15-37-7.png
     
    Last edited: Sep 15, 2018
    Invertex likes this.
  18. lifeformed

    lifeformed

    Joined:
    Aug 1, 2012
    Posts:
    51
    Your code works, but if you put the declarations of x and y inside Start(), then it doesn't work. Here's what it looks like for me:

     
    Last edited: Sep 16, 2018