Compiling c# Scripts to .Dll files?

I am looking to compile a specific script (for an asset) to a DLL, I have had a lot of trouble finding any step-by-steps on how to do this easily in Monodevelop.

I was hoping someone with experience in these matters would be able to point me in the right direction, or write a step-by-step that’s easy to follow.

Using either Visual Studio, or MonoDevelop.

Create a new ‘class library’ project (not a unity a project, a VS/Mono project in the IDE).

Go to the references and remove all references, except for:
System
System.Core
System.Xml

Now select to add references. Select browse. Go to the installation folder of unity and navigate to:
Unity Install Dir\Editor\Data\Managed

In my case it’s:

C:\Program Files (x86)\Unity\Editor\Data\Managed

Select the UnityEngine.dll in there (if you’re creating an ‘editor’ dll, also select the UnityEditor.dll, note an ‘editor’ dll MUST be distinct from a runtime dll).

Now, right click the project, select properties, and select ‘Application’. In there you’ll see ‘Target Framework’.
If you have Visual Studio w/ Unity VS Tools installed, or you have MonoDevelop, you should see an option for ‘Unity 3.5 .net full base class libraries’. Select that.

If you don’t, go with .Net Framework 2.0. You may be able to get away with 3.0, or 3.5… I’m not sure though.

Now build.

NOTE - if you need the new Unity UI in your separate dll, you’ll need to add a reference to UnityEngine.UI. This can be found by navigating to the folder:
Unity Install Dir\Editor\Data\UnityExtensions\Unity\GUISystem

In there you’ll find a folder for the version (mine is 4.6.1), and in there is the dll.

28 Likes

Lovely, works like a charm!

Thanks!

Just wanted to say thanks this works even with VS 2017 RC. You just have to make sure you have .Net Desktop development component installed. Thanks for the walk through.

Sorry, I don’t want to hijack the thread but I’m just curious… what would be the reason to compile the code into .dll files?
I’m uneducated and don’t know any better :wink: so as said, just curious.

A DLL is a compiled and relatively well-protected version of a C# class or Namespace, it allows you to help prevent editing or stealing of code, it isn’t a perfect system but a DLL just helps you to protect your code.

We use DLL’s for some of our more elaborate/secretive code that we don’t want others to replicate.

2 Likes

It can still be relatively easily decompiled. To me, one of the main benefits is that it reduces project build time in Unity because it’s already compiled so Unity doesn’t have to compile it.

4 Likes

Allright, thanks for the replies, sounds interesting enough to me to take a deeper look at. ^^

Any net code is trivial to decompile with freely available tools, whether it’s in an executable or dll.

Sorry for dumped this thread but with a little hard work add, u maybe make it harder to de-compile by using Obfuscator or custom pack/unpack dll.

Hi, am i supposed to look into runtime dlls if my end result is to have a dll that has to update according to the user input while program runs?

You could use a language such as Lua which has much better support for runtime programming as opposed to C# which needs to be pre-compiled.

Moonsharp is probably one of the most common interpreters for Lua within Unity:
http://www.moonsharp.org/

Unsure what exactly you’re asking for.

Does your code change on the fly? Is custom code being generated as the game plays? Like the user gets to type in code of their own that then gets ran?

If so @TaleOf4Gamers_1 is right that an interpreted language like Lua might be best for that.

But if you need branching code that is predefined, but just changes what code is ran based on user input… well, that’s just code. If it’s complex branching, there’s multiple design patterns to assist with that.

But as is… this question is far to vague for us to say anything on the matter. What’s the problem you’re attempting to solve?

1 Like

When I build I get this error:

"The type or namespace ‘Task’ does not exist in the namespace


I want to convert some scripts to dll but I’m not sure how. I have no idea what the heck I’m doing.
I’m following the steps blind and I got stuck with that error.

Do I need using System.Threading.Tasks; in the project?

This is what’s on the script file by default.

Is this in Unity, or is this in your own visual studio project?

The main issue is that System.Threading.Tasks is a .Net 4.0 feature, and if you’re not targeting .Net 4.0 or later, you won’t have access to it.

In unity, the default is .Net 3.5, you can target .Net 4.6 experimental in the latest versions. This can be done through Edit->Project Settings-Player, and you’ll see in the inspector panel the player settings, under Other Settings you’ll find api compatibility level.

If this is your own visual studio project that you’re compiling into a dll (like the thread’s topic), you need to make sure you are targeting the appropriate .Net as well. Right click the project, select properties, and in the window that appears under Application you’ll find the target framework:

Note, if you want this to work in Unity. You can’t target above 4.6. And if you target above 3.5, you need to make sure that you set your project settings for your game to target experimental 4.6 as well.

1 Like

I feel dumber every micro-second.

Unity Project!

*The Frameworks: System, System.Core and System.Data are selected, and everything else deselected.
*The UnityEngine and UnityEditor references has been added.
*Experimental (.NET 4.6 Equivalent) has been set as scripting runtime version
*Target framework is: .NET Framework 3.5

So am I supposed to create a Class Library project, then copy my unity C# files to said project? And then build?

(because that’s what I did, but after copying all my scripts, I got 101 errors that I don’t know how to fix)

What kind of errors? You need to add some more information.

1 Like

Hmm, hard to explain. I don’t even know if I’m on the right track

For example: The type namespace name ‘SceneManagement’ does not exist in the namespace ‘UnityEngine’ (are you missing an assembly reference?)

But there are 101 errors … I think I’m way off, doing everything wrong.

I took a screenshot below:

Not necessarily.

That’s just what this thread is about. This thread is about compiling a custom dll to then import into a project (you import the dll, not all the script files).

If that’s what you want, that’s what this thread covers. If that’s NOT what you want… you’re in the wrong thread.

If you created a project and then copied those *.cs files into your unity project. You’re going about all this the wrong way.

I think you may have tripped into the wrong thread.

So… instead, what are you trying to accomplish?

1 Like