问题描述
我使用的是Unity3D v4.2.2f1,MonoDevelop的版本是捆绑在一起的 - v2.8.2。
I'm using Unity3D v4.2.2f1, and the version of MonoDevelop it comes bundled with - v2.8.2.
我看到有些情况下,代码将在Unity3D中编译,但不会在MonoDevelop中编译。例如,Unity3D接受方法参数的默认值,但结果是MonoDevelop中的默认参数设置不允许(CS0241)。
I see that there are cases where C# code will compile in Unity3D, but not in MonoDevelop. For instance, default values for method arguments are accepted by Unity3D, but result in "Default parameter settings are not permitted (CS0241)" in MonoDevelop.
发现在线是不要尝试从MonoDevelop编译。好 - 但我很想知道这种不匹配是如何产生的。我以为Unity3D和MonoDevelop都编译C#使用Mono?是Unity3D带有两个版本的Mono,一个为自己和一个MonoDevelop?
General advice I've found online is to not attempt to compile from MonoDevelop. Fine - but I'm keen to understand how this mismatch arises. I thought that both Unity3D and MonoDevelop compile C# using Mono? Is it that Unity3D comes with two versions of Mono, one for itself and one for MonoDevelop? If so, why?
在Unity中编译而不是在MonoDevelop中编译的方法示例(不在MonoDevelop中编译但是从Unity3D编译的位是 = false'赋给setNative):
Example first line of method that compiles in Unity but not in MonoDevelop (the bit that doesn't compile in MonoDevelop but compiles from Unity3D is the '=false' assignment to setNative):
public static void SetUnityVersion(string path, bool setNative=false) {
...implementation...
}
推荐答案
当在Windows上安装Unity时,MonoDevelop默认使用Microsoft .Net Framework。和Unity使用自定义构建的单声道,指出。
When installed with Unity on Windows, MonoDevelop uses the Microsoft .Net Framework by default. And Unity uses "a custom build of Mono", as pointed out here.
导致编译错误的语言功能称为,并在 C#4.0
中引入。现在,Unity目标 .Net Framework 3.5
生成的MonoDevelop项目不支持此功能。
The language feature that causes compiler error is called Optional parameters and was introduced in C# 4.0
. Now, MonoDevelop projects generated by Unity target .Net Framework 3.5
, which does not support this feature.
将生成的项目的目标框架更改为 .Net 4.0
(在Monodevelop中右键单击项目,然后 options-> general-> / code>),一切都应该编译良好。但是,如果你对Unity中的项目进行任何更改(例如添加一个脚本),MonoDevelop项目文件将被重新生成,你的更改将会丢失。
If you change target framework of generated projects to .Net 4.0
(in Monodevelop right-click on project, then options->general->Target Framework
), everything should compile fine. But if you make any change to the project in Unity (add a script for example), MonoDevelop project files will be regenerated and your change will be lost.
问题,您可以下载并安装版本,然后配置它作为MonoDevelop中的默认运行时( Tools-> Options-> .Net Runtimes-> Add
,找到已安装的Mono版本,然后按设为默认
)。 Monodevelop现在将使用 Mono
来编译您的代码,而Mono编译器不会投诉默认参数值。
To workaround this problem, you can download and install a release of Mono for Windows, then configure it as your default runtime in MonoDevelop (Tools->Options->.Net Runtimes->Add
, find the installed Mono release, then hit Set as Default
). Monodevelop will now use Mono
to compile your code, and Mono compiler will not complain about default parameter values.
应该节省你在Unity和MonoDevelop之间切换的一些麻烦,只是为了检查代码编译。使用Unity 4.5.3f3和MonoDevelop 4.0.1测试。
This should save you some trouble of switching between Unity and MonoDevelop just to check if the code compiles. Tested with Unity 4.5.3f3 and MonoDevelop 4.0.1.
这篇关于通过Unity3D和MonoDevelop编译C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!