Unity 3D 2017 简单实现支持c# 6.0 语言编译

其实unity2017只需要一步,把.net api兼容性修改程.net api 4.6。

副作用就是必须带.net 4.6发布。

另外通过unity 5.5,5.6的mcs.rsp加一行-langversion:6也能用。可以继续使用.net 2.0

https://blog.csdn.net/love_hot_girl/article/details/82987097

How to setup Unity 2017 and Visual Studio 2015 to use C# 6. (Step by Step)

https://whatroidoes.wordpress.com/2017/11/10/how-to-setup-unity-2017-and-visual-studio-2015-to-use-c-6-step-by-step/

How to setup Unity 2017 and Visual Studio 2015 to use C# 6. (Step by Step)

Posted on November 10, 2017 by Roi de Janeiro

Unity 2017 brought support for C# version 6. Unfortunately it doesn’t work out of the box with Visual Studio. Trying to google the problem I wasn’t able to find an all-in-one solution, but step by step I was able to work it out. This should work with a clean new project as well as with a previously created one.

The following is a step-by-step-guid to get things working. For the hasty ones: There is a summary of all steps at the end of this post. For the rest: I tryed my best to explain what we’re doing here. Don’t blame me if If my explanations are not 100% correct. I wouldn’t call myself an advanced user, if I didn’t have to for this to work. ?

1. Get Unity to use DotNet 4.6

C# 6 is part of DotNet v.4.6. Unity by standart uses DotNet v.3.5. To change this, in Unity go to Edit => Project Settings => Player. Under Configuration change Scripting Runtime Version from Stable (.Net 3.5 Equivalent) to Experimental (.Net 4.6 Equivalent). Unity should now be ready to run C# 6 code.

2. Getting access to your project properties

Create a new C# script or choose an existing one and double click it to start Visual Stuio. (Im using VS 2015 v.3 but I think it should work in other versions too.) If you want to see how it doesn’t work yet, just insert some C# 6 exclusive code to a valid part of your script. For example:

int test {get;}

VS should mark this as erraneous, saying “Feature ‘readonly automatically implemented properties’ is not available in C# 4. Please use language version 6 or greater.” As you may have guessed, we also have to configure VS to use C# 6.

The C# version used is set in VS’ Project Properties. They are located under Project as the bottom menu item. (Instead of project the name of your project is used.) The problem is: If you klick it, the screen flashes and nothing happens. This is because Unity preconfigured the project for you and since you could potentially mess up your whole VS-Unity-pipeline playing with the project settings, the developers made it acessible only if you consider yourself an advanced user. So let’s tell VS that you are.

Navigate to Tools => Options. This should open up a window with a list of cathegories on the left. Choose Tools for Unity. Here under Miscellaneous (on the right side of the window) change Access to project properties to true, wich of course grants you said access to the project properties. Klick Ok to close the window.

3. Change project properties to use C# 6

Now try navigating to Project => Projec Properties again. This time a new tab should open up. On the left click Application if it is not highlightet. Here under Target framework choose .Net Framework 4.6. Visual Studio will tell you, that it wants to restart. Let it!

Next click Build on the lefthand list and finally proclaim yourself an advanced user by klicking the Advanced-button in the bottom right of the tab.

Another window should pop up, finally containing a setting for the utilized C# Language Version. Just change it from C# 4.0 to C# 6.0, click Ok and after a restart of VS everything should work fine.

 

Summary:

  1. In Unity:
    Edit => Project Settings => Player 

    change Scripting Runtime Version from Stable (.Net 3.5 Equivalent) to Experimental (.Net 4.6 Equivalent)
    Ok
  2. In Visual Studio:
    Tools => Options => Tools for Unity

    set Access to project properties to true
    Ok
  3. In Visual Studio:
    Project => Project Properties => Application
    as Target framework choose Net Framework 4.6.
    klick  Ok and agree to restart Visual Studio
    Project => Project Properties => Build => Advaned
    change Language Version to C# 6.0
    Ok
    restart Visual Studio
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值