[英]How to use “UnityEngine” in runtime-compiled C# code?
I am trying to compile code at runtime using C#, Unity and this tutorial .我正在尝试使用 C#、Unity 和本教程在运行时编译代码。 I've got everything running, but want to be able to use UnityEngine methods like
Debug.Log();
我已经让一切都在运行,但希望能够使用 UnityEngine 方法,如
Debug.Log();
in the pseudo-code.在伪代码中。 In order to do so, I wrote
using UnityEngine;
为此,我
using UnityEngine;
in it, but got the following error:在其中,但收到以下错误:
The type or namespace name "UnityEngine" could not be found.
找不到类型或命名空间名称“UnityEngine”。 Are you missing an assembly reference?
您是否缺少程序集参考?
How do I solve this?我该如何解决这个问题?
I have tried adding a reference of UnityEngine to CompilerParameters.ReferencedAssemblies
like this:我尝试将 UnityEngine 的引用添加到
CompilerParameters.ReferencedAssemblies
,如下所示:
//parameters is of type CompilerParameters
parameters.ReferencedAssemblies.Add("UnityEngine.dll");
but that gives me the following error:但这给了我以下错误:
Metadata file UnityEngine.dll could not be found.
找不到元数据文件 UnityEngine.dll。
This is the most relevant part of my code, but you won't be able to reproduce it like that.这是我的代码中最相关的部分,但您将无法像那样重现它。 Instead, get the code from the above mentioned tutorial.
相反,从上述教程中获取代码。
public void Compile()
{
string code = @"
using UnityEngine;
namespace First
{
public class Program
{
public static void Main()
{
" + "Debug.Log(\"Test!\");" + @"
}
}
}
";
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add("UnityEngine.dll");
}
Since I am inexperienced with C#, and only use it with Unity, I don't know what a.dll file is and am clueless about where to start when fixing this.由于我对 C# 缺乏经验,并且仅将其与 Unity 一起使用,因此我不知道 a.dll 文件是什么,并且在修复此问题时不知道从哪里开始。 I am thankful for any kind of help!
我感谢任何形式的帮助!
You are supposed to put the path of the UnityEngine.dll there, not just UnityEngine.dll
, unless it exists in the working directory, which is highly unlikely.您应该将 UnityEngine.dll 的路径放在那里,而不仅仅是
UnityEngine.dll
,除非它存在于工作目录中,这是极不可能的。
According to this answer , you can easily find UnityEngine.dll (and other dlls as well) in your file system.根据这个答案,您可以在文件系统中轻松找到 UnityEngine.dll(以及其他 dll)。 It is under
Editor\Data\Managed
, so you should write:它在
Editor\Data\Managed
下,所以你应该写:
parameters.ReferencedAssemblies.Add(@"C:\\path\to\your\unity\installation\Editor\Data\Managed\UnityEngine.dll");
I tried some of the open source solutions, but couldn't find a solution which worked on all 3 platforms.我尝试了一些开源解决方案,但找不到适用于所有 3 个平台的解决方案。 Finally I bought this asset, which works without problems on Windows, Mac and Linux: https://assetstore.unity.com/packages/tools/integration/dynamic-c-82084 The documentation and support is very good.
最后我买了这个资产,它在 Windows、Mac 和 Linux 上没有问题: https://assetstore.unity.com/packages/tools/integration For example I had a problem that I couldn't compile a class which uses the Unity Screen class, and the support told me that I had to include the UnityEngine.CoreModule.dll in the settings page of the asset, which solved the problem.
例如,我有一个问题,我无法编译使用 Unity 屏幕 class 的 class,支持人员告诉我必须包含 UnityEngine.CoreModule.Z06416233FE5EC4C5933122E4AB248 资产的设置页面。
PS: I don't get money for this recommendation, I'm just a happy user of the asset. PS:我没有为这个推荐赚钱,我只是资产的快乐用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.