Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Step by step tutorial for c# .net dll and Unity3D(Pro only).

Discussion in 'Scripting' started by Mr.Smart, Aug 5, 2011.

?

Are you Like my Tutorial?

  1. yes

    21 vote(s)
    77.8%
  2. normal

    1 vote(s)
    3.7%
  3. no

    5 vote(s)
    18.5%
  1. Mr.Smart

    Mr.Smart

    Joined:
    Aug 5, 2011
    Posts:
    54
    Hi, today i will explain you How to create and manage library(.dll) files in your unity project.

    Tools:
    1.Unity3D Pro
    2.VISUAL C# 2010 EXPRESS or Visual Studio 2010,
    3.C# Basic knowledge
    4.Brain
    5.Hands :cool:.


    Starting new Project:

    Open VISUAL C# 2010 EXPRESS or Visual Studio 2010, go to File>New Project>Visual C#>Class Library. Set Your file name(I named it Tutorial) and Choose(.NET Framework 3.5) not above, otherwise unity will not read .dll.

    after setup it will open something like this.

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Text;
    6.  
    7. namespace Tutorial
    8. {
    9.     public class Class1
    10.     {
    11.     }
    12. }
    13.  
    Ok lets start coding.... :-|

    for more beautifulnes lets change class name (REMEMBER: when you change class name, change also file name in the project) I actualy changing name of the project .cs file name, and it automaticly changing class name for me :cool: .

    Now my code will look like this:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Text;
    6.  
    7. namespace Tutorial
    8. {
    9.     public class tut
    10.     {
    11.       //code goes here
    12.     }
    13. }
    14.  
    15.  
    the (namespace Tutorial) will be used in our unity later, it`s mean we must leave it exactly that it or you can change it to other name, for now i will use (namespace Tutorial);

    Now i will put some basic function with name "textview" and it will be string. Then i will returne my name.

    to construct public function in c# you make it like this (public [string,int,etc...] [function name]() { }).

    OK my function is like this

    Code (csharp):
    1.  
    2. public string textview()
    3.         {
    4.             return "Mr.Smart";
    5.         }
    6.  

    Now my code will look like this:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Text;
    6.  
    7. namespace Tutorial
    8. {
    9.     public class tut
    10.     {
    11.  
    12.         public string textview()
    13.         {
    14.             return "Mr.Smart";
    15.         }
    16.     }
    17. }
    18.  
    My dll is ready, i need to build it. go to Build>Configuration Manager... and change debug to release.

    your dll must be builded in yourproject\bin\Release folder.

    Now go to your Unity project folder and open Assets folder, in that folder make "new folder" called "Plugins".

    This will be you plugin folder, paste your.dll to that folder, Unity will automaticly import it.

    Usage is very seample open your c# script in your unity project.
    Lets say that we make new script and there is nothing.

    Blank c# script with class name tutorial.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class tutorial : MonoBehaviour {
    6.    
    7.    
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17. }
    18.  
    19.  
    to get dll work in our script we must make new destination to it.

    seamply we will put in upper side "using [your dll file name];" it will automaticly popup if your dll script will not have any error, that is why we didnt delete the "namespace Tutorial" from our dll code.

    when we inported dll we must make new variable with the new class of our dll.

    for me it will look like
    Code (csharp):
    1.  public tut a =  new tut();
    it somthing like to make new Vector3 class.

    We have imported dll , we have new class. Now we need to call function in our "tut()" class.

    it will look like this
    Code (csharp):
    1. a.textview()
    and i will print it in my awake function
    Code (csharp):
    1.  
    2. void Awake(){
    3.         //printing
    4.         print(a.textview());
    5.     }
    6.  
    My code finaly will look like this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using Tutorial;
    5. public class tutorial : MonoBehaviour {
    6.    
    7.    
    8.     //making the new class
    9.     public tut a = new tut();
    10.    
    11.     //on awake
    12.     void Awake(){
    13.         //printing
    14.         print(a.textview());
    15.     }
    16.     // Use this for initialization
    17.     void Start () {
    18.    
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.    
    24.     }
    25. }
    26.  
    27.  
    Attach the script to some object and run game. you should recieve my name in console window.

    Tutorial Source: View attachment $tutorial.zip
    With regards Mr.Smart
     
    Last edited: Aug 16, 2011
  2. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    2 notes:

    1) That's in depth, I just drag and drop the dll :)

    2) Unless there's a detail I missed, there is no reason that won't work in Indy free.
     
  3. Mr.Smart

    Mr.Smart

    Joined:
    Aug 5, 2011
    Posts:
    54
    Wow you are superman!!

    this is not draging droping tutorial this is basic to how construct new plugin , what about the Free or an pro you can see in Unity manual
    Unity - Plugins - Pro/Mobile-Only Feature
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    As much as I agree on your point on 1 (good tutorial btw) he is right on 2 :)

    This is no plugin in the sense of a "requires pro" plugin, this is just external code compiled into an assembly which indeed works for free and pro :) (on mobile its a bit different as it depends on what you used, system.net for example would lead to iOS Pro / Android Pro only)


    Plugins as per the documentation on plugins and the feature limitation tables are native code plugins so unmanaged code + bridging to it through DLLImport and the InteropServices, which you don't use here :)
     
  5. Mr.Smart

    Mr.Smart

    Joined:
    Aug 5, 2011
    Posts:
    54
    Nice one , i like your explanation
     
  6. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    As mentioned by dreamora:

    Unity has extensive support for C, C++ or Objective-C based Plugins. Plugins allow your game code (written in Javascript, C# or Boo) to call into native code libraries.

    This tutorial is about none of those plugins, so the Pro only restriction does not apply.

    -----

    There's nothing wrong with the depth, just pointing out that (mainly to readers) that the length is due to depth, not because of the complexity. It's also a compliment.
     
    Last edited: Aug 5, 2011
  7. Mr.Smart

    Mr.Smart

    Joined:
    Aug 5, 2011
    Posts:
    54

    lets imagine tutorial like this :)
    DLL CODE
    Code (csharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5.  
    6. namespace Tutorial
    7. {
    8.     public class tut
    9.     {
    10.  
    11.         public string textview()
    12.         {
    13.             return "Mr.Smart";
    14.         }
    15.     }
    16. }
    17.  
    SCRIPT CODE
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Runtime.InteropServices;
    4. using Tutorial;
    5. public class tutorial : MonoBehaviour {
    6.    
    7.    
    8.     //bulding the new class
    9.     public tut a = new tut();
    10.    
    11.     //on awake
    12.     void Awake(){
    13.         //printing
    14.         print(a.textview());
    15.     }
    16.     // Use this for initialization
    17.     void Start () {
    18.    
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.    
    24.     }
    25. }
    26.  
    are you like this way mate ?? ;)
     
  8. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Hehehe this is my tutorial:

    1. Create a DLL in VS.
    2. Drag and drop.
    3. Use Import.
    But then again, I'd suck at explaining things to beginners :p

    In all seriousness, this is defiantly one of the better tutorial efforts out there. It could do with an editor, but the level of detail is impressive. Keep it up!
     
    Last edited: Aug 5, 2011
  9. Mr.Smart

    Mr.Smart

    Joined:
    Aug 5, 2011
    Posts:
    54
    :D Yeah realy nice one friend.
     
  10. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    good tutorial. Something new that i can learn. Which is useful to me in the future. :D Good job.
     
  11. Mr.Smart

    Mr.Smart

    Joined:
    Aug 5, 2011
    Posts:
    54
    Thenks mate :)
     
  12. SEG-Veenstra

    SEG-Veenstra

    Joined:
    Sep 3, 2010
    Posts:
    326
    Very useful, thanks. I'm at a point that I'm trying to learn as much as possible atm, so these kind of tutorials are good for me!
     
  13. vreference

    vreference

    Joined:
    Mar 22, 2011
    Posts:
    154
    Just so we are clear - there is effectively no advantage (other than organization) to using compiled .net .dlls, correct?

    code obfuscation maybe?
     
  14. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    As mentioned by others, .net assembly dlls are indeed supported on all platforms and all licenses, whereas binary C-style dlls are not available on the free Unity license and also not supported in the WebPlayer.

    .net assemblies can easily be decompiled. I primarily use them to simplify distribution. You can include a lot of script files in one and even other assets - such as textures.

    Personally I build my assemblies from the command-line - using a perl script - of which I have an example here:
    http://angryant.com/general/tipsandtricks/assembling-and-assimilating/

    An additional use of assemblies would be runtime updating and patching by sending assemblies to an executing player, caching and running it, without restarting the application. You'll find an example of that here:
    http://angryant.com/general/tipsandtricks/downloading-the-hydra/
     
  15. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Thanks for this...
     
  16. frank45

    frank45

    Joined:
    Apr 23, 2012
    Posts:
    26
    Awesome tutorial really helped me a lot...!! and also if there is any way in which we can import managed Dll into unity and use functions of it..!!
     
  17. Ersin

    Ersin

    Joined:
    Oct 25, 2012
    Posts:
    13
    Thank you !
     
  18. marsian

    marsian

    Joined:
    Feb 3, 2013
    Posts:
    1
    Thank you!!
     
  19. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    One thing you may find useful if you're using Visual Studio to write your DLL anyway is setting up a post-build event to automatically copy the DLL to the appropriate areas of your project. I use 2012, so it may differ based on what version you're using, but for me it's Project -> Properties -> Build Events

    I set up "Post-build event command line:" and I have it set to "Run the post build event: On successful build"

    My actual event is:

    Code (csharp):
    1.  
    2. copy "$(TargetDir)\Sortasoft.dll" "E:\Documents\Projects\Clients\Meriwether\Assets\Middleware\First Party\"
    3. copy "$(TargetDir)\Sortasoft.dll" "E:\Documents\Projects\Unity\Adaptation\Assets\Sortasoft\"
    4.  
    While this exact process is VS specific, I'm sure pretty much every IDE has some form of the exact same thing.
     
  20. juanzhao5

    juanzhao5

    Joined:
    Nov 15, 2017
    Posts:
    7
    Want to run a .net framework 4.x or 3.x client against a asp.net core 2.0 signalr service. possible?

    for now my client is a .net framework 4.x or 3.x console app. eventually it will be a DLL i drop into unity3d plugins directory.

    Then i can call my methods like so MyNet4ClientClass.MakeSignalrCoreCall(data).

    Is this possible? Can .NET 3.x or 4.x talk to asp.net core 2.0 signalr service?

    I tried to add the nuget package for Microsoft.AspNetCore.SignalR 1.0.0-alpha2-final but....

    "Could not install package 'Microsoft.AspNetCore.SignalR 1.0.0-alpha2-final'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author. 0 "

    i thought this post was hopeful https://www.rizamarhaban.com/2016/09/13/asp-net-core-signalr-for-windows-10-uwp-app/

    in the post the guy talks to signalr code 2.0 from a UWP app the client UWP app uses "Microsoft.AspNet.SignalR.Client": "2.2.1" from nuget

    and service which uses Microsoft.AspNetCore.SignalR.Server: 0.2.0-alpha1-22118 obtained from https://dotnet.myget.org/F/aspnetcore-ci-dev/api/v3/index.json repository

    this works its seems because these older SignalR.Server packages still exposes themselves the old signalr way. you can see from the url it exposes at /signalr/hubs

    but the new new signalr core changed its protocol sometime so now incompatible.

    how can i talk to the new new signalr core from .net framework 4.x or 3.x client ?