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.
- If you use Mac OSX, create a new solution in Xamarin Studio (or MonoDevelop); if you are in Windows, use Visuals studio:
- Create a library project:
- 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):
- Add or create the MonoBehaviour script to the library project. In this example, I just created a new script, which is pretty simple:
- 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.
- Build the assembly, and find the generated dll in the bin folder;
- Copy the dll into the Assets/ folder of our Unity Project:
Note: You don’t have to put this dll into /Plugins folder
- Now expand the DLL assembly in Unity3D, you will find an icon for your script:
- Drag the MonoBehaviour to a game object, and run the app!
Yeal, you get what you want now! Happy clean coding!