Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by asdasdsads · Oct 18, 2017 at 12:57 AM · editor-scriptingimporting assets

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image asdasdsads · Oct 18, 2017 at 05:58 PM 0
Share

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!

avatar image MacDx asdasdsads · Oct 18, 2017 at 06:23 PM 1
Share

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).

avatar image Bunny83 MacDx · Oct 18, 2017 at 07:27 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can you set the vertexes of a Sprite's Physics Object programmatically when the texture is imported? 2 Answers

Queue method to be called after asset processor is done 0 Answers

Where is the Unity IAP package located? 1 Answer

Unity UI 4.6 - Programmatically adding events - EventTrigger.delegates is null 0 Answers

How to add a component on a GameObject in Custom Inspector 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges