简体   繁体   English

如何在Unity3D项目上启用c#7功能

[英]How to enable c# 7 features on Unity3D projects

I wrote a new method in my Unity3D project (using Visual Studio 2017), and for that method I need C# 7 or greater. 我在Unity3D项目中编写了一个新方法(使用Visual Studio 2017),对于该方法,我需要C#7或更高版本。 So I followed the instructions in this guide . 所以我按照本指南中的说明操作。

Now, when I try to open the project's properties, Visual Studio opens it, but then it suddenly closes right after opening. 现在,当我尝试打开项目的属性时,Visual Studio会打开它,但随后它会在打开后立即关闭。 Now I can't even open project's properties. 现在我甚至无法打开项目的属性。

This is how it looks: 这是它的样子:

GIF

Why am I getting the first and third errors? 为什么我会收到第一个和第三个错误? (the ones I showed in the gif) (我在gif中展示的那些)

EDIT 编辑

The Roslyn compiler is now supported in Unity 2018.3 , which allows you to use the latest C# 7 features! Unity 2018.3现在支持Roslyn编译器,允许您使用最新的C#7功能!

See: https://blogs.unity3d.com/2018/09/13/unity-2018-3-beta-get-early-access-now/ 请参阅: https//blogs.unity3d.com/2018/09/13/unity-2018-3-beta-get-early-access-now/


For Unity3D < 2018.3.* 对于Unity3D <2018.3。*

I will tell you how I have enabled C# 7.3 in Unity3D projects (although some of its features can't be compiled yet). 我将告诉你我是如何在Unity3D项目中启用C#7.3的(虽然它的某些功能还无法编译)。 But beware: this method is a hack! 但要注意:这种方法很糟糕! It is a premature and experimental usage of C# 7 in Unity. 这是Unity中C#7的过早和实验性使用。

If you follow the instructions below, I believe you may be able to easily track down the issue, given that in the case of your question it is quite difficult to provide a full MCVE for the SO community. 如果您按照以下说明操作,我相信您可以轻松找到问题,因为在您的问题中,为SO社区提供完整的MCVE非常困难。 But I believe that what you actually are trying to achieve is enabling C# 7 for Unity, and not solve the bizarre bug in the project properties window. 但我相信你实际想要实现的是为Unity启用C#7,而不是解决项目属性窗口中的奇怪错误。

Before proceeding, I would recommend completely reinstalling Visual Studio 2017 and Unity3D, just to make sure that you are not having any issue related to a bad installation. 在继续之前,我建议完全重新安装Visual Studio 2017和Unity3D,以确保您没有任何与错误安装相关的问题。 Always download Unity directly from Unity's webpage instead of using Viual Studio's Installer, so you can get the latest version available. 始终直接从Unity的网页下载Unity,而不是使用Viual Studio的安装程序,因此您可以获得最新版本。

