Import a DLL with C# code in editor

Hello,

How does one import a DLL in unity in the editor through C# code? I want to import 2 DLL’s when I click on a menu item. I’m working on the DLL’s a lot, and don’t want to keep having to manually import them

This is the code that will ask for the locations of the DLL’s, but how do I actually import them in Unity? Tried some Googling, but alas, no luck. AssetDatabase.ImportAsset does not seem to do much at all.

I want to import the 2 DLL’s to the folder: [Assets]/Code/Libs/.

public static class ImportDLLsMenu
{
    private const string Rasta = "RastaCore";
    private const string GameCommon = "GameCommon";

    [MenuItem("Import/Import DLLs")]
    private static void ImportDlls()
    {

        AssetDatabase.ImportAsset(GetRastaCoreDllPath());
        AssetDatabase.ImportAsset(GetGameCommonDllPath());
    }

    [MenuItem("Import/Reset DLL locations")]
    private static void ResetLocations()
    {
         // todo
    }

    private static string GetRastaCoreDllPath() { return GetPathOrAskAndSave(Rasta); }
    private static string GetGameCommonDllPath() { return GetPathOrAskAndSave(GameCommon); }

    private static string GetPathOrAskAndSave(string pathName){
        var key = string.Format("Dll_Paths_{0}", pathName);
        var path = EditorPrefs.GetString(key);

        if (string.IsNullOrEmpty(path))
        {
            path = EditorUtility.OpenFilePanel(string.Format("Select {0}.dll", pathName), "", "dll");
            EditorPrefs.SetString(key, path);
        }

        return path;
    }
}

Thanks in advance!

Jorik

P.S. mods, the error of the CAPTCHA seems to be outdated: https://imgur.com/a/s9reh (happend after I successfully clicked on the checkbox)

Uhm you just need to copy the DLL into your assets folder. Unity’s can’t “reference” external DLLs. All assets that should be used in a Unity project (which do not belong to built-in stuff like UnityEngine.dll and others) have to be inside the Assets folder or a subfolder.

If you have an external VisualStudio / MonoDevelop solution you can specify there a target path that is inside your Unity project assets folder. So when you build your DLL it’s already inside the project and you don’t have to do anything. when you switch back to Unity, Unity will recognise the changed file.

This does NOT directly answer the question about doing this in c# code, but still may prove useful to you:

I use a simple (manually created) batch script in the DLL project’s configuration (build events->post build event command line), to automatically copy the newly compiled DLL’s into the asset folders of projects that depend on it. The batch script is no more than a few DOS “copy” commands.

This way, as soon as I compile a new version of DLL, all projects using it are automatically updated with it.