Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Package the script as a dll

Discussion in 'Scripting' started by z_yq, May 27, 2022.

  1. z_yq

    z_yq

    Joined:
    May 3, 2017
    Posts:
    15
    I wrote a tool to package the script as a dll, but when the script has a generic or declares a Text component, the output is wrong, I want to know how to fix it, it's been bothering me for two days

    public int Exec(
    bool isUseUnityEngineDll,
    bool isUseUnityEditorDll,
    bool isUseUnityEngineUIDll,
    bool isDebug,
    string dllName,
    string[] srcNames
    )
    {
    string args = "";

    if (isUseUnityEngineDll)
    {
    var path = "UnityEngine.dll";
    var files = Directory.GetFiles(mEditorPath, path, SearchOption.AllDirectories);
    args += " -r:\"" + files[0] + "\"";
    }
    if (isUseUnityEditorDll)
    {
    var path = "UnityEditor.dll";
    var files = Directory.GetFiles(mEditorPath, path, SearchOption.AllDirectories);
    args += " -r:\"" + files[0] + "\"";
    }

    if (isUseUnityEngineUIDll)
    {
    foreach (var path in GetUnityExtensionDllPaths())
    {
    var dllFileName = Path.GetFileName(path);
    var pathStr = Path.GetFullPath(path);
    //Debug.Log("pathStr:" + pathStr);
    args += " -r:\"" + pathStr + "\"";
    }
    }
    args += " -target:library -out:" + dllName;

    if (isDebug)
    {
    args += " -debug";
    }

    foreach (var srcName in srcNames)
    {
    args += " " + srcName;
    }
    Debug.Log("args:" + args);
    return base.Exec(args);
    }

    public int Exec(string arg)
    {
    int exitCode = -1;

    try
    {
    using (var process = new Process())
    {
    process.StartInfo.FileName = mExecFullPath;
    process.StartInfo.Arguments = arg;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.Start();

    mOutput = process.StandardOutput.ReadToEnd();
    //UnityEngine.Debug.Log("Error:" + process.StandardError.ReadToEnd());
    process.WaitForExit();
    exitCode = process.ExitCode;

    process.Close();
    }
    }
    catch (Exception e)
    {
    UnityEngine.Debug.Log(e.Message);
    }
    return exitCode;
    : upload_2022-5-27_9-40-1.png
    When I compile this script to package the dll, I get an error
    upload_2022-5-27_9-41-55.png
     
  2. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    150
    Hi!
    1. First of all use code tags when posting code. it will be much easier to read code;
    2. it would be much more helpful if you post the whole compiler output so it will be clear what the error is instead of guessing it;
    3. I can't say about generic without an error message, but I am almost sure what's wrong with Text component.

    UnityEngine.UI.Text is placed in com.unity.ugui package. scripts in this package are compiled to UnityEngine.UI.dll. so you need to reference this assembly to make it work. Currently compiled assemblies are located in Library/ScriptAssemblies folder.

    Also, take a look at https://docs.unity3d.com/ScriptReference/Compilation.AssemblyBuilder.html . This class provides the functionality to compile any code with Unity compilation pipeline and save dll anywhere you want. also, it has easier control over unity DLLs dependencies
     
  3. z_yq

    z_yq

    Joined:
    May 3, 2017
    Posts:
    15
    Sorry,I found the error output : upload_2022-6-1_15-4-4.png
    What need I do?
     
  4. z_yq

    z_yq

    Joined:
    May 3, 2017
    Posts:
    15
    I have solved it, please refer to the dll of the engine under the following path
    EditorApplication.applicationContentsPath + "/Managed/UnityEngine"; Thanks!