Enabling C# 7 in Unity 3D projects (tested on Unity3D v.2018.2.10f, VS 2017 v.15.8.5) 在Unity 3D项目中启用C#7(在Unity3D v.2018.2.10f,VS 2017 v.15.8.5上测试)

  1. First create a new fresh empty project. 首先创建一个新的空项目。

  2. Go to Edit -> Project Settings -> Player , find the Other Settings section, then under Configuration / Scripting Runtime Version choose .NET 4.x Equivalent . 转到Edit -> Project Settings -> Player ,找到Other Settings部分,然后在Configuration / Scripting Runtime Version选择.NET 4.x Equivalent

  3. We want to tell mcs.exe to process the C# code using the new "experimental" features of C# 7. For that, just create the file mcs.rsp inside your Assets folder. 我们想告诉mcs.exe使用C#7的新“实验”功能处理C#代码。为此,只需在Assets文件夹中创建文件mcs.rsp Edit it and write the this line inside it: 编辑它并在其中写下这一行:

     -langversion:experimental 
  4. Now, create a new folder named Editor inside your Assets folder. 现在,在Assets文件夹中创建一个名为Editor的新文件夹。 Any scripts added to this folder will make Unity create a *.Editor.csproj project file, which holds contain scripts aimed to modify the Unity Editor. 添加到此文件夹的任何脚本都将使Unity创建一个*.Editor.csproj项目文件,该文件包含旨在修改Unity编辑器的脚本。

  5. We need to tell Visual Studio that your project supports the C# 7.3 language. 我们需要告诉Visual Studio您的项目支持C#7.3语言。 This doesn't mean that Unity will be able to compile all features of C# 7.3, but at least Visual Studio will not bitch about the features you are trying to use experimentally. 这并不意味着Unity将能够编译C#7.3的所有功能,但至少Visual Studio不会讨论您试图通过实验使用的功能。

    However if you edit a csproj file directly, Unity will automatically overwrite it at some point (Unity always auto-generate project and solution files). 但是,如果直接编辑csproj文件,Unity会在某些时候自动覆盖它(Unity总是自动生成项目和解决方案文件)。 So what you can do is to install a hook which is called when the project file is auto-generated, so you can open the project file yourself and add your customization to it (your customizations are not limited just to changing the language version: you could do more stuff, but you must understand what you are doing here). 所以你可以做的是安装一个在自动生成项目文件时调用的钩子,这样你就可以自己打开项目文件并为其添加自定义(你的自定义不仅限于更改语言版本:你可以做更多的事情,但你必须明白你在做什么)。

    For this purpose, place the following script inside the Editor folder: 为此,将以下脚本放在Editor文件夹中:

     #if ENABLE_VSTU using SyntaxTree.VisualStudio.Unity.Bridge; using System; using System.IO; using System.Linq; using System.Text; using System.Xml.Linq; using UnityEditor; using UnityEngine; [InitializeOnLoad] public class ProjectFilesGeneration { private class Utf8StringWriter : StringWriter { public override Encoding Encoding { get { return Encoding.UTF8; } } } static ProjectFilesGeneration() { ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) => { // Ignore projects you do not want to edit here: if (name.EndsWith("Editor.csproj", StringComparison.InvariantCultureIgnoreCase)) return content; Debug.Log($"CUSTOMIZING PROJECT FILE: '{name}'"); // Load csproj file: XNamespace ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003"); XDocument xml = XDocument.Parse(content); // Find all PropertyGroups with Condition defining a Configuration and a Platform: XElement[] nodes = xml.Descendants() .Where(child => child.Name.LocalName == "PropertyGroup" && (child.Attributes().FirstOrDefault(attr => attr.Name.LocalName == "Condition")?.Value.Contains("'$(Configuration)|$(Platform)'") ?? false) ) .ToArray(); // Add <LangVersion>7.3</LangVersion> to these PropertyGroups: foreach (XElement node in nodes) node.Add(new XElement(ns + "LangVersion", "7.3")); // Write to the csproj file: using (Utf8StringWriter str = new Utf8StringWriter()) { xml.Save(str); return str.ToString(); } }; } } #endif 

If you want to fully understand what has changed in your project file, just use any diff tool, to compare the previous version and the new version of the csproj file. 如果您想完全了解项目文件中的更改,只需使用任何diff工具,即可比较以前的版本和csproj文件的新版本。 The hack above basically does something similar to the link you've posted when changing the language version, except that it does that every time Unity auto-generates the project file. 上面的hack基本上做了类似于你在更改语言版本时发布的链接,除了每次Unity自动生成项目文件时都会这样做。 Microsoft also provides plenty of documentation on the definitions inside csproj files. Microsoft还提供了大量有关csproj文件内csproj文档。

The properties window not opening sounds like some issue with your installation of Visual Studio (which I can't give much advice on) 不打开的属性窗口听起来像安装Visual Studio的一些问题(我不能给出太多建议)

For the coding errors, it looks like you're trying to declare a method inside another method (which is quite odd): 对于编码错误,看起来你正试图在另一个方法中声明一个方法(这很奇怪):

void Update() {
    ...
    public void Method1();
}

I imagine that's not your intention. 这不是你的意图。 It's complaining because the version of C# you're compiling in doesn't support that (and local methods in C#7 can't be marked public in any case). 它抱怨,因为您编译的C#版本不支持(并且C#7中的本地方法在任何情况下都不能标记为public )。 It's also complaining because your method isn't abstract (or extern) but doesn't have a body, which C# doesn't allow. 它也抱怨,因为你的方法不是抽象的(或外部),但没有一个C#不允许的主体。 Is this what you meant? 这是你的意思吗?

void Update() {
    if (Input.GetKeyDown(KeyCode.R)) // Note no ; here!
    {
        GetComponent<Renderer>().material.color = Color.red;
    }
}

public void Method1() {
    // Some code
}

ALSO you have an erroneous ; 你也有错误; after your if () statement in line 15. That will cause the GetComponent<Renderer> line (line 17) to always be run, not just if the condition is true. 在第15行的if ()语句之后。这将导致GetComponent<Renderer>行(第17行) 始终运行,而不仅仅是条件为true。 Again, this is probably not what you intended I imagine. 同样,这可能不是你想象的那样。

It looks like you are using Unity (you mention it in your post). 看起来你正在使用Unity(你在帖子中提到它)。 I cannot tell by your gif because its like 4 pixels wide. 我不能用你的gif告诉它,因为它像4像素宽。

If you are indeed using Unity, you will be unable to use C# 7 features yet. 如果您确实使用Unity,那么您将无法使用C#7功能。 They've only just allowed experimental support for C# 6 (see here ). 他们只是为C#6提供了实验支持(见这里 )。

Unfortunately you are out of luck if you want to use C# 7 in Unity right now . 不幸的是,如果你现在想在Unity中使用C#7,那你就不走运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM