Search Unity

my experience on dll

Discussion in 'Editor & General Support' started by masood-t, Dec 21, 2012.

  1. masood-t

    masood-t

    Joined:
    Nov 25, 2012
    Posts:
    13
    Hi
    I need to write a dll in visualstudio C# and use it in unity and after some problems finally i can do it.
    because search and find get many time of me i am createing this topic to write it so you can use with less search:

    at visual studio:
    i made a dll project and code is :
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. //using System.Linq;
    5. using System.Text;
    6.  
    7. namespace ClassLibrary1
    8. {
    9.     public class Class1
    10.     {
    11.         public static int Add(int i, int j)
    12.         {
    13.             return (i + j)*1000;
    14.         }
    15.     }
    16. }
    17.  
    after build it , i copy dll to my project in asset folder and write a js code like this:
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. function Start () {
    5.     print(ClassLibrary1.Class1.Add(3, 4));
    6. }
    7.  
    8. function Update () {
    9.  
    10. }
    11.  
    just this.no need to use [dllimport ... and other thing.
    and finally attach this js to an object (like empty object)

    1-in VS goto project menu and go to properties (last item) and set "target frame work" to ".net framework 2.0"
    2-in unity goto edit>project setting>player and in "setting for pc and mac stand alone" set ".net 2 subset" for "api compatibility level "
     
  2. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    You still need to dllimport if the program is a native plugin.
     
  3. masood-t

    masood-t

    Joined:
    Nov 25, 2012
    Posts:
    13
    and this is it's C# code for unity:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Runtime.InteropServices;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour {
    7.      [DllImport ("ClassLibrary1")]
    8.     private static extern int Add(int i, int j);
    9.    
    10.     void Start () {
    11.         print ("from c#"+ ClassLibrary1.Class1.Add (3,2));
    12.     }
    13.     void Update () {
    14.    
    15.     }
    16. }
    17.  
    18.  

    important note: i use " public static int" in dll but i have to use "private static extern int" in predefinition
     
    Last edited: Dec 21, 2012
  4. masood-t

    masood-t

    Joined:
    Nov 25, 2012
    Posts:
    13
    i think you can't use dllimport in js codes.
    and do you expalne what's the meaning of "native"?
     
  5. masood-t

    masood-t

    Joined:
    Nov 25, 2012
    Posts:
    13
    my delphi dll code:
    Code (csharp):
    1.  
    2. library Project1;
    3. uses
    4.   SysUtils, Classes;
    5.  
    6. {$R *.res}
    7. function AddIntegers(_a, _b: integer): integer;stdcall;
    8. begin
    9.   Result := _a + _b;
    10. end;
    11.  
    12. exports
    13.    AddIntegers;
    14. begin
    15.  
    16. end.
    17.  
    my js code:
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. function Start () {
    5.     var a:int;
    6.     a= Project1.AddIntegers(3,33);
    7.     print(a);
    8. }
    9.  
    10. function Update () {
    11.  
    12. }
    13.  
    but something is wrong.
    i will refresh this post if i can solve problems.
     
  6. masood-t

    masood-t

    Joined:
    Nov 25, 2012
    Posts:
    13
  7. masood-t

    masood-t

    Joined:
    Nov 25, 2012
    Posts:
    13
    I have to use a dll that made by delphi but still have problem.
    i need a small code for c# or js in unity3d.
    can anyone show me a sample: