简体   繁体   English

Unity Visual Studio C#版本同步

[英]Unity Visual Studio C# version synchronization

I just started to use delegates and events in my Unity game to update labels when appropriate, instead of updating the labels every frame, although most of the time no changes occur. 我刚开始在我的Unity游戏中使用委托和事件来在适当的时候更新标签,而不是每帧更新标签,尽管大多数时候没有发生变化。

To make sure, that a static event call of 为了确保,这是一个静态事件调用

public static event OnSomething onSomething;

actually has listeners, I use 实际上有听众,我用

if (onSomething!= null) {
    onSomething();
}

It's just a convenience problem, but Visual Studio 14 (2015) says, that this can be simplified to 这只是一个方便的问题,但Visual Studio 14(2015)说,这可以简化为

onSomething?.Invoke();

which I also like more, because I'm familiar with nullables and would like to use them, but if I do change the code to the suggestion, Unity says 我也更喜欢,因为我熟悉nullables并想使用它们,但如果我确实将代码更改为建议,Unity表示

error CS1644: Feature `null propagating operator' cannot be used, because it is not part of the C# 4.0 language specification.

So is there a way to make a Unity project use v4.0 of C#, or can I tell Visual Studio, that the project uses an older version of C#, so it stops these suggestions? 那么有没有办法让Unity项目使用C#的v4.0,或者我可以告诉Visual Studio,该项目使用旧版本的C#,所以它会停止这些建议吗?

Edit: Little clarification: I don't neccessarily want to use any specific C# version. 编辑:很少澄清:我不一定要使用任何特定的C#版本。 I want to use the newest version possible to use these "cool new features", as long as there is no major reason not to use the newest version. 我想使用最新版本来使用这些“酷炫的新功能”,只要没有主要原因不使用最新版本。

I don't neccessarily want to use any specific C# version. 我不一定要使用任何特定的C#版本。 I want to use the newest version possible to use these "cool new features", as long as there is no major reason not to use the newest version. 我想使用最新版本来使用这些“酷炫的新功能”,只要没有主要原因不使用最新版本。

Unity does not officially support anything above .NET 3 . Unity 没有 正式支持.NET 3以上的任何内容。 Although, they are currently working on this. 虽然,他们目前正在研究这个问题。 They want to support .NET 4 first then other .NET version. 他们希望首先支持.NET 4,然后支持其他.NET版本。

Can you use newest C# in Unity? 你能在Unity中使用最新的C#吗?

Yes! 是!

There is a project dedicated to this on butbucket called "Unity C# 5.0 and 6.0 Integration" . butbucket上有一个专门用于此项目的项目,称为“Unity C#5.0和6.0集成” You can get it here . 你可以在这里得到它。 It lets you do exactly this and the instructions required to use this is posted on that site. 它允许您完成此操作,并在该站点上发布使用它所需的说明。

This works on iOS, Android, Windows and MacOS but I wouldn't use this to release my game simply because it's not officially supported. 这适用于iOS,Android,Windows和MacOS,但我不会使用它来发布我的游戏只是因为它没有得到官方支持。


As for your attemp to use 至于你的尝试使用

onSomething?.Invoke();

I suggest you don't use that in your Unity at-all. 我建议你不要在你的Unity中使用它。 You will run into many problems if you form a habit of using that. 如果你养成使用它的习惯,你会遇到很多问题。

The reason for this is because Unity uses custom equality operator for Components and GameObjects . 这样做的原因是Unity使用了ComponentsGameObjects 自定义相等运算符 It won't work if you try to do this on a Component or GameObject . 如果您尝试在ComponentGameObject上执行此操作,它将无法工作。 You will simply create so many problems for your self. 你只会为自己创造很多问题。 Here is an example of one and here is another reason to stay away from Null propagation when working in Unity. 这里是一个的例子, 这里的另一个原因是在Unity工作时要远离零传播了。

EDIT : 编辑

Unity 2017 now supports 4.6 which will allow this feature to work but I will still suggest you test and experiment a lot with this to make sure there is no big problem such as this during run-time before releasing your game with it. 2017年团结现在支持 4.6,这将使此功能工作,但我还是会建议你测试和试验了很多本,以确保不存在大的问题,如期间它释放你的比赛前的运行时间。

Unity has recently released a preview version of .NET 4.6 that can be enabled by going to: Edit > Project Settings > Player and setting Scripting Runtime Version as 'Experimental (.NET 4.6 Equivalent)'. Unity最近发布了.NET 4.6的预览版本,可以通过以下方式启用:编辑>项目设置>播放器并将脚本运行时版本设置为'实验(.NET 4.6等效)'。

You'll need at least Unity 2017.1 to use this feature however. 但是,您至少需要Unity 2017.1才能使用此功能。

I've included a link with some more information: https://docs.unity3d.com/Manual/ScriptingRuntimeUpgrade.html 我已经添加了一些链接,其中包含更多信息: https//docs.unity3d.com/Manual/ScriptingRuntimeUpgrade.html

I would like to point out that C#4.0 has nothing to do with .NET 3.5, 4.0, 4.5 etc. Its the version of Visual Studio and the compiler. 我想指出C#4.0与.NET 3.5,4.0,4.5等无关。它是Visual Studio和编译器的版本。 The reason your getting the recommendation from Visual Studio is because your using 2015 or newer. 您从Visual Studio获得建议的原因是您使用2015或更新版本。

I myself got sick of this suggestion and found a way to disable it, unfortunately it will disable it on ALL projects solutions that you ever work with. 我自己厌倦了这个建议并找到了一种方法来禁用它,不幸的是它会在你曾经使用过的所有项目解决方案中禁用它。 Note this is specific to Visual Studio 2015 and I have Enterprise Edition this has NOT been tested on Community Edition so not sure if it works for that one or if the location of the files may be in a different spot as well but here is how I disabled it: 请注意,这是特定于Visual Studio 2015,我有企业版,尚未在Community Edition上进行测试,因此不确定它是否适用于该版本,或者文件的位置可能位于不同的位置,但这是我的方式禁用它:

Visual Studio 2015 uses a rule set for the code analysis, now you can change these rules on a project per project basis by right clicking your project choosing properties then go to the Code Analysis Tab at the bottom, but Unity every time you add a C# script to your game will overwrite any changes you make to the project file. Visual Studio 2015使用规则集进行代码分析,现在您可以通过右键单击项目选择属性来更改每个项目的项目规则,然后转到底部的代码分析选项卡,但每次添加C#时Unity都会游戏脚本将覆盖您对项目文件所做的任何更改。 So what I did was as follows 所以我做的如下

in the folder: 在文件夹中:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets

is all the rule sets that visual studio 2015 uses for code analysis, if you edit the file: 是Visual Studio 2015用于代码分析的所有规则集,如果您编辑文件:

MinimumRecommendedRules.ruleset

and add: 并添加:

<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
    <Rule Id="IDE1005" Action="None" />
</Rules>

to the end just before the closing </RuleSet> it will disable the suggestion globally on all projects. 在结束</RuleSet> ,它将在所有项目上全局禁用建议。

Hope this helps someone else who is also sick of the suggestion in Unity projects 希望这可以帮助那些也厌倦了Unity项目中的建议的人

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

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