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 :
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
namespace ClassLibrary1
{
public class Class1
{
public static int Add(int i, int j)
{
return (i + j)*1000;
}
}
}
after build it , i copy dll to my project in asset folder and write a js code like this:
#pragma strict
function Start () {
print(ClassLibrary1.Class1.Add(3, 4));
}
function Update () {
}
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 "
You still need to dllimport if the program is a native plugin.
and this is it’s C# code for unity:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class NewBehaviourScript : MonoBehaviour {
[DllImport ("ClassLibrary1")]
private static extern int Add(int i, int j);
void Start () {
print ("from c#"+ ClassLibrary1.Class1.Add (3,2));
}
void Update () {
}
}
important note: i use " public static int" in dll but i have to use “private static extern int” in predefinition
i think you can’t use dllimport in js codes.
and do you expalne what’s the meaning of “native”?
my delphi dll code:
library Project1;
uses
SysUtils, Classes;
{$R *.res}
function AddIntegers(_a, _b: integer): integer;stdcall;
begin
Result := _a + _b;
end;
exports
AddIntegers;
begin
end.
my js code:
#pragma strict
function Start () {
var a:int;
a= Project1.AddIntegers(3,33);
print(a);
}
function Update () {
}
but something is wrong.
i will refresh this post if i can solve problems.
using dll function by creating it’s class instance:
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: