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):
    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 import 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!

Leave a comment

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


Tags: , , , , , ,

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: