Search Unity

Compiling c# Scripts to .Dll files?

Discussion in 'Scripting' started by Studio_Akiba, Apr 14, 2015.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,422
    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.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    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.
     
    Menyus777, jayvatar, Novack and 25 others like this.
  3. haxpor

    haxpor

    Joined:
    Oct 15, 2012
    Posts:
    11
    Lovely, works like a charm!
     
  4. philmzo

    philmzo

    Joined:
    Dec 2, 2015
    Posts:
    7
    Thanks!
     
  5. bwheatley

    bwheatley

    Joined:
    Jun 23, 2012
    Posts:
    45
    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.
     
  6. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    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 ;) so as said, just curious.
     
  7. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,422
    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.
     
    honor0102 and Kona like this.
  8. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    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.
     
  9. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    Allright, thanks for the replies, sounds interesting enough to me to take a deeper look at. ^^
     
  10. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Any net code is trivial to decompile with freely available tools, whether it's in an executable or dll.
     
  11. GunLengend

    GunLengend

    Joined:
    Sep 24, 2014
    Posts:
    54
    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.
     
  12. Sadaan

    Sadaan

    Joined:
    Dec 10, 2014
    Posts:
    15
    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?
     
  13. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    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/
     
  14. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    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 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?
     
    TaleOf4Gamers likes this.
  15. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    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.
     
  16. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    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:
    download.png

    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.
     
    dogzerx2 likes this.
  17. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    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)
     
    Last edited: Mar 16, 2018
  18. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    What kind of errors? You need to add some more information.
     
    dogzerx2 likes this.
  19. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    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:
     
  20. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    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?
     
    dogzerx2 likes this.
  21. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    that explains so much

    I wanted to compile all the files in my Unity project into dll, I've been told if they're in th form of dll files, they don't add too much time when compiling new scripts
     
  22. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Let's take a step back and focus on this statement:

    To be pedantic, you don't convert scripts to dll. It's still C#. You move scripts to dll. Having the same script in both your Unity project and DLL project is a recipe for disaster.

    The tricky part is what if your code needs access to the Unity API? You're going to have to add references to the Unity DLLs in your solution.

    As for how to do that... well, read this. Pay particular attention to the "Step by Step Guide" section.
     
    dogzerx2 likes this.
  23. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    I'll start there! Thank you so much! =)
     
  24. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Well... yes and no. C# is insanely fast when it comes to parsing code (compared to C++, for example). You'd need a lot of code for the hassle of DLL management to be worth it.

    DLLs are for:
    - Plugins
    - Proprietary code you don't want to distribute (which means running your DLLs through an obfuscator, adding to the hassle of DLL management)
    - Code that doesn't change much (like libraries)
    - Code that's meant to be used between multiple projects (which is basically a plugin, so I repeat myself)
     
    8bitRick and dogzerx2 like this.
  25. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I will add this, in case it's useful (murky on what the above poster seeks) -- you can create assembly definition files, which can help compilation times: https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html

    If you set it up properly, and you have a lot of scripts, it could help. I haven't used them much, but if the speed up was all you were searching for, that might be for you, depending how many scripts you have, etc..
     
    IncomeCountry and dogzerx2 like this.
  26. krupps

    krupps

    Joined:
    Oct 17, 2017
    Posts:
    159
    It's not that easy. You can apply Obfuscators, so it's a pain to read like piecing a shredded document back together. You can also wrap your dll code into a C++ wrapper.

    You can also you a 3rd party packer.

    Your freely available tools won't work and you'll have to be an expert at IDA. Which eliminates 90% of the people who would be shady enough to steal code.

    It's also just as easy to decompile games and search for your own code blocks and sue.
     
  27. stechmann

    stechmann

    Joined:
    Feb 24, 2012
    Posts:
    34
    @lordofduct -- thanks for the detailled instructions. I followed them step by step, but MonoDevelop gives me this error when trying to build the DLL:

    "Could not load file or assembly 'IKVM.Reflection, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies"

    Unity 2018.2

    Any ideas on how to fix this? Thanks!!
     
    VictoriaZamora likes this.
  28. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    IKVM.Reflection is a 3rd party assembly. If your code references another assembly you'll need to 1) make sure it's in your references and 2) make sure any of its dependencies are also referenced.

    If you use nuget (which IKVM.Reflection is in), it should resolve both of these. If you had used nuget already, but before using what I posted (especially where I said remove all references). You'll need to re-import those references. Note that my post dealt with creating a basic library, and the removal of those dependencies was to give you the barest of minimum for unity support. It was posted several years ago when Unity only supported an older version of mono/.Net.

    It also means those same dll's/assemblies will also need to be placed into your Unity project.
     
    VictoriaZamora likes this.
  29. VictoriaZamora

    VictoriaZamora

    Joined:
    Aug 13, 2020
    Posts:
    1
    this doesn't even look like the 2019 versions anymore, how do you check this? Where in the unity manual can I read this topic?
     
  30. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    For starters it depends on how you have Unity configured. If you go to Edit->Project Settings, then under 'Player' you find the 'Other Settings' section and you'll see this:
    upload_2020-8-13_19-16-18.png

    In this 2019.3.7f1 there's only 2 options 4.x and 2.0

    4.x is safe for 4.6 and even 4.7.2 if you're on the very latest (2019.3+ if I recall correctly)
    Standard 2.0 is safe for... Standard 2.0

    And if you see a 3.5 or 3.x in there for whatever reason (say you're on an older version), that one is safe for 3.5.

    Here is documentation on it:
    https://docs.unity3d.com/2019.3/Documentation/Manual/dotnetProfileSupport.html
     
    VictoriaZamora likes this.