Problems implementing dynamic compilation using Microsoft CSharp in Unity

374 views Asked by At

Problems implementing dynamic compilation using Microsoft CSharp in Unity.

It works normally in the Unity editor, but the following error occurs when debugging after build.

It's a feature I really need, so I've been looking for it a lot, but I can't find much information. Any help would be greatly appreciated.

The code in question:

using UnityEngine;
using System;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Text;

public class test : MonoBehaviour
{
    private void Start()
    {
        var assembly = Compile(@"
            using UnityEngine;
            using System.Collections;

            public class RuntimeCompiled : MonoBehaviour
            {
                public static RuntimeCompiled AddYourselfTo(GameObject host)
                {
                    return host.AddComponent<RuntimeCompiled>();
                }

                void Start()
                {
                    Debug.Log(""TEST"");
                }
            }");

        var runtimeType = assembly.GetType("RuntimeCompiled");
        var method = runtimeType.GetMethod("AddYourselfTo");
        var del = (Func<GameObject, MonoBehaviour>)
            Delegate.CreateDelegate(
            typeof(Func<GameObject, MonoBehaviour>),
            method
            );

        // We ask the compiled method to add its component to this.gameObject
        var addedComponent = del.Invoke(gameObject);

        // The delegate pre-bakes the reflection, so repeated calls don't
        // cost us every time, as long as we keep re-using the delegate.
    }

    public static Assembly Compile(string source)
    {
        // Replace this Compiler.CSharpCodeProvider wth aeroson's version
        // if you're targeting non-Windows platforms:
        var provider = new CSharpCodeProvider();
        var param = new CompilerParameters();

        // Add ALL of the assembly references
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            string cash_d = assembly.Location.ToString();
            if (!cash_d.Contains("mscorlib.dll"))
            {
                param.ReferencedAssemblies.Add(assembly.Location);
            }
        }

        // Or, uncomment just the assemblies you need...

        // System namespace for common types like collections.
        //param.ReferencedAssemblies.Add("System.dll");

        // This contains methods from the Unity namespaces:
        //param.ReferencedAssemblies.Add("UnityEngines.dll");

        // This assembly contains runtime C# code from your Assets folders:
        // (If you're using editor scripts, they may be in another assembly)
        //param.ReferencedAssemblies.Add("CSharp.dll");


        // Generate a dll in memory
        param.GenerateExecutable = false;
        param.GenerateInMemory = true;

        // Compile the source
        var result = provider.CompileAssemblyFromSource(param, source);

        if (result.Errors.Count > 0)
        {
            var msg = new StringBuilder();
            foreach (CompilerError error in result.Errors)
            {
                msg.AppendFormat("Error ({0}): {1}\n",
                  error.ErrorNumber, error.ErrorText);
            }
            throw new Exception(msg.ToString());
        }

        // Return the assembly
        return result.CompiledAssembly;
    }
}

Below is the error content.

Multi - casting "[IP] -- [Port] 55354 [Flags] 2 [Guid] 344250057 [EditorId] 2120216054 [Version] 1048832 [Id] WindowsPlayer(2,DESKTOP---) [Debug] 1 [PackageName] WindowsPlayer [ProjectName] MRS_Main" to[225.0.0.222:54997]...
Starting managed debugger on port 56057
Initialize engine version: 2021.3.1f1(3b70a0754835)
[Subsystems] Discovering subsystems at path D:/ Documents / Unity / MRS_Main / build / MRS_Main_Data / UnitySubsystems
GfxDevice: creating device client; threaded = 1; jobified = 1
Direct3D:
Version: Direct3D 11.0[level 11.1]
    Renderer: NVIDIA GeForce RTX 3050 Laptop GPU(ID= 0x25a2)
    Vendor: NVIDIA
  VRAM:     3991 MB
  Driver:   30.0.14.9649
< RI > Initializing input.
< RI > Input initialized.
  < RI > Initialized touch support.
    UnloadTime: 0.779800 ms
    NotSupportedException: Microsoft.CSharp.CSharpCodeProvider::.ctor
      at Microsoft.CSharp.CSharpCodeProvider..ctor ()[0x00000] in <00000000000000000000000000000000>:0 

      at test.Compile (System.String source)[0x00001] in D:\Documents\Unity\MRS_Main\Assets\Scripts\test.cs:48 

      at test.Start ()[0x00006] in D:\Documents\Unity\MRS_Main\Assets\Scripts\test.cs:12 
0

There are 0 answers