- Home /
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)
Answer by Bunny83 · Oct 18, 2017 at 01:09 AM
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.
Hello, I don't want to reference an external DLL, I just want to copy a newer version into the assets, replacing the older one!
Like bunny said you just need to copy the files. So you could use the Copy method of the File class, providing a source file path and a destination path, that will copy your file verbatim you would just need to delete the old one first. Or you could open the new dll file copy it's content, open the older one and overwrite it with the content's of the newer one using a binary reader/writer. (Note that by opening files, I mean using the File class of course).
Right If you really want to copy it manually you can use Unity's "FileUtil" class. It has the CopyFileOrDirectory method. It takes either a file path that is relative to the "project folder" (note: "project", not "assets"), or an absolute path. This is quite handy since the source path would need to be an absolute path and the destination path should be a path relative inside your project.
Unfortunately the docs don't say anything about what happens if the destination file already exists.
I wouldn't recommend to delete the old DLL before copying the new if your DLL contains classes that are derived from $$anonymous$$onoBehaviour or ScriptableObject as this could break existing references.
As i said in my answer the easiest solution is to directly build your DLL into your project. So just change the target file in your DLL project in VisualStudio or $$anonymous$$onoDevelop.
Ins$$anonymous$$d of building directly to your Asset folder you can also add a post build event in Visual Studio (don't know about $$anonymous$$onoDevelop as i don't use this "crap" ^^). Just adding the following line to the post build events in your DLL project would do:
copy $(TargetPath) Your\Path\To\Your\Project\Assets\Code\Libs\
This will copy the DLL automatically to the specified location when you build it in VS.
Answer by Glurth · Oct 18, 2017 at 07:11 PM
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.