Search Unity

Question Using language version 9.0 in C# code

Discussion in 'Scripting' started by Olipool, Nov 13, 2022.

  1. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Hi all,

    as far as I understand, Unity generates the .csproj files by itself so when I share a project in GIT or as a .unitypackage file those .csproj files are excluded and are generated on import of the files.
    Now I want to use some newer language features like the switch expression. Since Unity did not let me use it in the scripts I had to change the files manually and set the language version to 9.0 like this:
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    3.   <PropertyGroup>
    4.     <LangVersion>9.0</LangVersion>
    5. ...
    But how do I enforce this for other team members without including the .csproj in the GIT or .unitypackage? I think it is especially crucial when publishing to the Asset Store.

    Thank you very much for any pointers! :)
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,861
    I think people just distribute pre-compiled .dll's to get around this. This is how Odin works at least.
     
    Olipool likes this.
  3. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Thank you, that is good to know for sure! I am planning to release a complete project where devs should explicitly be able to look into the source code so that option is not for me I guess.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,861
    Then offer both (again, what Odin does).

    Do note that Unity is the compiler and each version of Unity only supports up to a certain C# language level. Even if you somehow 'set' the level to something higher, if it's not supported by Unity it will throw a bunch of errors when it tries to compile syntax it doesn't support.

    So I don't think what you want to do is going to work anyway. Probably just worth using the syntax level of the lowest version you expect to support, or distribute .dll's.
     
    Last edited: Nov 14, 2022
    Kurt-Dekker likes this.
  5. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Thanks again! I don't want to go above what Unity can handle but regarding the link version 9 is supported, it is just that the .csproj files are not created that way. So I needed to add the 9.0 manually.
    I will look into the DLL solution!
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    You shouldn't have to edit the csproj file to get C# 9.0 features.

    My LangVersion in 2021.3.9f1 (version I happen to be on) is 9.0 without me editing that file.

    All you have to do is go to:
    Edit->Project Settings->Player->Other Settings

    And change the Api Compatibility Level to .Net 4.x


    This will update your ProjectSettings.asset file (found in your project root/ProjectSettings) to reflect what version it should compile. And based on this the *.csproj file will be generated with the appropriate configuration.

    ...

    Basically, you should never edit the generated *.csproj files because Unity will likely overwrite them on you with out you knowing.
     
    Olipool and orionsyndrome like this.
  7. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Thank you for the hint, I will check out your version. I am on 2021.3.6f1 and don't have that option (not with mono or IL2CPP) and the release notes did not mention anything about it.

    That would be a possibility to share with others but I think that will only work if the .unitypackage gets exported as a complete project. If you just want to share some scripts that won't overwrite the settings on the users' computer/Unity preferences.

    unity_complevel.PNG
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Olipool likes this.
  9. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Thank you for the clarification and the link. I was really thrown off by the different naming.

    I tested it with a new project and the version is 9.0 now. I could have sworn it was 8.0 before and I had to set it to 9.0. I apologize for the unnecessary confusion this post has caused. Now my initial question about sharing content can be answered by demanding a minimum version of Unity to use.

    Thanks all!
     
    MelvMay likes this.
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    The images are different just cause different versions of Unity.

    I didn't even take that image from my version of Unity (as I wasn't on the appropriate device at the time), and instead was taken from the MSDN article about it:
    https://learn.microsoft.com/en-us/visualstudio/gamedev/unity/unity-scripting-upgrade

    Of course, mind you, you can always select the change and then check your regenerated *.csproj to confirm.
     
    Olipool likes this.
  11. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Thanks for the clarification! :)