Packaging Unity3D MonoBehaviour script into DLL

09 Dec

You might have dozens or even hundreds of MonoBehaviour based scripts in your typical Unity3D projects.

For some reasons, either you want to protect your source code from direct access by others, or you might want to put stable helper utility functions in a separate assembly, it is likely that you would like to package all these scripts into a .net assembly in the form of a DLL.

This post focuses on the detailed procedures to implement such functions.

  1. If you use Mac OSX,  create a new solution in Xamarin Studio (or MonoDevelop); if you are in Windows, use Visuals studio:


  2. Create a library project:


  3. Add references of UnityEngine.dll and/or UnityEditor.dll to the project, you can do so by browsing into the folder (typically, depending on your specific Unity3D installation directory):
    Mac:              /Applications/Unity/
    Windows:      C:\Program Files\Unity\Editor\Data\Managed
  4. Add or create the MonoBehaviour script to the library project. In this example, I just created a new script, which is pretty simple:

  5. Now, here comes the most important step, and most developers fail to be successful in packaging the scripts into DLL, simply due to missing of this step: change the .net version to .Net 3.5 in project settings.
    On OSX (Xamarin Studio): Click menu Project > [YourProject] Option, select Build > General on the left panel, and then select Mono/.Net 3.5, as shown below:


    On Windows (Visual Studio), select the project, press ALT + Enter, and in the popup dialog, change the .Net version to .Net 3.5.

  6. Build the assembly, and find the generated dll in the bin folder;
  7. Copy the dll into the Assets/ folder of our Unity Project:
    Note: You don’t have to put this dll into /Plugins folder
  8. Now expand the DLL assembly in Unity3D, you will find an icon for your script:


  9. Drag the MonoBehaviour to a game object, and run the app!


Yeal, you get what you want now! Happy clean coding!


Posted by on December 9, 2015 in Programming, Unity 3d


Tags: , , , , , ,

2 responses to “Packaging Unity3D MonoBehaviour script into DLL

  1. Ashraf

    April 18, 2017 at 9:27 pm

    Excellent article! Your clear description makes all those mad speculations on Unity forums sound like a bad dream.

  2. isgoed

    August 19, 2017 at 12:32 am

    Can you also load a monobehaviour-dll at runtime?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: