The AssemblyBuilder is Obsolete, what is the replacement API?

I see the AssemblyBuilder is Obsolete in unity 2023.1. It says the similar functionality can be achieved using the Roslyn compiler. So which Roslyn compiler API should I use. Is there any code example or documents in msdn?

bump

I have the same problem, it is deprecated but it is not clear how to use the Roslyn compiler from my scripts.

Bump.
Would be a great if anyone from the Unity team could take a look at this.

I dont think AssemblyBuilder is involved in Mono.Cecil or any IL Weaving

For example context on this, the code in both Mirage and Mirror: AssemblyBuilder is used to compile a list of .cs files into a .dll for unit tests. The tests then run IL Weaver on that .dll file.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using UnityEditor;
using UnityEngine;

public static class BuildScripts
{
    [MenuItem("Build/Generate Dll")]
    public static void Build()
    {
        List<SyntaxTree> trees = new();

        string[] scriptFolderPathArray = { "Assets/Codes" };

        foreach (var folderPath in scriptFolderPathArray)
        {
            string[] scriptsPath = Directory.GetFiles(folderPath, "*.cs", SearchOption.AllDirectories);

            foreach (string filePath in scriptsPath)
            {
                var originalText = File.ReadAllText(filePath);
                var syntaxTree = CSharpSyntaxTree.ParseText(originalText);
                trees.Add(syntaxTree);
            }
        }


        List<PortableExecutableReference> references = new();
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            if (assembly.IsDynamic)
            {
                continue;
            }

            //如果脚本中包含拓展函数,会导致错误 : error CS0121: The call is ambiguous between the following methods or properties: 'XXX' and 'XXX'
            if (assembly.FullName.Contains("Assembly-CSharp"))
            {
                continue;
            }

            if (!string.IsNullOrEmpty(assembly.Location))
            {
                references.Add(MetadataReference.CreateFromFile(assembly.Location));
            }
        }


        CSharpCompilationOptions options = new(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Release, allowUnsafe: true, warningLevel: 1);
        var compilation = CSharpCompilation.Create("Gen", trees, references, options);

        string output = "./Code";
        if (!Directory.Exists(output))
        {
            Directory.CreateDirectory(output);
        }

        var result = compilation.Emit(Path.Combine(output, "Generated.dll"));
        Debug.Log($"编译结果:{result.Success} | 输出路径:{Path.Combine(output, "Gen.dll")}");
        foreach (var resultDiagnostic in result.Diagnostics)
        {
            Debug.Log(resultDiagnostic.ToString());
        }
    }
}

9773328--1400841--upload_2024-4-16_13-48-41.png

1 Like

:slight_smile: