简体   繁体   English

如何在运行时编译的 C# 代码中使用“UnityEngine”?

[英]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.

相关问题 如何在主C#代码和运行时编译代码中创建和使用相同的类? - How to create and use same class in main C# code and runtime compiled code? 如何在运行时编译和运行C#代码,其中编译的代码可以访问程序的其余部分 - How to compile and run C# code at runtime where the compiled code can access the rest of the program 将类类型传递给运行时编译的代码C# - passing class types to runtime compiled code C# C# Roslyn 编译 - 在动态编译的代码中使用 Nuget 包 - 编译但在运行时抛出异常(无法加载文件或程序集) - C# Roslyn Compile - Use Nuget Package within dynamically compiled Code - compiles but throws an Exception at runtime (Could not load file or assembly) 编辑源代码并在C#中的运行时使用它 - Edit Source code and use it at runtime in c# 在运行时编译C#数组并在代码中使用它? - Compile a C# Array at runtime and use it in code? 为什么当我使用 !!= C# 时代码会被编译 - Why does the code get compiled when I use !!= C# C#如何添加代码以在运行时执行 - C# How to add a code to execute at Runtime 如何将以下代码转换为C#中的编译表达式? - How can I convert the following code to a Compiled Expression in C#? 如何将通用列表传递给动态编译的C#代码? - How to pass a generic list to a dynamically compiled C# code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM