Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Building a DLL library in monodevelop

Discussion in 'Scripting' started by ColossalDuck, Dec 25, 2010.

  1. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    How do I do it. I believe I am very close, but I still haven't got that cigar.

    Below is what I have.


    What I am wondering is why I can't build it. Am I missing any assembly references?
    Is the code wrong? Any help would be greatly appreciated.

    I started by making a new solution in monodevelop, and using the Library defaults.
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    dll
    Code (csharp):
    1. using System;
    2.  
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7. namespace dllTest
    8.  
    9. {
    10.  
    11.     public class GenericCode
    12.  
    13.     {
    14.  
    15.         public static int a = 0;
    16.  
    17.        
    18.  
    19. //      Generate a random number between min and max, including min and max
    20.  
    21.         public void GenRandom (int min, int max)
    22.  
    23.         {
    24.  
    25.             System.Random random = new System.Random();
    26.  
    27.             a = (random.Next(min, max +1));
    28.  
    29.         }
    30.  
    31.     }
    32.  
    33. }
    unity
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. using dllTest;
    6.  
    7. public class test : MonoBehaviour {
    8.  
    9.     GenericCode gc = new GenericCode();
    10.    
    11.     void OnGUI () {
    12.         if(GUILayout.Button("gen random"))
    13.         {
    14.             gc.GenRandom(0,10);
    15.             int a = GenericCode.a;
    16.             Debug.Log(a);
    17.         }
    18.     }
    19. }
     
  3. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    I still can't get MonoDevelop to build the dll. When I click build, nothing is outputted.
     
  4. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    ... Once I delete the Unity dlls, it builds fine. But then I can't use Unity commands. This seems dumb. I am probably making a very simple mistake.
     
  5. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Actually, its just when UnityEngine.dll is added.
     
  6. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Meh, I can do it in Visual Studio so its fine. But if anyone figures out the problem, I am all ears!
     
  7. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    odd, should work in Mono also, works fine for me.
     
  8. rahuxx

    rahuxx

    Joined:
    May 8, 2009
    Posts:
    537
    can some one provide a detail tutorial on it. text or video both will work.
     
  9. ClandestineMan

    ClandestineMan

    Joined:
    Jul 16, 2010
    Posts:
    17
    I would be interested in how to build the dll as well. what is the command line for it?
     
  10. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    it's realy easy in visual C#, create a new class library project.
    add a reference to the UnityEngine.dll in the project.
    add your functions to the class.
    build the dll and copy it in the assets folder.
    call your functions from your script.
     
  11. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    Does building such things into a dll offer any performance benefits?
     
  12. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    i don't think so.
    the goal for me is just to be able to reuse code in different projects easily.
     
  13. WhendricSo

    WhendricSo

    Joined:
    Jan 1, 2011
    Posts:
    171
    building a DLL is useful if you want to sell your work as a plugin, because it prevents others from easily viewing your source code
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, because it's trivial to decompile DLLs. Anyway you should generally be providing source code regardless, since many people won't buy without it. The primary reasons for using a DLL are 1) everything consolidated into one file, 2) double-clicking errors in the console goes to where it was called in your code, rather than the source code, which is much more useful when you're actually trying to use the code as a regular user rather than as the developer, and 3) since they're already compiled, having DLLs in your project adds nothing to script compilation times.

    --Eric
     
  15. zd

    zd

    Joined:
    Apr 18, 2013
    Posts:
    6
    Andrey-Postelzhuk likes this.
  16. franktinsley

    franktinsley

    Joined:
    Jul 1, 2010
    Posts:
    130
    There's actually a really good argument for not supplying source code. It enforces that new features be requested by users and added by the original author to the source so everyone using the product can benefit.
     
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, not at all. I supply source code and it's still the case that new features are requested by users and added by me. There's no indication that having a DLL (only) would change that behavior. As I mentioned, it's trivial to decompile DLLs anyway.

    --Eric
     
    lordofduct likes this.